2. Java project setup
Review Introduction to Java
Tasks
- Clone the project
- Rename the project
- Rebuild the project
- Execute TODO
- Learn to compile and execute classes
- Run a unit test
- Create a Git repository on the server
- Commit and push changes
- Update your local repository and push your changes to the remote repository
- Troubleshooting
- Optional tasks
- More information
Clone the project
- Open terminal
- Change current folder to the IDE's project folder
Clone project with command:
git clone https://odo.lv/git/JTM/- Open cloned project in IDE (e.g. Eclipse or IntelliJ IDEA)
- In project options set it as Maven project
Rename the project
- Use your student ID in the project name, for example aa00000.
- Right-click on JTM and select Refactor—Rename and rename the project in the following format: aa00000
- Open your project folder, for example /home/student/workspace/aa00000.
- Open the file pom.xml and change the line <artifactId>JTM</artifactId> to: <artifactId>aa00000</artifactId>
Rebuild the project
Go to your project and recompile it:
cd ~/workspace/aa00000
mvn clean compileIf you use Eclipse, update setting files:
mvn eclipse:clean eclipse:eclipseIn Eclipse select the project in Package Explorer and press F5
Checkpoints
Check that the Eclipse project name is e.g. aa00000
Check if your project is in a folder, e.g. /student/workspace/aa00000/
Check if the file /home/student/workspace/aa00000/.project contains the line: <name>aa00000</name>
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
- Select the menu Window — Open Perspective — Java
- Right-click the Resource button and select Close
- Use code generation to create a class Hello (Hello.java file) with a method public static void main(String[] args).
- In the Project Explorer, select the package: jtm.activity02
- Right-click package and select: New — Class
- In the Name field, enter: Hello
- In the Which method stubs would you like to create? group, select public static void main(String[] args)
- Click Finish
Add the following code to the method to print the greeting in different languages:
System.out.println("Hello! Sveicināti! 你好!");
- Run HelloTest1.java as a JUnit test to verify the correct implementation of this class.
- Right-click HelloTest1.java.
- Select Run as — JUnit test
- Verify that a green status bar is visible in the JUnit view
- To double-check the system settings, also run ConfigTests.java in the Junit package jtm.activity01.
- 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
- Select a class and activate the Run — Run as... — Java application menu
On the command line
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
- Select the Run— Run as— Java Application menu
- Select the Run— Run as— JUnit Test menu
- Verify that the unit tests passed successfully
Create a Git repository on the server
Create an SSH connection to cserver
ssh aa00000@cserverwhere aa00000 is your StudentId
(and bcerver is the short name created in the file /etc/hosts for tools.odo.lv)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
Enter your original user password:
aa00000@cserver's password:type your password and press enter
Change your user password by entering the command cserver:
passwdand enter the requested information:
Current password:
New password:
Retype new password:original password set by the teacher
your new password
your new passwordCreate a git remote repository, and log out of the server:
mkdir aa00000
cd aa00000
git --bare init
exit- Rename project source repository:git remote rename origin source
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/aa00000or 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
...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
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:
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
- Use git status to see what has changed since the last commitm committed changes
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 pushIf 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 masterTo ensure that all of your local repository's history is pushed to your remote repository, run the command:
git statusand 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:
- 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).
- 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:
Create an desktop icon
- Right-click on the desktop
- Select Create launcher...
- Enter:
- name: Eclispse,
- command: /home/student/IDEs/eclipse/eclipse
- If desired, select Icon: /home/student/IDEs/eclipse/icon.xpm
- 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
- ^ 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.