macos - How to run a Java Class in Terminal - Stack Overflow
cmd - How do I run a Java program from the command line on Windows? - Stack Overflow
How to run a simple Java program using terminal command? - Unix & Linux Stack Exchange
How to run java program on Command Prompt/Terminal with Intellij (WINDOWS)?
Videos
Hey guys,
I'm having trouble understanding how to run a program in a terminal or on command prompt with Windows. Can someone give me a detailed explanation on how to run a program on command prompt/terminal?
Thanks!
cd into the directory in which your Calculator.java file is stored, run
javac Calculator.java
this will create a file Calculator.class. You can now run the compiled class with
java Calculator
mind that there is no .class to be added!
The online docs should be your first recourse: https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html which tell us that the command line is
java [options] classname [args]
In more depth, and linked from somewhere on that page, you can read https://docs.oracle.com/javase/8/docs/technotes/tools/unix/classpath.html#CBHHCGFB
which is applicable to pretty much all the Java tools. These docs contain the answer to your question. You can either cd into the directory that is the root of your classpath and use the default classpath, as one answer suggested, or use the classpath options described in the docs to set the directory(-ies) at the top of your classpath. E.g.,
java -cp /Users/mac/Documents/workspace/Calculator/bin Calculator
Source: javaindos.
Let's say your file is in C:\mywork\
Run Command Prompt
CopyC:\> cd \myworkThis makes C:\mywork the current directory.
CopyC:\mywork> dirThis displays the directory contents. You should see filenamehere.java among the files.
CopyC:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\binThis tells the system where to find JDK programs.
CopyC:\mywork> javac filenamehere.javaThis runs javac.exe, the compiler. You should see nothing but the next system prompt...
CopyC:\mywork> dirjavac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.
CopyC:\mywork> java filenamehereThis 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 :
The Java File
Copy
TheJavaFile.javaCompile the Java File to a *.class file
Copy
javac TheJavaFile.java- This will create a
TheJavaFile.classfile
- This will create a
Execution of the Java File
Copy
java TheJavaFileCreation of an executable
*.jarfileYou've got two options here -
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
Executable by Entry Point:
jar -cvfe TheJavaFile.jar <MainClass> TheJavaFile.class
To run the Jar File
Copy
java -jar TheJavaFile.jar