How to Run Java Class on Command Line

  1. The first thing you have to do is open up the command line. Do that by launching "Windows Command Prompt":

    Open up the start menu on your computer and type "cmd", then hit enter. It will launch the cmd.exe file and then the command prompt should pop up.

    The easiest way is to navigate to the file in Windows Explorer, and then hold SHIFT and right click on any white space in the directory. Then choose the option "Open command window here".
    If you do that, you can move on to step 3.
  2. You have to locate where your .java file is on your computer. Once you have found the location, navigate there in the command prompt.
  3. After that, you need to compile your .java file manually (using the command line).

    Type the following into the command line and hit enter:

    You should receive no feedback, but also no errors. You should just be able to type a new line into the command line.

    What you just did was compile the .class file (notice the "c" in "javac" - it means "compiler"). Look at the directory with your .java file and you should also see a .class file.
  4. And finally, you can run your Java class by using the .class file you just generated.

    Type the following into the command line and hit enter:
    java YourClassName

    Notice to not add the "c" at the end of "java" and to take out the ".java" from your file completely.

          Created: June 2, 2015
Completed in full by: Michael Yaworski