DevOpsJava

Writing a script to run a Java program

By June 6, 2023 4 Comments
  1. Open a text editor and create a new file.
  2. Copy and paste the below script into the file.

#!/bin/bash # Set the Java home directory JAVA_HOME=/path/to/java/home # Set the classpath and main class name CLASSPATH=/path/to/your/application.jar MAIN_CLASS=com.example.YourMainClass # Check if the program is already running if pgrep -f "$MAIN_CLASS" >/dev/null; then echo "Stopping the running instance of the program..." pkill -f "$MAIN_CLASS" sleep 5 # Wait for the program to stop gracefully fi # Run the Java program echo "Starting the program..." $JAVA_HOME/bin/java -cp $CLASSPATH $MAIN_CLASS
  1. Replace /path/to/java/home with the actual path to your Java installation directory.
  2. Replace /path/to/your/application.jar with the actual path to your compiled Java application’s JAR file.
  3. Replace com.example.YourMainClass with the fully qualified name of your application’s main class.
  4. Save the file with a “.sh” extension, such as “run.sh”.
  5. Open a terminal and navigate to the directory where you saved the script.
  6. Make the script executable by running the command: chmod +x run.sh
  7. Run the script by executing the command: ./run.sh

The script first checks if the program is already running using pgrep. If a running instance is found, it stops the program using pkill. After ensuring that the previous instance has stopped, it starts a new instance of the Java program using the specified Java home directory, classpath, and main class. Adjust the script as per your specific application’s requirements.

4 Comments

  • Tech says:

    #!/bin/bash

    # Set the Java home directory
    JAVA_HOME=/path/to/java/home

    # Set the classpath and main class name
    CLASSPATH=/path/to/your/application.jar
    MAIN_CLASS=com.example.YourMainClass

    # Check if the program is already running
    if pgrep -f “$MAIN_CLASS” >/dev/null; then
    echo “Stopping the running instance of the program…”
    pkill -f “$MAIN_CLASS”
    sleep 5 # Wait for the program to stop gracefully
    fi

    # Run the Java program
    echo “Starting the program…”
    $JAVA_HOME/bin/java -cp $CLASSPATH $MAIN_CLASS

  • test says:

    #!/bin/bash

    # Set the Java home directory
    JAVA_HOME=/path/to/java/home

    # Set the classpath and main class name
    CLASSPATH=/path/to/your/application.jar
    MAIN_CLASS=com.example.YourMainClass

    # Check if the program is already running
    if pgrep -f “$MAIN_CLASS” >/dev/null; then
    echo “Stopping the running instance of the program…”
    pkill -f “$MAIN_CLASS”
    sleep 5 # Wait for the program to stop gracefully
    fi

    # Run the Java program
    echo “Starting the program…”
    $JAVA_HOME/bin/java -cp $CLASSPATH $MAIN_CLASS

  • 234567 says:

    #!/bin/bash

    # Set the Java home directory
    JAVA_HOME=/path/to/java/home

    # Set the classpath and main class name
    CLASSPATH=/path/to/your/application.jar
    MAIN_CLASS=com.example.YourMainClass

    # Check if the program is already running
    if pgrep -f “$MAIN_CLASS” >/dev/null; then
    echo “Stopping the running instance of the program…”
    pkill -f “$MAIN_CLASS”
    sleep 5 # Wait for the program to stop gracefully
    fi

    # Run the Java program
    echo “Starting the program…”
    $JAVA_HOME/bin/java -cp $CLASSPATH $MAIN_CLASS

Leave a Reply