If it is not explicitly told in following recipe, setting up services are described for Ubuntu 16.04 server, but applications are described for Ubuntu 16.04 and/or Lubuntu 16.04 workstation. If you use different Ubuntu version or Linux distribution, settings as well as content, names and places of configuration files may be different!
Got it.

2. Preparation of Java project

Review Introduction to Java

  

Review notes about practical exercise

  

Practical tasks

Import project

  1. Download JTM.zip file
  2. Open Eclipse IDE:
    1. open IDEs/eclipse folder in file manager,
    2. double click on eclipse file and select Execute;
  3. Import Java project in Eclipse:
    1. select menu: File— Import...
    2. General— Existing projects into workspace, Next
    3. Check Select archive file 
    4. Select downloaded file e.g. /home/student/Downloads/JTM.zip
    5. Finish

Rename project

  1. Generate your own unique project name with following command:

    head -c 32 /dev/urandom|sha256sum|xxd -r -p|base64|cut -b -6|tr "+/=" "abc"

    If you don't have necessary GNU tools on your computer, get identifier from here.

  2. Right click on JTM and select Refactor—Rename
  3. Rename project in form: Aaaaaa (where Aaaaaa is actual output from previously entered command)
  4. Open your project folder e.g. /home/student/workspace/Aaaaaa.
  5. Open file pom.xml and change line <artifactId>JTM</artifactId> to: <artifactId>Aaaaaa</artifactId>
Generated code e.g. Aaaaaa will be used as unique identifier for your project and knowledge checks!

Rebuild project

  1. If necessary (for recent Eclipse versions it is not necessary), open terminal and create M2_REPO variable for Eclipse:

    cd
    mvn -Declipse.workspace=/home/student/workspace/ eclipse:configure-workspace
  2. Go to your project and enerate project meta-info for Eclipse, e.g.:

    cd ~/workspace/Aaaaaa
    mvn clean compile eclipse:clean eclipse:eclipse
  3. Refresh project explorer in Eclipse

Checkpoints

help Check that project name in Eclipse is e.g. Aaaaaa
help Check that your project is located in folder e.g. /student/workspace/Aaaaaa/
help Check that file /home/student/workspace/Aaaaaa/.project has line: <name>Aaaaaa</name>
help 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

  1. Select menu Window — Open Perspective — Java
  2. Right click on Resource button and select Close
  3. Use code generation to create class Hello (Hello.java file) with public static void main(String[] args) method in it.
    1. In the Project Explorer view, select package: main.java.jtm.activity02
    2. Right click on the package and select: New — Class
    3. For field Name enter: Hello
    4. In group Which method stubs would you like to create? select public static void main(String[] args)
    5. Press Finish
  4. Add following code into main method, to print hello message in different languages:

    System.out.println("Hello! Sveicināti! 你好!");

    If you can't properly save non-Latin characters as literal value of printed message, check your project settings:

    1. Right click on project root entry (e.g. Aaaaaa) in Package explorer:
    2. Select Resource menu and check that Text file encoding is UTF-8 either as inherited setting from container,
      or set explicitly.
  1. Run HelloTest1.java as JUnit test to test proper implementation of this class.
    1. Right click on HelloTest1.java
    2. Select Run As — JUnit Test
    3. Check that JUnit view shows green status bar
    4. To to doube-check system configuration, run also ConfigurationTest1.java Junit in jtm.activity01 package.

Learn to compile and execute classes

In Eclipse

  1. Select class and activate menu Run — Run as... — Java application

In command line

  1. Open terminal and enter commands:

    cd ~/workspace/Aaaaaa/
    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

  1. Select menu Run— Run as— Java Application
  2. Select menu Run— Run as— JUnit Test
  3. 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

Optional tasks

Create desktop entry for Eclipse

  1. Right click on desktop
  2. Select Create launcher...
  3. Enter:
    1. name: Eclispse,
    2. Command: /home/student/IDEs/eclipse/eclipse
    3. If you like select Icon: /home/student/IDEs/eclipse/icon.xpm
  4. Save desktop entry

Review topic: Extra 16: Java building tools

Export project:

  1. Select project package explorer view
  2. select File — Export... — General — Archive File,
  3. to archive file: /home/student/JTM.zip,
  4. Finish.
    Then you can import project as it was described earlier.

  
Tags:
Created by Valdis Vītoliņš on 2020-05-28 13:26
Last modified by Valdis Vītoliņš on 2020-05-28 13:27
 
Xwiki Powered
Creative Commons Attribution 3.0 Unported License