16 Jan 2014

Create a jar from folder and how to run the jar in the console on LINUX

1. Create a JAR from folder

 jar -cf NameYouNeed.jar ./FolderYouWantToCreateJAR

c - Create a new Archive
f - file name of the archive is specified here

2. The above command creates a NameYouNeed.jar with MANIFEST.MF file which will have details like java version, entry point(main class name) etc.

3. The above MANIFEST.MF will not have the entry point(main class), to add an entry point you should use

jar -cfe NameYouNeed.jar ./FolderYouWantToCreateJAR Main.class

e - entry point

4. JAVA can run this jar file as below

java -jar Main.jar

5. When using a package you need to specify the path, can you either . or /

jar -cfe Main.jar foo/Main foo

Here foo/Main is for Manifest main class, second foo is for path where the files to be taken for jarring.

If you want to use any library jars then you should specify the parameter "Class-Path" because the usage of -jar option with java will ignore the default classpath, even if you use -cp option the jar in -cp is ignored.For manipulating the "Class-Path" in the manifest file we will use "-m" where we need to use a temporary file from which the jar will take parameters to overwrite the default Manifest file it generates, We'll see below how to do it.


Create a file myManifest.MF with the following content




Main.jar being your jar location.

We can also add "Main-Class" into myManifest.MF which goes into Manifest file  the jar created.







The following command

jar -cmf myManifest.MF Main.jar Main.class 




will create the Manifest file appropriately with proper classpath so the library will be located by java while executing.







No comments:

Post a Comment

Note: only a member of this blog may post a comment.