How do I run a Java program from the command line on Windows?


I'm trying to execute a Java program from the command line in Windows. Here is my code:


public class CopyFile
    public static void main(String[] args)

        InputStream inStream = null;
        OutputStream outStream = null;


            File afile = new File("input.txt");
            File bfile = new File("inputCopy.txt");

            inStream = new FileInputStream(afile);
            outStream = new FileOutputStream(bfile);

            byte[] buffer = new byte[1024];

            int length;
            // copy the file content in bytes
            while ((length = > 0)

                outStream.write(buffer, 0, length);



            System.out.println("File is copied successful!");

        catch (IOException e)

I'm not sure how to execute the program - any help? Is this possible on Windows? Why is it different than another environment (I thought JVM was write once, run anywhere)?

4/10/2017 3:05:56 AM

Accepted Answer

Source: javaindos.

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

Run Command Prompt

C:\> cd \mywork

This makes C:\mywork the current directory.

C:\mywork> dir

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

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

This tells the system where to find JDK programs.

C:\mywork> javac

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

C:\mywork> dir

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

C:\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!

11/2/2017 10:26:24 AM

In case your Java class is in some package. Suppose your Java class named is present in com.hello.programs, then you need to run it with the package name.

Compile it in the usual way:

C:\SimpleJavaProject\src\com\hello\programs > javac

But to run it, you need to give the package name and then your java class name:

C:\SimpleJavaProject\src > java com.hello.programs.ABC

Complile a Java file to generate a class:


Execute the generated class:

java filename

It is easy. If you have saved your file as A.text first thing you should do is save it as Now it is a Java file.

Now you need to open cmd and set path to you file before compile it. you can refer this for that.

Then you can compile your file using command


Then run it using

java A

So that is how you compile and run a java program in cmd. You can also go through these material that is Java in depth lessons. Lot of things you need to understand in Java is covered there for beginners.


You can compile any java source using javac in command line ; eg, javac To run : java CopyFile. You can also compile all java files using javac *.java as long as they're in the same directory

If you're having an issue resulting with "could not find or load main class" you may not have jre in your path. Have a look at this question: Could not find or load main class


Since Java 11, java command line tool has been able to run a single-file source-code directly. e.g.


This was an enhancement with JEP 330:

For the details of the usage and the limitations, see the manual of your Java implementation such as one provided by Oracle: