INFO0062 - Object-Oriented Programming Programming in Java with a terminal Jean-François Grailet University of Liège Faculty of Applied Sciences Academic Year 2018-2019
1 / 13 About using a terminal Under a Linux distribution or macos, you can use a terminal. To put it simply, it s a small window where you can type various commands. Such a mecanism also exists under Windows, but is less convenient to use. Using a terminal to program in Java has multiple advantages: You only need to install Java on your computer. The only other program you might want to add afterwards is a code editor. It s very simple to use. You don t have to handle your programs as projects. Just provide all relevant source files to the compiler. That s it!
Installation of Java
2 / 13 How to install Java To install Java SE (Standard Edition), you can Windows/macOS: download it on Oracle s website. Linux: install it via an online repository. N.B.: Java s lattest versions are 64-bit only. I.e., only compatible with a device that has a 64-bit hardware. Hopefully, most computers (including laptops) are 64-bit nowadays. If your device is quite old, it might be worth to double check it. N.B.: quite old here means more than several years (e.g. 5).
3 / 13 How to install Java SE Windows/macOS Download and install Java SE 11 32-bit Windows: download and install Java SE Development Kit 8u201 for x86 Linux You can use an online repository to download and install Java SE 11 32-bit devices: prefer using your OS default repositories. $ sudo apt-get update $ sudo apt-get install default-jre $ sudo apt-get install default-jdk
Compiling/running Java in a terminal
4 / 13 Two commands You will need to know and use two distinct commands: javac: the Java compiler. java: the Java command, used to run a Java program. The reason why there are two commands is Java s design. As explained in the course, Java code is compiled into bytecode. This code isn t directly executable and needs to be interpreted. The java command is the interpreter here. Java was designed this way to ensure portability of applications. I.e., ensuring a same program can run on computers with differing environments.
5 / 13 Compiling and running a simple Java program Suppose you want to compile a single class: open a terminal. Using the cd command, place yourself in the directory containing your code. cd is for "change directory". To check the contents of the current folder, you can also use ls. You will find many tutorials online about these simple but common commands. Tutorial in French $ cd MyJavaDirectory/
6 / 13 Compiling and running a simple Java program (II) Suppose our class is named MyClass. To compile it, run this command: $ javac MyClass.java If compilation is successful, you can run it with: $ java MyClass That s it!
7 / 13 Compiling and running a Java program made of several classes At compilation, you must provide all source files to javac. Upon running the program, you only need to provide the main class. For instance, let s say we have a program consisting of the following classes: MyMain (main class) ClassA, ClassB Run these commands: $ javac ClassA.java ClassB.java MyMain.java $ java MyMain
8 / 13 Compilation options javac places.class files in the same folder as the.java files. If you want to separate both kinds of files, you can create an output directory. Use the -d flag of javac to do so. If we want MyClass.class in a bin/ folder: $ javac -d bin MyClass.java To run a.class file located in a bin/, you will have to type $ java -cp bin MyClass -cp is another flag of javac used to specify where to look for.class.
9 / 13 Compilation options (II) Note that you can provide paths to javac. For instance, if you place your.java files in a folder src/: $ javac -d bin src/myclass.java In fact, placing.java in src/ and.class in bin/ is a common practice. Both kinds of files are clearly isolated from each other. Home folder can consist of documentation, usage examples, etc. By default, Eclipse creates src/ and bin/ folders when creating a project.
About Linux
A few words on Linux At this point in your cursus, you probably have heard about Linux a lot. Teacher(s) introduced you to the Linux terminal and its command lines. You most probably had laboratory sessions with Linux distributions.... and maybe some zealots tried to convert you to Linux! Why is it a big deal in computer science? Most Linux distributions are free. Programming under Linux is usually much easier. This is especially true for simple programs. With practice, using a terminal is also handier 1 than a GUI. Computer scientists should consider using Linux. 1 FR: plus pratique, plus commode 10 / 13
11 / 13 How can I use Linux? You can discover and use Linux without modifying or dumping your current OS. Running a Linux distribution in a virtual machine (FR) Using a bootable USB stick (EN) You can also dual boot (FR) your computer. Both your original OS and Linux will co-exist on your computer. You will select the OS to run when powering-up your computer. Requires to partition your disk space. Don t do this now if you are not confident about modifying your disk.
12 / 13 Which Linux distribution should I pick? Typically, Ubuntu ("Linux for human beings") is recommended for beginners. Has a complete GUI for browsing files and running programs, and even a store Terminal is not essential for a beginner to use it Other popular distributions you can consider using Mint Fedora
Code editors you can use
13 / 13 Code editors you can use To write Java code conveniently, get a text editor with code coloration. macos - TextWrangler Linux - Geany Linux - Emacs In some Linux distributions like Ubuntu, you can also use Gedit (native). If you also need such a text editor for Windows, consider trying Notepad++.