One way to run a process from a different directory to the working directory of your Java program is to change directory and then run the process in the same command line. You can do this by getting cmd.exe to run a command line such as cd some_directory && some_program.

The following example changes to a different directory and runs dir from there. Admittedly, I could just dir that directory without needing to cd to it, but this is only an example:

Copyimport java.io.*;

public class CmdTest {
    public static void main(String[] args) throws Exception {
        ProcessBuilder builder = new ProcessBuilder(
            "cmd.exe", "/c", "cd \"C:\\Program Files\\Microsoft SQL Server\" && dir");
        builder.redirectErrorStream(true);
        Process p = builder.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while (true) {
            line = r.readLine();
            if (line == null) { break; }
            System.out.println(line);
        }
    }
}

Note also that I'm using a ProcessBuilder to run the command. Amongst other things, this allows me to redirect the process's standard error into its standard output, by calling redirectErrorStream(true). Doing so gives me only one stream to read from.

This gives me the following output on my machine:

C:\Users\Luke\StackOverflow>java CmdTest
 Volume in drive C is Windows7
 Volume Serial Number is D8F0-C934

 Directory of C:\Program Files\Microsoft SQL Server

29/07/2011  11:03    <DIR>          .
29/07/2011  11:03    <DIR>          ..
21/01/2011  20:37    <DIR>          100
21/01/2011  20:35    <DIR>          80
21/01/2011  20:35    <DIR>          90
21/01/2011  20:39    <DIR>          MSSQL10_50.SQLEXPRESS
               0 File(s)              0 bytes
               6 Dir(s)  209,496,424,448 bytes free
Answer from Luke Woodward on Stack Overflow
🌐
IONOS
ionos.com › digital guide › websites › web development › java commands
The most important Java commands in an overview - IONOS
September 27, 2022 - Get an overview of the most important Java commands, which could help you program faster! This article lists the most used Java commands.
🌐
CodeJava
codejava.net › java-core › tools › examples-of-using-java-command
java command examples
August 4, 2019 - It must be multiple of 1024 and is greater than 1MB for initial size and 2MB for maximum size. Append k or K to indicate kilobytes; m or M to indicate megabytes. For example, the following command launches a program with initial heap size 32MB and maximum heap size 1024MB: java -Xms32M -Xmx1024M MyProgram Other Java Tools Tutorials:
Top answer
1 of 15
166

One way to run a process from a different directory to the working directory of your Java program is to change directory and then run the process in the same command line. You can do this by getting cmd.exe to run a command line such as cd some_directory && some_program.

The following example changes to a different directory and runs dir from there. Admittedly, I could just dir that directory without needing to cd to it, but this is only an example:

Copyimport java.io.*;

public class CmdTest {
    public static void main(String[] args) throws Exception {
        ProcessBuilder builder = new ProcessBuilder(
            "cmd.exe", "/c", "cd \"C:\\Program Files\\Microsoft SQL Server\" && dir");
        builder.redirectErrorStream(true);
        Process p = builder.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while (true) {
            line = r.readLine();
            if (line == null) { break; }
            System.out.println(line);
        }
    }
}

Note also that I'm using a ProcessBuilder to run the command. Amongst other things, this allows me to redirect the process's standard error into its standard output, by calling redirectErrorStream(true). Doing so gives me only one stream to read from.

This gives me the following output on my machine:

C:\Users\Luke\StackOverflow>java CmdTest
 Volume in drive C is Windows7
 Volume Serial Number is D8F0-C934

 Directory of C:\Program Files\Microsoft SQL Server

29/07/2011  11:03    <DIR>          .
29/07/2011  11:03    <DIR>          ..
21/01/2011  20:37    <DIR>          100
21/01/2011  20:35    <DIR>          80
21/01/2011  20:35    <DIR>          90
21/01/2011  20:39    <DIR>          MSSQL10_50.SQLEXPRESS
               0 File(s)              0 bytes
               6 Dir(s)  209,496,424,448 bytes free
2 of 15
19

You can try this:-

CopyProcess p = Runtime.getRuntime().exec(command);
🌐
Refactoring.Guru
refactoring.guru › home › design patterns › command › java
Command in Java / Design Patterns
January 1, 2026 - Command pattern in Java. Full code example in Java with detailed comments and explanation. Command is behavioral design pattern that converts requests or simple operations into objects.
🌐
GeeksforGeeks
geeksforgeeks.org › java › command-line-arguments-in-java
Command Line Arguments in Java - GeeksforGeeks
Command-line arguments in Java are values passed to a program during execution through the command prompt.
Published   May 28, 2026
🌐
Codecademy
codecademy.com › article › java-for-programmers-java-and-the-command-line
Java and the Command Line | Codecademy
As a developer though, you need the Java Development Kit, or JDK, which allows you access to the classes and interfaces of the core Java language that you use here on Codecademy such as System.out.println(). The current version of Java we have here at Codecademy can be found by typing the command java --version into a terminal when you are doing a lesson.
🌐
W3Schools
w3schools.com › java › java_getstarted.asp
Java Getting Started
Save the code in Notepad as "Main.java". Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and type "javac Main.java":
🌐
Christopher Siu
users.csc.calpoly.edu › ~gfisher › classes › 102 › info › command-line-java.html
Java Command-Line Essentials
Open a command shell. Change directory to the location of the compiled file(s). ... java classname where classname is the name of Java class containing the main method of the program to be executed.
Find elsewhere
🌐
Reddit
reddit.com › r/learnprogramming › best list of java commands/methods/symbols for beginner?
r/learnprogramming on Reddit: Best list of java commands/methods/symbols for beginner?
September 9, 2015 -

