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
cmd - How do I run a Java program from the command line on Windows? - Stack Overflow
command line - Run java classfile from terminal - Unix & Linux Stack Exchange
Using a text file as input for Java
How to run java program on Command Prompt/Terminal with Intellij (WINDOWS)?
Videos
To compile the file, open your terminal and type
javac filename.java
To run the generated class file, use
java filename
But to do this you need to have the Java JDK installed in your computer. You can install it with the instructions in How do I install Java?.
OpenJDK works best for me. It's simple and I have never faced any problem with it. Just follow these simple steps:
From Terminal install open jdk
sudo apt-get install openjdk-7-jdkWrite a java program and save the file as filename.java
Now to compile use this command from the terminal
javac filename.javaIf everything works well then a new "filename.class" file should be created.
To run your program that you've just compiled type the command below in terminal:
java filename
NOTE
You can use any text editor (like gedit) ,
replace the filename with watever name you want
you need to be on same directory as the "present working directory" (got by running pwd) while running the command from terminal.
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