๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_getstarted.asp
Java Getting Started
Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and type "javac Main.java": ... This will compile your code. If there are no errors in the code, the command prompt will take you to the next line.
๐ŸŒ
W3Schools Blog
w3schools.blog โ€บ home
Command line arguments in java - W3schools.blog
December 27, 2024 - Free Online Tutorials, W3schools provides tutorials and interview questions of all technology like java, android, physics, chemistry, math, english, javascript, ajax, core java, sql, python, php, c language etc.
๐ŸŒ
W3Schools
w3schools.in โ€บ java โ€บ compile-java-file-using-javac
How to Compile a Java File Using javac - W3Schools
You can directly run javac in command prompt if the environment variable is set correctly.
๐ŸŒ
Princeton CS
introcs.cs.princeton.edu โ€บ java โ€บ 15inout โ€บ windows-cmd.html
Java and the Windows Command Prompt
This page is obsolete ยท This document instructs you on how to use the Windows Command Prompt with Java. These instructions are specialized to Windows 7, but are similar for Windows XP and Windows Vista
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_syntax.asp
Java Syntax
Finally, remember that each Java statement must end with a semicolon (;). ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
๐ŸŒ
W3Schools
w3schools.in โ€บ java-tutorial โ€บ compile-java-file-using-javac
How to Compile a Java File Using javac
You can directly run javac in command prompt if the environment variable is set correctly.
๐ŸŒ
Princeton CS
introcs.cs.princeton.edu โ€บ java โ€บ windows โ€บ command-prompt.php
Using Java from the Command Prompt in Windows
This document instructs you on how to use Java from the Command Prompt in Windows. The Command Prompt is necessary for redirecting standard input, redirecting standard output, and pipingโ€”you will use these features in Section 1.5 ยท These instructions apply to 32-bit and 64-bit Windows 8, ...
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
ByteScout
bytescout.com โ€บ blog โ€บ learn-java-commands.html
Learn TOP-25 Java Commands in 2023 โ€“ ByteScout
You can use rmic compiler command to seamlessly create stub and skeleton class files using the Java Remote Method Protocol9 (JRMP) and automatically stub and tie all class files (IIOP protocol) for the remote protocol.
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
    
๐ŸŒ
W3Schools
w3schools.com โ€บ java
Java Tutorial
Reference Overview Java Keywords String Methods Math Methods Output Methods Arrays Methods ArrayList Methods LinkedList Methods HashMap Methods Scanner Methods File Methods FileInputStream FileOutputStream BufferedReader BufferedWriter Iterator Methods Collections Methods System Methods Errors & Exceptions ... Complete the W3Schools coding course, strengthen your knowledge, and earn a certificate you can add to your CV, portfolio, and LinkedIn profile.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ command-line-arguments-in-java
Command Line Arguments in Java - GeeksforGeeks
Command-line arguments are passed after the class name while running the Java program.
Published ย  May 28, 2026
๐ŸŒ
Princeton CS
introcs.cs.princeton.edu โ€บ java โ€บ windows โ€บ manual.php
Hello World in Java on Windows (manual instructions)
This document instructs you on how to manually setup a Java programming environment for your Windows computer and provides a step-by-step guide for creating, compiling, and executing a Java program using either DrJava or the Command Prompt. All of the software is freely available on the Web ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-open-command-prompt-insert-commands
Java Program to Open the Command Prompt and Insert Commands - GeeksforGeeks
April 22, 2025 - It is done by passing "cmd" command to exec() method. ... // Java program to illustrate // open cmd prompt class Geeks { public static void main(String[] args) { try { // Just one line and you are done !
๐ŸŒ
IONOS
ionos.com โ€บ digital guide โ€บ websites โ€บ web development โ€บ java commands
The most important Java commands in an overview - IONOS
September 27, 2022 - Weโ€™ve collected a set of the most important Java commands so you can learn how to include them in your work.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_intro.asp
Introduction to Java
By the end of this tutorial, you will know how to write basic Java programs and apply your skills to real-life examples. You don't need any prior programming experience - just curiosity and practice! Follow the tutorial in order. Each chapter builds on the previous one. Get Started ยป ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
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);
๐ŸŒ
KaaShiv InfoTech
kaashivinfotech.com โ€บ home โ€บ how to run a java program in cmd: step-by-step guide for beginners
How to Run a Java Program in CMD โ€“ Easy Step-by-Step
August 9, 2025 - Learn how to run a Java program in CMD with simple steps. Compile, execute, and troubleshoot Java code directly from the command line.
๐ŸŒ
Jack's blog
jacknodes.hashnode.dev โ€บ java-programming-in-cmd-everything-you-need-to-get-started
Java Programming in Cmd: Everything You Need to Get Started
September 16, 2023 - So you want to learn Java programming but don't have an IDE installed yet? No problem, you can get started with Java using just your command line. CMD, or the Windows Command Prompt, has everything you need to compile and run simple Java programs. ...