Just learning and am watching two different video tutorials on java. Is therre a list of commands/symbols that would allow me to play around when I am not following these courses/lessons? Only real "command" I know is the system.print one and a few others.

🌐
Baeldung
baeldung.com › home › java › core java › command-line arguments in java
Command-Line Arguments in Java | Baeldung
December 27, 2025 - The code above expects the file path as a command-line argument. If no file path is provided, it throws an exception and prints the usage instructions instead of attempting to read the file. Let’s place a file named hello.txt in our resources folder and pass its absolute path as an argument in our IDE run configuration: "/home/baeldung/tutorials/core-java-modules/core-java-lang/src/main/resources/hello.txt"
🌐
Minecraft Wiki
minecraft.wiki › w › Commands
Commands – Minecraft Wiki
For up to 10 macros, a command can be entered in a text input, which can be quickly executed in-game by pressing Alt + Key. Command macros can be preceded by a slash, but it is not required. This feature is not to be confused with Java Edition's function macros, where functions may reference ...
🌐
Princeton CS
introcs.cs.princeton.edu › java › 15inout › windows-cmd.html
Java and the Windows Command Prompt
Prepend C:\Program Files\Java\jdk1.6.0_27\bin; to the beginning of the PATH variable. Click OK three times. You will type commands in an application called the Command Prompt.
🌐
Baeldung
baeldung.com › home › java › the command pattern in java
The Command Pattern in Java | Baeldung
January 8, 2024 - In this article, we learned the command pattern’s key concepts and how to implement the pattern in Java by using an object-oriented approach and a combination of lambda expressions and method references.
🌐
Medium
medium.com › @AlexanderObregon › executing-java-code-from-the-command-line-d6a3c09bb565
Executing Java Code from the Command Line | Medium
March 9, 2025 - Learn how to compile and run Java programs from the command line using javac and java, manage classpaths, and understand how the JVM processes bytecode.
🌐
DigitalOcean
digitalocean.com › community › tutorials › command-line-arguments-in-java
Command Line Arguments in Java | DigitalOcean
August 3, 2022 - Command-line arguments in Java are used to pass arguments to the main program. If you look at the Java main method syntax, it accepts String array as an argument. When we pass command-line arguments, they are treated as strings and passed to the main function in the string array argument.
🌐
Stack Overflow
stackoverflow.com › questions › 22472743 › meaning-of-the-java-command-in-unix
linux - Meaning of the java command in unix - Stack Overflow
The Java Runtime Environment. You don't need the development tools. ... The exact meaning of the command is to launch Java. It tells Java the Class Path, which is where Java will look for classes to load.
Top answer
1 of 13
274

Source: javaindos.

Let's say your file is in C:\mywork\

Run Command Prompt

CopyC:\> cd \mywork

This makes C:\mywork the current directory.

CopyC:\mywork> dir

This displays the directory contents. You should see filenamehere.java among the files.

CopyC:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin

This tells the system where to find JDK programs.

CopyC:\mywork> javac filenamehere.java

This runs javac.exe, the compiler. You should see nothing but the next system prompt...

CopyC:\mywork> dir

javac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.

CopyC:\mywork> java filenamehere

This runs the Java interpreter. You should then see your program output.

If the system cannot find javac, check the set path command. If javac runs but you get errors, check your Java text. If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java HelloWorld command. Java is case-sensitive!

2 of 13
121

To complete the answer :

  1. The Java File

    CopyTheJavaFile.java
    
  2. Compile the Java File to a *.class file

    Copyjavac TheJavaFile.java
    
    • This will create a TheJavaFile.class file
  3. Execution of the Java File

    Copyjava TheJavaFile
    
  4. Creation of an executable *.jar file

    • You've got two options here -

      1. With an external manifest file :

        • Create the manifest file say - MANIFEST.mf

        • The MANIFEST file is nothing but an explicit entry of the Main Class

        • jar -cvfm TheJavaFile.jar MANIFEST.mf TheJavaFile.class

      2. Executable by Entry Point:

        • jar -cvfe TheJavaFile.jar <MainClass> TheJavaFile.class
  5. To run the Jar File

    Copyjava -jar TheJavaFile.jar
    
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › environment › cmdLineArgs.html
Command-Line Arguments (The Java™ Tutorials > Essential Java Classes > The Platform Environment)
The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run. For example, suppose a Java application called Sort sorts lines in a file.
🌐
Programiz
programiz.com › java-programming › command-line-arguments
Java Command-Line Arguments
The String array stores all the arguments passed through the command line. Note: Arguments are always stored as strings and always separated by white-space. The main() method of every Java program only accepts string arguments.