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
๐ŸŒ
Princeton CS
introcs.cs.princeton.edu โ€บ java โ€บ 15inout โ€บ linux-cmd.html
Java and the Linux Command Line
From the shell, type the java command below. [wayne] ~/introcs/hello> java HelloWorld Hello, World If all goes well, you should see the output of the program - Hello, World. If your program gets stuck in an infinite loop, type Ctrl-c to break out. If you are entering input from the keyboard, you can signify to your program that there is no more data by typing Ctrl-d for EOF (end of file). You should type this character on its own line.
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.

๐ŸŒ
FOSS Linux
fosslinux.com โ€บ home โ€บ programming โ€บ compile and run java from the linux cli (2026 developer guide)
How to Compile and Run Java from Command-line in Linux
April 29, 2026 - fosslinux@dev:~$ sdk use java 11.0.22-tem Using java version 11.0.22-tem in this shell. fosslinux@dev:~$ java -version openjdk version "11.0.22" 2024-01-16 ยท When you close the terminal, your system reverts to your default version. This isolation prevents you from accidentally breaking system dependencies while maintaining full control over your development environment. By mastering these command line tools, you ensure your code behaves predictably regardless of the underlying operating system. ... Get the latest Linux guides and hardware reviews delivered to your inbox.
๐ŸŒ
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. ... The Javac command tool is not available in my system.
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ unix_commands โ€บ java.htm
java Command in Linux
The java command in Linux allows us to run a compiled Java application. When we execute this command, it initiates the Java Virtual Machine (JVM) in the background, loads the specified class, and invokes the main() method of that class.
Find elsewhere
๐ŸŒ
CommandLinux
commandlinux.com โ€บ home โ€บ man page โ€บ java
java
March 5, 2026 - Reports information about use of native methods and other Java Native Interface activity. ... Displays version information and exits. See also the -showversion option. ... Specifies that the version specified by the release is required by the class or JAR file specified on the command line.
๐ŸŒ
LinuxVox
linuxvox.com โ€บ blog โ€บ install-java-linux-command-line
Install Java on Linux via Command Line โ€” linuxvox.com
The two most common package managers are: ... # Update the package list sudo apt update # Install OpenJDK 11 sudo apt install openjdk - 11 - jdk # If you want a different version, for example, OpenJDK 8 sudo apt install openjdk - 8 - jdk ยท ...
๐ŸŒ
It's FOSS
itsfoss.com โ€บ run-java-program-ubuntu
How to Run Java Programs in Ubuntu
January 11, 2023 - You do not need to specify the class extension here. Just the name of the class. And this time, you use the command java, not javac. ... This will print Hello World on the screen for my program. ... And thatโ€™s how you run a Java program in the Linux terminal.
๐ŸŒ
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 about the Java version, user home directory, and any error message that occurs while running an applet or application. You can enable the Java Console for the Linux and Solaris platforms through the Java Control Panel
๐ŸŒ
VITUX
vitux.com โ€บ your-first-java-program-in-the-ubuntu-terminal
Your First Java Program in the Ubuntu Terminal โ€“ VITUX
If you are new to Java Programming ... need for this purpose include the Java Runtime Environment and the Java Development Kit. This article covers the installation of these two through the Ubuntu command line....
๐ŸŒ
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 Java program must be compiled using the Java Compiler Command (javac) before being executed through the terminal. Letโ€™s go over the step-by-step procedure. Linux cannot compile or execute Java programs without the Java Development Kit. Therefore, the first step is to ensure the JDK has been installed on your machine.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-execute-and-run-java-code
How to Execute and Run Java Code from the Terminal
March 10, 2022 - Finally, I want to create the class file of my Java code inside the Source folder. The directory tree looks like this: myJavaProgram > Source. For compiling this type of Java code with the packages, we use the command javac -d .
๐ŸŒ
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.
๐ŸŒ
Oracle
docs.oracle.com โ€บ en โ€บ java โ€บ javase โ€บ 19 โ€บ docs โ€บ specs โ€บ man โ€บ java.html
The java Command
December 12, 2022 - As a result, you may not realize any benefits from using compressed pointers with large Java heap sizes. ... Sets a custom command or a series of semicolon-separated commands to run when an irrecoverable error occurs. If the string contains spaces, then it must be enclosed in quotation marks. Linux and macOS: The following example shows how the -XX:OnError option can be used to run the gcore command to create a core image, and start the gdb debugger to attach to the process in case of an irrecoverable error (the %p designates the current process identifier):