2. Preparation of Java project
Main steps
- Import project
- Rename project
- Rebuild project
- Implement TODO
- Learn to compile and execute classes
- Run Unit test
- Where Eclipse settings are stored
- Where IntelliJ IDEA settings are stored
- Optional tasks
Import project
- Open Eclipse IDE:
- open IDEs/eclipse folder in file manager,
- double click on eclipse file and select Execute.
- Download JTM.zip file
- Extract JTM.zip archive in the project folder of your IDE, e.g. in workspace folder
- Import Java project in Eclipse:
- select menu: File— Import...
- select Maven— Existing Maven Projects and press Next
- Check Select root directory and choose your workspace folder and open it
- Select extracted project from archive /JTM/pom.xml...
- Finish
Rename project
- Get unique identifier for your project from your teacher (e.g. uXX)
- Right click on JTM and select Refactor—Rename
- Rename project in form: uXX (where uXX is actual output from previously entered command)
- Open your project folder e.g. /home/student/workspace/uXX.
- Open file pom.xml and change line <artifactId>JTM</artifactId> to: <artifactId>uXX</artifactId>
Rebuild project
Go to your project and regenerate project meta-info for Eclipse, e.g.:
cd ~/workspace/uXX
mvn clean compile eclipse:clean eclipse:eclipse- Refresh project explorer in Eclipse: select project in Package Explorer and press F5.
Checkpoints
Check that project name in Eclipse is e.g. uXX
Check that your project is located in folder e.g. /student/workspace/uXX/
Check that file /home/student/workspace/uXX/.project has line: <name>uXX</name>
Select Window — Preferences, search for Java — Build Path — Class Variables and check M2_REPO has value /home/student/.m2/repository. If this variable doesn't exist, press New..., enter necessary name and value, press OK and agree to recompile entire project.
Implement TODO
- Select menu Window — Open Perspective — Java
- Right click on Resource button and select Close
- Use code generation to create class Hello (Hello.java file) with public static void main(String[] args) method in it.
- In the Project Explorer view, select package: jtm.activity02
- Right click on the package and select: New — Class
- For field Name enter: Hello
- In group Which method stubs would you like to create? select public static void main(String[] args)
- Press Finish
Add following code into main method, to print hello message in different languages:
System.out.println("Hello! Sveicināti! 你好!");
- Run HelloTest1.java as JUnit test to test proper implementation of this class.
- Right click on HelloTest1.java
- Select Run As — JUnit Test
- Check that JUnit view shows green status bar
- To to doube-check system configuration, run also ConfigurationTest1.java Junit in jtm.activity01 package.
- If necessary, change waitRatio = 1f value in jtm.testssuite.AllTests class to bigger value to allow tests to run longer
Learn to compile and execute classes
In Eclipse
- Select class and activate menu Run — Run as... — Java application
In command line
Open terminal and enter commands:
cd ~/workspace/uXX/
java -version
javac -cp target/classes -d target/classes src/main/java/jtm/activity02/Hello.java
java -cp target/classes jtm.activity02.Hello
Run Unit test
- Select menu Run— Run as— Java Application
- Select menu Run— Run as— JUnit Test
- Check that unit test passes
Where Eclipse settings are stored
- .project .classpath files of the project
- .settings folder of the project
- .metadata folder of the workspace
- .eclipse folder of the Eclipse
Where IntelliJ IDEA settings are stored
- name.iml file and .idea folder of the project
- .config/JetBrains/Idea.... folder of the IDEA
Optional tasks
Create desktop entry for Eclipse
- Right click on desktop
- Select Create launcher...
- Enter:
- name: Eclispse,
- Command: /home/student/IDEs/eclipse/eclipse
- If you like select Icon: /home/student/IDEs/eclipse/icon.xpm
- Save desktop entry
Review topic: JavaExerciseExtra16
Export project:
- Select project package explorer view
- select File — Export... — General — Archive File,
- to archive file: /home/student/uXX.zip,
- Finish.
Then you can import project as it was described earlier.