Updated 2021-08-15

Run Java on the Cluster

Overview

  • This guide will focus on running java normally. Mpj (mpj express) is available on the cluster if you wish to run Java in parallel.

Tips

  • In the PBS script, load the java development kit with module load jdk/1.8.0_202, you can use module avail jdk to see what versions of java are available.
  • Make sure to compile and run java as you normally would. Include the compile and run steps after the #PBS directives in the PBS script.
  • As always, make sure your java program and any files needed are all in the same folder.

Walkthrough: Run Java on the Cluster

  • This walkthrough will use a simple Java program that prints "Hello World"
  • In order to create/run the program we will need both a Java program file, as well as a PBS script to submit our job - both can be found below.
  • You can transfer the files to your account on the cluster if necessary to follow along. The file transfer guide may be helpful.

Part 1: The Java Program

helloWorld.java

public class helloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World");
   }
}

The PBS script below assumes your file is saved as helloWorld.java, if you choose a different file name you will need to update the commands accordingly.

Part 2: The PBS Script

helloWorldTest.PBS

#PBS -N helloWorldJava
#PBS -A [Account] 
#PBS -l nodes=1:ppn=2
#PBS -l pmem=2gb
#PBS -l walltime=1:00
#PBS -q inferno
#PBS -j oe
#PBS -o helloWorldJava.out

cd $PBS_O_WORKDIR
module load jdk/1.8.0_202
javac helloWorld.java
java -cp $PBS_O_WORKDIR helloWorld
  • Note the classpath MUST be specified to point to the absolute path of the compiled class file.
  • The #PBS directives are standard, requesting just 1 minute of walltime and 1 node with 2 cores. More on #PBS directives can be found in the PBS guide
  • $PBS_O_WORKDIR is simply a variable that represents the directory you submit the PBS script from. Make sure the java script you want to run (in this case, helloWorld.java) is in the same directory you put the PBS script.
  • Output Files will also show up in this dir as well (in this case, the helloWorld.class file)
  • module load jdk/1.8.0_202 loads the 1.8.0 version of the Java JDK. To see what Java JDK versions are available, run module avail jdk, and load the one you want
  • javac helloWorld.java is the line that compiles the java file, so it can be run
  • java helloWorld runs the program

Part 3: Submit Job and Check Status

  • Make sure you're in the dir that contains the PBS Script as well as the java program
  • Submit as normal, with qsub <pbs script name>. In this case qsub helloWorldTest.pbs
  • Check job status with qstat -t <job_id>, where the job id is the number returned after running qsub
  • You can delete the job with qdel <job_id> , again where the jobid is the number returned after running qsub

Part 4: Collecting Results

  • In the directory where you submitted the PBS script, you should see a helloWorldJava.out file, which contains the results of the job. Use cat helloWorldJava.out or open the file in a text editor to take a look.
  • helloWorldJava.out should look like this:
Job name:   helloWorldJava
Queue:      inferno
End PBS Prologue Tue Sep  4 16:33:42 EDT 2018
---------------------------------------
Hello World
---------------------------------------
Begin PBS Epilogue Tue Sep  4 16:33:44 EDT 2018
Job ID:     22254724.shared-sched.pace.gatech.edu
  • After the result files are produced, you can move the files off the cluster, refer to the file transfer guide for help.
  • Congratulations! You successfully ran a Java program on the cluster.