Creating Executable Program in Java

Executable software can be created using JAR(Java Archive) utility. Once you create jar file of the program it can run on any machine having JRE(Java Runtime Environment ) installed. JAR file should contain the .class files and resource such images, audio or any other files required. Including only .class files makes the code private. It can also include .java files, the source code of the program. Following are the commands to be run at Command Prompt or Terminal in Linux. Step 1: Compiling the Java Programs First compile all the Java programs of an application. This can be done with following command. The class files should be kept in separate directory. e.g. If All the source code files are present in “Source Code” and you want to place your code in “Classes” directory then following command can be used. Javac -d ../Classes *.java For running this command current directory should be the directory which contain the .java files. In our example “Source Code” directory. -d option tells the compiler that compiled files (.class files) should be kept in a separate directory. .. / moves one directory up from the current directory. *.java Tell the compiler that all the files in the current directory with .java extension should be compiled. Step 2: Create a manifest.txt file manifest.txt file should be created which tells where the main() method is present so that execution can begin from main() method. It contain following code. Main-Class: Editor Here Editor is the name of the class which contain the main method. After writing name of the class Enter key should be pressed otherwise the manifest may not work properly. If there are packages in the project then complete class name with the package should be specified as follows. Main-Class: com.Editor Step 3: Running the Jar command Go to Classes directory and then type following command. manifest file should be present in Classes directory. cd Classes Jar -cvmf manifest.txt Editor.jar *.class jar -cvmf creates the jar file manifest.txt name of the manifest file Editor.jar name of the jar file which you want to create *.class tells that all .class files should be used in jar When the above command is run then executable .jar file with name Editor.jar is created. META-INF directory and MANIFEST.MF file inside the META-INF directory is created. META-INF stands for Meta Information. MANIFEST.MF is a real manifest file which is created by taking information from the manifest.txt file. Manifest.txt in not included in the jar. Step 4: Running the Executable JAR For running the executable jar file following command can be used or it can be executed by double clicking the jar file. Java -jar Editor.jar

Add new comment