If your Main class is in a package called FileManagement, then try:

java -cp . FileManagement.Main

in the parent folder of the FileManagement folder.

If your Main class is not in a package (the default package) then cd to the FileManagement folder and try:

java -cp . Main

More info about the CLASSPATH and how the JRE find classes:

  • How Classes are Found
  • Setting the class path (Solaris/Linux)
  • http://en.wikipedia.org/wiki/Classpath_(Java)
Answer from Bart Kiers on Stack Overflow
๐ŸŒ
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.
๐ŸŒ
It's FOSS
itsfoss.com โ€บ run-java-program-ubuntu
How to Run Java Programs in Ubuntu
January 11, 2023 - And thatโ€™s how you run a Java program in the Linux terminal. This was the simplest of the example. The sample program had just one class. The Java compiler creates a class file for each class in your program.
๐ŸŒ
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:~$ javac -cp ".:external_libs/utilities.jar" Main.java ยท The -cp flag defines the classpath. The dot (.) represents the current directory, and the colon (:) separates multiple paths on Linux. Windows uses a semicolon, which is a common source of cross-platform confusion. When you execute the program, you must provide the exact same classpath to the JVM, otherwise it will throw a ClassNotFoundException at runtime.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ tutorial โ€บ getStarted โ€บ cupojava โ€บ unix.html
"Hello World!" for Solaris OS, Linux, and Mac OS X (The Javaโ„ข Tutorials > Getting Started > The "Hello World!" Application)
Results of the ls command, showing the .java source file. Now are ready to compile the source file. At the prompt, type the following command and press Return. ... The compiler has generated a bytecode file, HelloWorldApp.class. At the prompt, type ls to see the new file that was generated: the following figure. Results of the ls command, showing the generated .class file. Now that you have a .class file, you can run your program.
๐ŸŒ
VITUX
vitux.com โ€บ your-first-java-program-in-the-ubuntu-terminal
Your First Java Program in the Ubuntu Terminal โ€“ VITUX
In this article, you have learned to install both the Java Runtime Environment and the Java Development Kit used to compile and run your java programs. We also wrote a simple Hello World program in Java and ran to see if you and your system are ready to move to the more complex world of Java Programming. Vitux.com is a Linux compendium with lots of unique and up to date tutorials.
๐ŸŒ
Princeton CS
introcs.cs.princeton.edu โ€บ java โ€บ linux
Hello World in Java on Linux
August 14, 2019 - To make our textbook libraries accessible to Java, use the command java-introcs instead. For example, type the following two commands to test standard drawing and standard audio: You can use Checkstyle and Findbugs to check the style of your programs and identify common bugs. To run Checkstyle, type the following command in the Terminal: Here is a list of available checks. To run Findbugs, type the following command in the Terminal: Here is a list of bug descriptions. My distribution of Linux is { Gentoo, Debian, Ubuntu, Fedora, Red Hat, SuSE, Mandriva, or Slackware }. How should I modify the instructions?
๐ŸŒ
Princeton CS
introcs.cs.princeton.edu โ€บ java โ€บ 15inout โ€บ linux-cmd.html
Java and the Linux Command Line
This document instructs you on how to use the Linux shell with Java. For a light-hearted essasy on the command line, you might enjoy Neal Stephenson's light-hearted essay In the Beginning was the Command Line. You will use the Java compiler javac to compile your Java programs and the Java interpreter java to run ...
Find elsewhere
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-execute-and-run-java-code
How to Execute and Run Java Code from the Terminal
March 10, 2022 - To compile a Java code/program, we get the class file. Then we need to execute/run the class file. We need to use the command javac file_name_with_the_extension. For example, as I want to compile my Main.java, I will use the command javac Main.java.
๐ŸŒ
Dracula Servers
draculaservers.com โ€บ home โ€บ how to run java programs in linux terminal?
How to Run Java Programs in Linux Terminal? - Dracula Servers Tutorials
June 30, 2024 - However, the steps are the same: Update the Linux and run the respective installation command. To install JDK on Ubuntu, run the following commands. sudo apt-get update sudo apt-get install default-jdk ยท To install JDK on Fedora, run the following commands. sudo dnf update sudo dnf install java-11-openjdk-devel
๐ŸŒ
JavaPointers
javapointers.com โ€บ java โ€บ java-core โ€บ how-to-run-a-command-using-java-in-linux-or-windows
How To Run a Command using Java in Linux or Windows - JavaPointers
May 24, 2020 - public static void main(String... args) { Main main = new Main(); //test command in linux main.runCommand("pwd"); main.runCommand("ifconfig", "-a"); } Here, we try to print the current working directory and the network configurations available.
๐ŸŒ
UW Computer Sciences
pages.cs.wisc.edu โ€บ ~deppeler โ€บ cs400 โ€บ readings โ€บ Linux โ€บ command_line.html
Run a Java program from the command line
cd to your program project's folder if it is already written (linux command is: cd ~/private/cs400/workspace/project/ ) If you must edit your program source code, try one of these editors ... vi (or vim - my personal favorite) vi MyProgram.java You will need to learn about modes to use vi - here's a tutorial to help you get started. Or, google vi tutorial to find many more. Java programs must be compiled and linked before they can be run.
๐ŸŒ
TecAdmin
tecadmin.net โ€บ run-a-java-program-command-line
How to Run Java Program in Command Line - TecAdmin
April 26, 2025 - Now compile the HelloWorld.java file using java compiler. This will create a HelloWorld.class file in current directory. ... Finally, run your java program which โ€˜javaโ€™ command followed by your class name.
๐ŸŒ
LinuxConfig
linuxconfig.org โ€บ home โ€บ java hello world example on linux
Run Java Hello World on Linux: A Beginner's Guide
June 3, 2020 - Learn to write and run a Java Hello World on Linux. Step-by-step guide from checking Java to executing your first program. Ideal for beginners.
Top answer
1 of 10
64

You can use java.lang.Runtime.exec to run simple code. This gives you back a Process and you can read its standard output directly without having to temporarily store the output on disk.

For example, here's a complete program that will showcase how to do it:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class testprog {
    public static void main(String args[]) {
        String s;
        Process p;
        try {
            p = Runtime.getRuntime().exec("ls -aF");
            BufferedReader br = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
            while ((s = br.readLine()) != null)
                System.out.println("line: " + s);
            p.waitFor();
            System.out.println ("exit: " + p.exitValue());
            p.destroy();
        } catch (Exception e) {}
    }
}

