Hexa's Blog

How to compile executable jar file from source code

15/07/2014 etc

This thread will illustrate how to make a executable file.

#1.Create file “manifest.txt”

1
2
Main Class: package.to.my.ProgramLaucher
\n - 'this character will not be writen, I mean that you need to end with a new line'

for example, my class is:

1
2
3
4
5
6
package cosc2101.assignment1.controller;
public class ProgramLauncher {
    public static void main(String[] args) {
	     System.out.println("Hello World");
    }
}

then, my manifest file should be

1
2
Main Class: cosc2010.assignment1.controller.ProgramLaucher
next line here(compulsory)

#2. Making file .class This file is a compiled file of file.java. In order to make this file. Use the following command on the terminal.

javac .path/to/file.java

In this case, my output is a file named ProgramLaucher.class #3.Setting up folder for compile In order to compile java source code, you need to add file manifest.txt and compiled file file.class. For example in the case above, after make a new directory, our directory should be

------/
      manifest.txt
	  cosc2101/assignment1/controller/ProgrammerLaucher.class

#4. Make .jar file Within this directory

jar -cvfm ProgramLaucher.jar manifest.txt *.class

'c' refers to creating new archive file, in this case, it is
`ProgramLaucher.jar`
'v' refers to verbose, while processing, all relevant details will be report on
the terminal screen
'f' refers to specified file .jar will be created - creating option is assigned
by `c` but, what and where to create is assigned by `f`
'm' refers to manifest file

#5. Run file .jar Using the following command

java -jar ProgramLaucher.jar