🌐
Linux Hint
linuxhint.com › run-java-command-line-linux
How to Run Java from Command-line in Linux – Linux Hint
To run the java program in Linux, we need to verify if Java Development Kit (JDK) is available in the system and its version.
🌐
It's FOSS
itsfoss.com › run-java-program-ubuntu
How to Run Java Programs in Ubuntu
January 11, 2023 - Installing JDK that also contains the Java compiler · The above command should work for other Debian and Ubuntu based distributions like Linux Mint, elementary OS etc. For other distributions, use your distribution’s package manager.
🌐
DZone
dzone.com › software design and architecture › cloud architecture › how to make it easy and simple to start java processes in linux/docker
How to Make It Easy and Simple to Start Java Processes in Linux/Docker
July 5, 2018 - In short, let's look at the example of the Nexus 3 mentioned above, how to return from the labyrinth of shell scripts to something more similar to java -jar <program.jar>, given the convenient modern DevOps-tools. In a nutshell, in ancient times, when UNIX was not interrogated at the mention of "in the sense of Linux?", there were no Systemd and Docker, etc. To manage the processes, we used portable shell scripts (init scripts) and PID-files. Init scripts set the necessary environment settings, which in different UNIX-s were their own, and, depending on the arguments, they started the process or restarted/stopped it using the ID from the PID-file.
🌐
Linux Journal
linuxjournal.com › article › 6290
Getting Started with Java on Linux | Linux Journal
August 26, 2002 - Write once, run everywhere: that's the slogan the Java community uses to propagate their language-of-choice. It's probably true, but only if you first manage to set up the beast on your box. This article gets you started with Java on Linux by showing you how to get the Java Compiler and Virtual Machine installed so you can run core Java programs.
🌐
LinuxForDevices
linuxfordevices.com › home › how to run a command-line java program on linux?
How to Run a Command-Line Java Program on Linux? - LinuxForDevices
July 19, 2021 - In this tutorial, we will learn about how to run a command-line Java program from the Linux terminal.
🌐
DEV Community
dev.to › mtendekuyokwa19 › setting-up-for-java-on-linux-133f
Setting Up for Java on Linux. - DEV Community
February 14, 2024 - Firstly, congratulations for using a great open source software,Linux. To get started I will have to explain the two main components you need to have Java up and running on your machine. You will need the Java development kit (JDK) and an IDE.
🌐
The Art of Code
artofcode.wordpress.com › 2025 › 01 › 14 › running-java-application-as-a-linux-service
Running Java application as a Linux service | The Art of Code
April 1, 2025 - This post is a continuation of my previous one, "Turning a laptop into a Java app server in 2025". In this part we're going to setup a Java application as a service in Linux. We will use Systemd for this so a service will be automatically started on system boot and it will be run…
Find elsewhere
🌐
Princeton CS
introcs.cs.princeton.edu › java › linux
Hello World in Java on Linux
August 14, 2019 - This document instructs you on how to setup a Java programming environment under Linux. It also provides a step-by-step guide for creating, compiling, and executing your first Java program using either DrJava or the command line. We assume some familiarity with the command line.
🌐
Baeldung
baeldung.com › home › administration › service management › run a java application as a service on linux
Run a Java Application as a Service on Linux | Baeldung on Linux
March 18, 2024 - No ads, dark-mode and 6 months ... to start with. Any Java application from the system point-of-view is just an instance of the Java Virtual Machine. In this short tutorial, we’ll see how we can make our applications run as system services. We’ll use the facilities of the systemd software package. systemd is the initialization and service management system in most modern Linux ...
🌐
Oracle
java.com › en › download › help › linux_x64_install.html
Linux 64-bit installation instructions for Java
Unpack the tarball and install Java tar zxvf jre-8u73-linux-x64.tar.gz The Java files are installed in a directory called jre1.8.0_73 in the current directory. In this example, it is installed in the /usr/java/jre1.8.0_73 directory.
🌐
FOSS Linux
fosslinux.com › home › programming › compile and run java from the linux cli (2026 developer guide)
Compile and Run Java from the Linux CLI (2026 Developer Guide)
April 29, 2026 - fosslinux@dev:~$ java Main Executing Java directly from the Linux CLI. Java version: 21.0.2 · Starting with Java 11, the JDK introduced JEP 330.
🌐
Princeton CS
introcs.cs.princeton.edu › java › 15inout › linux-cmd.html
Java and the Linux Command Line
August 14, 2019 - You will use the Java compiler javac to compile your Java programs and the Java interpreter java to run them. We'll assume you have already installed these. You will type commands in an application known as the shell. Since you're using Linux, we assume you're somewhat familiar with it.
🌐
Opensource.com
opensource.com › article › 19 › 11 › install-java-linux
How to install Java on Linux | Opensource.com
$ tar --extract --file openjdk*linux-x64_bin.tar.gz \ --directory=$HOME/bin · Java is now installed.
🌐
GeeksforGeeks
geeksforgeeks.org › linux-unix › download-install-java-windows-linux-macos
Installing Java on Windows, Linux, and macOS - GeeksforGeeks
April 20, 2026 - Installing Java is the first step to start Java development on any system. It allows you to run Java applications and build projects across different operating systems. Java can be installed on Windows, Linux, and macOS using platform-specific steps.
🌐
Apryse
fluent.apryse.com › running on linux
Running Java Engine on Linux | Fluent Docs - Apryse
This article explains how to install and use the Fluent Java Engine in Linux. This tutorial specifically uses Ubuntu Server 18.04 LTS, but the steps should work on any Linux system with little extra effort.
Top answer
1 of 16
244

I wrote another simple wrapper here:

#!/bin/sh
SERVICE_NAME=MyService
PATH_TO_JAR=/usr/local/MyProject/MyJar.jar
PID_PATH_NAME=/tmp/MyService-pid
case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
            echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stoping ..."
            kill $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill $PID;
            echo "$SERVICE_NAME stopped ...";
            rm $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
            echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
esac 

You can follow a full tutorial for init.d here and for systemd (ubuntu 16+) here

If you need the output log replace the 2

nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &

lines for

nohup java -jar $PATH_TO_JAR >> myService.out 2>&1&
2 of 16
52

A simple solution is to create a script start.sh that runs Java through nohup and then stores the PID to a file:

nohup java -jar myapplication.jar > log.txt 2> errors.txt < /dev/null &
PID=$!
echo $PID > pid.txt

Then your stop script stop.sh would read the PID from the file and kill the application:

PID=$(cat pid.txt)
kill $PID

Of course I've left out some details, like checking whether the process exists and removing pid.txt if you're done.