If it is not explicitly told in following recipe, setting up services are described for Ubuntu 20.04 server, but applications are described for Xubuntu 20.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. Java project setup

Review Introduction to Java

Tasks

Clone the project

  1. Open terminal
  2. Change current folder to the IDE's project folder
  3. Clone project with command:

    git clone https://odo.lv/git/JTM/
  4. Open cloned project in IDE (e.g. Eclipse or IntelliJ IDEA)
  5. In project options set it as Maven project

Rename the project

  1. Use your student ID in the project name, for example aa00000.
  2. Right-click on JTM and select Refactor—Rename and rename the project in the following format: aa00000
  3. Open your project folder, for example /home/student/workspace/aa00000.
  4. Open the file pom.xml and change the line <artifactId>JTM</artifactId> to: <artifactId>aa00000</artifactId>

In all places in the description, replace aa00000 with your actual student ID.
This code will also be used as a unique identifier for your project and knowledge tests!

Rebuild the project

  1. Go to your project and recompile it:

    cd ~/workspace/aa00000
    mvn clean compile
  2. If you use Eclipse, update setting files:

    mvn eclipse:clean eclipse:eclipse

    In Eclipse select the project in Package Explorer and press F5

Checkpoints

help Check that the Eclipse project name is e.g. aa00000
help Check if your project is in a folder, e.g. /student/workspace/aa00000/
help Check if the file /home/student/workspace/aa00000/.project contains the line: <name>aa00000</name>
help Select Window — Preferences, look for Java — Build Path — Class Variables and check if M2_REPO has the value /home/student/.m2/repository. If this variable does not exist, press New..., enter the desired name and value, press OK and agree to recompile the entire project.

Execute TODO

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

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

    If you are unable to correctly save non-Latin characters as the letter value of the printed message, check the project settings:

    1. Right-click the root entry of the project (e.g., aa00000) in the Package Explorer:
    2. Select the Resource menu and check that Text file encoding is UTF-8 either as an inherited setting from the container or explicitly set.
  1. Run HelloTest1.java as a JUnit test to verify the correct implementation of this class.
    1. Right-click HelloTest1.java.
    2. Select Run as — JUnit test
    3. Verify that a green status bar is visible in the JUnit view
    4. To double-check the system settings, also run ConfigTests.java in the Junit package jtm.activity01.
  2. If necessary, change the waitRatio = 1f value in the jtm.testssuite.AllTests class to a higher value to allow the tests to run longer.

Learn to compile and execute classes

Inside Eclipse

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

On the command line

  1. Open a terminal and enter the commands:

    cd ~/workspace/aa00000/
    java -version
    javac -cp target/classes -d target/classes src/main/java/jtm/activity02/Hello.java
    java -cp target/classes jtm.activity02.Hello

Run a unit test

  1. Select the Run— Run as— Java Application menu
  2. Select the Run— Run as— JUnit Test menu
  3. Verify that the unit tests passed successfully

Create a Git repository on the server

Use your StudentId on estudijas.lu.lv as your username and initial password DatZB028-2025-StudentaId

  1. Create an SSH connection to cserver

    ssh aa00000@cserver

    where aa00000 is your StudentId
    (and bcerver is the short name created in the file /etc/hosts for tools.odo.lv)

  2. Accept server key fingerprint:

    The authenticity of host 'cserver (<no hostip for proxy command>)' can't be established.
    ECDSA key fingerprint is SHA256:EfeSv9IS82KmbOHismH1JPkqC/j0fxvoe5GK1jR2KVY.
    Are you sure you want to continue connecting (yes/no/[fingerprint])?

    type yes

  3. Enter your original user password:

    aa00000@cserver's password:

    type your password and press enter

    Don't be surprised that the password is not displayed as asterisks/circles/whatever. The password entry is invisible.

  4. Change your user password by entering the command cserver:

    passwd

    and enter the requested information:

    Current password:
    New password:
    Retype new password:

    original password set by the teacher
    your new password
    your new password

    Projects for users with the original password will not be checked out!

  5. Create a git remote repository, and log out of the server:

    mkdir aa00000
    cd aa00000
    git --bare init
    exit
  6. Rename project source repository:
    git remote rename origin source
  7. Add the remote repository to the project. (Note that your student code is in the URL three times: as a username on the server, in the user's home folder, and as a Java project in it with the same name.):

    git remote add origin ssh://aa00000@cserver/home/students/aa00000/aa00000

    or if you want to change the settings, open the .git/config file in your project and change the settings:

    ...
    [remote "origin"]
    url = ssh://aa00000@cserver/home/students/aa00000/aa00000
    ...
  8. Update .gitignore file in your project folder with the following content:

    .classpath
    .idea/**
    *.iml
    **/.~lock*
    *.log
    logs/**
    .metadata/**
    .project
    .settings/**
    doc/**
    target/**
    wbp-meta/**

Check settings

help Run ConfigAndHelloTest1 unit test package jtm.activity02 and check the results.

Commit and push changes

All changes can be made to the local repository and pushed to the remote repository using the following commands:1:

cd ~/workspace/aa00000
git add -A
git commit -m "Your comment"
git push -u origin master

Update your local repository and push your changes to the remote repository

  1. Use git status to see what has changed since the last commitm committed changes
  2. Add all changes to your local repository and push them to the remote repository:

    cd ~/workspace/aa00000
    git pull
    git add -A
    git commit -m "Your comment"
    git push
  3. If you have a local git repository with a different history than your remote project, and you know your local project is fine, you can force push your project history to the server by adding the -f switch, e.g. :

    git push -f origin master
  4. To ensure that all of your local repository's history is pushed to your remote repository, run the command:

    git status

    and check if it returns the lines:

    Your branch is up to date with 'origin/master'.

    nothing to commit, working tree clean

Troubleshooting

Connection timeout

If you are having a connection timeout:

  1. Go to https://www.whatismyip.com/ and check what your computer's public IP address is. Let your teacher know (say this and type the IP address in the Zoom chat).
  2. Wait for your teacher to unblock your computer.

Optional tasks

Modify project settings

If necessary, open the src/main/resources/application.properties file and modify the settings, for example:

waitRatio = 2.0

Create an desktop icon

  1. Right-click on the desktop
  2. Select Create launcher...
  3. Enter:
    1. name: Eclispse,
    2. command: /home/student/IDEs/eclipse/eclipse
    3. If desired, select Icon: /home/student/IDEs/eclipse/icon.xpm
  4. Save the desktop icon

Use Key Exchange for SSH Connections

If you're tired of typing your password every time you press enter, you can enable SSH Key Exchange by following these instructions:

More information

Where Eclipse settings are stored

  • .project .classpath project files
  • .settings project folder
  • .metadata workspace folder
  • .eclipse Eclipse folder

Where IntelliJ IDEA settings are stored

  • name.iml file and project folder .idea
  • IDEA folder .config/JetBrains/Idea....

Other information sources

  1. ^ origin is the name of the remote repository, and master is the branch mapping (shortened from master:master) from the local to the remote repository
    The -u switch will set the local repository's master branch as the default for pushing to the remote repository, so you can then use simple git push and git pull

  

Created by Valdis Vītoliņš on 2025-01-28 15:28
Last modified by Valdis Vītoliņš on 2025-02-28 13:39
 
Xwiki Powered
Creative Commons Attribution 3.0 Unported License