Part 1 - Java Client/Server Programming
In this tutorial, we assume that the Java Development Kit (JDK) is already installed
on your machine.
If this is not the case, you need to
download the JDK
(we recommend Adoptium OpenJDK) and install it. This should not be necessary on the university's PCs.
First of all, check the location and the version of the JDK:
- on Windows, the OpenJDK version would be usually installed in this folder: C:\Program Files\\jdk-.0..
- on Linux, you can find the location of the JDK by typing locate javac at the shell prompt
- on MacOs: in general, commands for Linux should work, but please double check yourself
If the path or version are different you will need to is set the JAVA_HOME
variable accordingly. All these examples and tasks should work, as
long as you are using Java .
In the following examples, we assume that
JDK is installed in C:\Program Files\\jdk-.0..
TASK 0 - Configure and test the Java development environment
- create a folder for this tutorial (for example , under your home drive on Windows, or home directory on Linux)
- on Windows
- open the command line interface (terminal) (start-> search cmd)
- change drive () and change directory (cd AnBx). You may also display the content of the folder (dir)
- on Linux
- open a Terminal
- change directory (cd AnBx)
- on Windows
- to create you first Java program, copy the code below in your favorite
editor and save it in the file HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
- set JAVA_HOME and PATH variables. In university labs, this might not be necessary. In that case, skip this
step.
On Windows, you can used the command echo %JAVA_HOME% to check if the variable is already set.
You can also check if the Java binaries are in the path by typing javac --version. There response should be the version of the current JDK installed.
If you get a javac is not recognized message then is likely the the path is not set correctly.
It is recommended to use environmental variables, so you do not need to set variable for every session. Please consult this tutorial if you are using Windows.
Alternatively, variables can be set for the current session only, by using this commands:
C:\Program Files\\jdk-.0..
set PATH=%PATH%;%JAVA_HOME%\bin
On Linux, execute echo $JAVA_HOME to check if the variable is already set.
You need to check on your system where the JDK is located. Try locate javac and identify the line (it should look like /usr/java/jdk.0_.)
vi ~/.bashrc or vi ~/.bash_profile
add these lines
export JAVA_HOME=/usr/java/jdk.0_.
export PATH=$JAVA_HOME/bin:$PATH
save the file, and load it again
source ~/.bashrc or
source ~/.bash_profile
On Linux the variable
should now be set for your profile not just for the current session
- compile your program
javac -cp . HelloWorld.java - run the program
java HelloWorld
- if you are unable to compile, there is probably an issue with the
environment
variables. Check their current value with
- on Windows: echo %JAVA_HOME% and echo %PATH%
- on Linux: echo $JAVA_HOME and echo $PATH
- N.B. if you close the terminal, you may need to set the environment variables again (unless set at system level)!
- More information about running your first Java program:
TASK 1 - Client/Server
- download the AnBxJ library in your folder
- download the first tutorial Tutorial_01.java
- study the code and try to understand how the program should work
- compile and run the program
//COMPILE (MAKE)
javac
-cp AnBxJ.jar Tutorial_01.java
// {Tutorial,client,server}_01.class files are
generated
On Windows:
//RUN SERVER (start opens
a new terminal)
start java -cp .;AnBxJ.jar Tutorial_01 server
//RUN
CLIENT
java -cp .;AnBxJ.jar Tutorial_01 client
On Linux or Mac:
Open another terminal and run the server
//RUN SERVER
java -cp .:AnBxJ.jar Tutorial_01 server
On the
previous terminal run the client
//RUN
CLIENT
java -cp .:AnBxJ.jar Tutorial_01 client
Note that on Linux and Mac you need to replace ; with : in the rest of the tutorial
TASK 2 - Client/Server
- modify Tutorial_01.java as follows (compile and run it after each step)
- add some lines of code to allow the client to ask and greet the server
- the server should run on port 77XX (where XX are the last two digits of your machine, each PC in the lab as an ID 01,02,03...)
- collaborate with another student, try to run the client and the server on
two different machines;
the client needs to know the server IP address of the server: use the ipconfig command on Windows, ifconfig on Linux.
N.B: it is unlikely to work in the university lab (security policy). Try with laptops or at home!
However, you might also try with one laptop and one lab's computer. - swap your roles (if you run the client, now run the server and viceversa)
TASK 3 - Key generation (optional)
- using the keytool, create a RSA (2048 bits) keypair for alice (keystore type: PKCS12)
- export alice's public key
- create a keypair for bob
- export bob's public key
- import bob's public key in alice's public keystore
- imports alice's public key in alice's public keytore
- keytool documentation
- hint: use command (batch/bash) files (.cmd/.sh)
- N.B: remember the keystore passwords!