September 11, 2016

Java basic compilation and execution


  1. Your Java File (source code)
    MyGame.java
  2. Compile the Java File to a *.class file. This is Java bytecode that can be executed on the Java Virtual Machine (JVM). JVM is installed automatically when we install Java and it will be invoked automatically when we run a java program.

    Note : In Eclipse, "run" perform both compilation and execution.
    javac MyGame.java
    • This will create a MyGame.class file 
  3. Execution of the Java File
    java MyGame
  4. Creation of an executable *.jar file. A jar file is simply a file containing a collection of java files.

    First, create a manifest file : touch manifest.mf

    In the manifest file, point Main-Class to our java file that has main entry point i.e. main() method. For example,
    Manifest-Version: 1.0
    Main-Class: MyGame
    Make sure the compiled output class files (myGame.class) and the manifest file are in the same folder.

    Create the .jar file by the following :

    jar cfm MyGame.jar manifest.mf *.class
  5. To run the Jar File
    java -jar MyGame.jar