How many ways to run java programming?
How to run a java program answer :
- The Java File
TheJavaFile.java
- Compile the Java File to a *.class file
javac TheJavaFile.java
- This will create a
TheJavaFile.class
file
- This will create a
- Execution of the Java File
java TheJavaFile
- Creation of an executable
*.jar
file
- You'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
- With an external manifest file :
- You've got two options here -
- 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:
manifest.mf:public class Test { public static void main(String[] args) { System.out.println("Hello world"); } }
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.Manifest-version: 1.0 Main-Class: Test
Then run:
Output:javac Test.java jar cfm test.jar manifest.mf Test.class java -jar test.jar
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
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