Skip to content
Advertisement

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:

JavaScript

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)?

Advertisement

Answer

Source: javaindos.

Let’s say your file is in C:mywork

Run Command Prompt

JavaScript

This makes C:mywork the current directory.

JavaScript

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

JavaScript

This tells the system where to find JDK programs.

JavaScript

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

JavaScript

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

JavaScript

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!

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement