Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

People also ask

Can I run Java code in terminal?

Whatever your purpose may be, executing Java code directly from the terminal is a very easy task. In this article, I will show you how you can execute Java directly from your favorite terminal window. Fear not!

Why I cant run my Java program in CMD?

It means that javac.exe executable file, which exists in bin directory of JDK installation folder is not added to PATH environment variable. You need to add JAVA_HOME/bin folder in your machine's PATH to solve this error. You cannot compile and run Java program until your add Java into your system's PATH variable.


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 filenamehere.java 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 filenamehere.java

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 filenamehere.java 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!


To complete the answer :

  1. The Java File

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

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

    java 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

    java -jar TheJavaFile.jar
    

Complile a Java file to generate a class:

javac filename.java

Execute the generated class:

java filename

In case your Java class is in some package. Suppose your Java class named ABC.java 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 ABC.java

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

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

java HelloWorld.java

This was an enhancement with JEP 330: https://openjdk.java.net/jeps/330

For the details of the usage and the limitations, see the manual of your Java implementation such as one provided by Oracle: https://docs.oracle.com/en/java/javase/11/tools/java.html