When compiled and run, it outputs:

line: ./
line: ../
line: .classpath*
line: .project*
line: bin/
line: src/
exit: 0

as expected.

You can also get the error stream for the process standard error, and output stream for the process standard input, confusingly enough. In this context, the input and output are reversed since it's input from the process to this one (i.e., the standard output of the process).

If you want to merge the process standard output and error from Java (as opposed to using 2>&1 in the actual command), you should look into ProcessBuilder.

2 of 10
27

You can also write a shell script file and invoke that file from the java code. as shown below

{
   Process proc = Runtime.getRuntime().exec("./your_script.sh");                        
   proc.waitFor();
}

Write the linux commands in the script file, once the execution is over you can read the diff file in Java.

The advantage with this approach is you can change the commands with out changing java code.

๐ŸŒ
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 - These can be JVM parameters as well as any other application-specific parameters, for example, config or data file locations. That may result in writing a wrapper shell script where we can set up all the required parameters before starting the JVM. Letโ€™s imagine we already have a wrapper script and now just want to turn it into a system service: #!/bin/bash JAVA_HOME=/path/to/jvmdir WORKDIR=/path/to/app/workdir JAVA_OPTIONS=" -Xms256m -Xmx512m -server " APP_OPTIONS=" -c /path/to/app.config -d /path/to/datadir " cd $WORKDIR "${JAVA_HOME}/bin/java" $JAVA_OPTIONS -jar javaapp.jar $APP_OPTIONS
๐ŸŒ
Oracle
java.com โ€บ en โ€บ download โ€บ help โ€บ enable_console_linux.html
How do I enable and view the Java Console for Linux or Solaris?
The Java Console provides information ... while running an applet or application. You can enable the Java Console for the Linux and Solaris platforms through the Java Control Panel ยท Open a Terminal window. Go to the Java installation directory. In this example Java is installed ...
๐ŸŒ
Opensource.com
opensource.com โ€บ article โ€บ 19 โ€บ 11 โ€บ install-java-linux
How to install Java on Linux | Opensource.com
Some distributions use the keyword latest to indicate the most recent version, which is usually what you need to run whatever application you're trying to run. Depending on what package manager you use, you might even consider using grep to filter the search results to include only the latest versions. For example, on Fedora: $ sudo dnf search openjdk | \ grep latest | cut -f1 -d':' java-latest-openjdk-demo.x86_64 java-openjdk.i686 java-openjdk.x86_64 java-latest-openjdk-jmods.x86_64 java-latest-openjdk-src.x86_64 java-latest-openjdk.x86_64 [...]