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.
$ scp <SourceFile> <user@host>:<directory>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.
$ scp ~/Desktop/SimQueue.java eshan@maxwell.ielm.ust.hk:~/Simulation/
$ scp ~/Desktop/ssj.jar eshan@maxwell.ielm.ust.hk:~/Simulation/
$ scp ~/Desktop/optimization-1.1.jar eshan@maxwell.ielm.ust.hk:~/Simulation/Instead of copying files one by one, you can copy a folder.
$ scp -r <SourceFolder> <user@host>:<directory>For example, the following command copies folder “Example”, together with all the files under this folder, to the server.
$ scp -r ~/Desktop/Example eshan@maxwell.ielm.ust.hk:Execute Java Program on server
Using javac command first to compile Java source code into Java bytecodes.
javac <options> <filename>.java-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.
java <options> <filename>For example, the following commands execute the “SimQueue” java program.
javac -cp optimization-1.1.jar:ssj.jar:. SimQueue.java
java -cp optimization-1.1.jar:ssj.jar:. SimQueueNote 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.
nohup <command-name> &For example, we can execute the “SimQueue” java program as follows.
javac -cp optimization-1.1.jar:ssj.jar:. SimQueue.java
nohup java -cp optimization-1.1.jar:ssj.jar:. SimQueue &Copy files from Server to Local
Similar to the part Copy files from Local to Server.
$ scp <user@host>:<SourceFile> <local directory>$ scp -r <user@host>:<SourceFolder> <local directory>For example, the following command copies file “output.txt” to local.
$ scp eshan@maxwell.ielm.ust.hk:~/Simulation/output.txt ~/Desktop/