How to run Java program on a server
We aim to execute a Java Program for simulation using SSJ on server. It will be particularly helpful if the simulation takes a long time to run. This program needs two external libraries ssj.jar and optimization-1.1.jar. We do this job in the following 3 steps.
- Step 1. Copy files from local to server
- Step 2. Execute Java Program on server
- Setp 3. Copy output files from server to local
Copy files from Local to Server
Using scp
command.
Type the following commands line by line in terminal to copy files “SimQueue.java”, “ssj.jar” and “optimization-1.1.jar” from local to server.
Instead of copying files one by one, you can copy a folder.
For example, the following command copies folder “Example”, together with all the files under this folder, to the server.
Execute Java Program on server
Using javac
command first to compile Java source code into Java bytecodes.
-cp
is a commonly used option. If the compiler needs to refer to your own classes, then you need to specify their location with this -cp
option.
Using java
command to interprete the Java bytecodes.
For example, the following commands execute the “SimQueue” java program.
Note that if you log in the server via ssh and start a process, the process will get killed once you abort the remote connection. To leave job running in background you can use nohup
command.
For example, we can execute the “SimQueue” java program as follows.
Copy files from Server to Local
Similar to the part Copy files from Local to Server.
For example, the following command copies file “output.txt” to local.