Programming in Java with Eclipse
Total Page:16
File Type:pdf, Size:1020Kb
INFO0062 - Object-Oriented Programming Programming in Java with Eclipse Jean-François Grailet University of Liège Faculty of Applied Sciences Academic Year 2018 - 2019 Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse About Eclipse Eclipse is an Integrated Development Environment. It’s a program specifically designed for software development. It provides • a code editor (with code coloration) • compilation and debugging tools • project management, auto-completion, etc. For those who followed INFO2009 or INFO0946, it’s like Code::Blocks. Eclipse is a very simple solution to program in Java under Windows. • macOS/Linux users can use it too, but using java/javac is a simpler approach. • Eclipse can however be arguably more practical for large-scale projects. 1 / 17 Installation of Java and Eclipse Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse How to install Eclipse You will need to: • install Java on your computer • install Eclipse itself 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). 2 / 17 Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse 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 3 / 17 Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse Installation of Eclipse Visit Eclipse’s official website Click on “Download Packages” (below main download button). Download a version in “Eclipse IDE for Java Developers”. Be careful, you must not take the Java EE edition ! • EE = Enterprise Edition Under Ubuntu and alike (Linux), you can also run $ sudo apt-get install eclipse 4 / 17 Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse Installation of Eclipse (II) The file you downloaded is an archive. Just unzip it and place its contents in a folder of your choice. To run Eclipse, just run the application file at the root of the unzipped folder. • Don’t hesitate to create a shortcut on your desktop towards this file. N.B.: it’s possible that Eclipse doesn’t run at first launch. • See next slide. 5 / 17 Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse Troubleshooting Eclipse start-up Eclipse refuses to start because it can’t find Java. To fix this, you need to edit the eclipse.ini file. Said file is located in the same folder as the application file. Before the -vmargs line, add this: -vm C:/Program Files/Java/jdk-11.0.2/bin/javaw.exe You might have to change the second line. • It’s the path to a file that was installed with Java. • Browse your computer to find it if this path doesn’t work. • The path might still be very similar. 6 / 17 Creating a project in Eclipse Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse Creating your workspace When you first use it, Eclipse will ask you to select a workspace. This workspace is a folder where Eclipse will place all its files. • This includes project data, but also your source .java files. In your computer, create such a folder (wherever you wish). • For instance, you can create INFO0062 in your Documents. 7 / 17 Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse Creating your workspace (II) Select it in Eclipse. Check the box to ensure Eclipse don’t ask you again about this folder. 8 / 17 Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse Your first project in Eclipse Eclipse will welcome you with a variety of options. Select “Create a new Java project”. 9 / 17 Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse Your first project in Eclipse (II) Enter a project name and click on Finish. 10 / 17 Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse Your first project in Eclipse (III) At first, your project will be empty. Note that Eclipse might suggest you to create a first module. • You can completely ignore this suggestion. • Click on Don’t create it. To add a new .java file, right-click on your project. • It’s located in the left column of your screen. Unfold the New menu and select File. 11 / 17 Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse Your first project in Eclipse (IV) Make double sure the file will be saved in the src folder. Then, give it a name (e.g.: HelloWorld) and suffix it with .java. 12 / 17 Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse Your first project in Eclipse (V) For a quick test, write a single main() method. Note that compilation errors/warnings, if any, will appear in the bottom panel. 13 / 17 Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse Your first project in Eclipse (VI) Compile and run it by clicking on the green play button. The message printed by your main() will appear in the bottom panel. 14 / 17 Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse Closing comments Depending on the situation, various dialogs might appear. For instance, there might be one if you want to compile/run an unsaved file. Just read carefully the messages when this occurs. • In this specific case, allow Eclipse to save your file(s). 15 / 17 Program arguments in Eclipse Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse Program arguments in Eclipse You can pass arguments to a Java program upon starting it. We will see during the semester some practical uses of this mechanism. To pass arguments in Eclipse, perform a right click on the Run button. In the menu that dropped, select "Run Configurations...". 16 / 17 Installation of Java and Eclipse Creating a project in Eclipse Program arguments in Eclipse Program arguments in Eclipse (II) Go to the Arguments tab. Enter the argument(s) to transmit in Program arguments. Finish by clicking on Run at the bottom of the window. 17 / 17.