How to run a java program from the command line

How many ways to run java programming? 
 
Before the Java virtual machine (VM) can run a Java program, the program's Java source code must be compiled into byte-code using the javac compiler.

How to run a java program 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
     
     

    You need to specify a Main-Class in the jar file manifest.
    Oracle's tutorial contains a complete demonstration, but here's another one from scratch. You need two files:
    Test.java:
    public class Test
    {
        public static void main(String[] args)
        {
            System.out.println("Hello world");
        }
    }
    manifest.mf:
    Manifest-version: 1.0
    Main-Class: Test
    Note that the text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
    Then run:
    javac Test.java
    jar cfm test.jar manifest.mf Test.class
    java -jar test.jar
    Output:
    Hello world
     
     
    How to run a java program from the command line step by step

    1. Install the Java Development Kit.

    2. Set system variables to easily be able to compile and execute java files.

    3.  Compile and execute a Java file from Command Prompt.

    ** This is mainly for Windows 7 and Vista.

    Step 1: Download latest JDK

    Picture of Download latest JDK
    1.Follow this link: http://www.oracle.com/technetwork/java/javase/downloads/index.html
    2. Click Download JDK under the download Java Standard Edition panel.
    3. Choose your operating System and agree to the terms of service.

    ** This will  probably be Windows since your using Command Prompt. **
     
    Useful tips for how to run java program