gudii9
asked on
jar file executable
Hi,
how to make a jar file explicitly marked as executable while being creating it? any example on this say test.java has main method and i want to make that class as executable by default to begin with using eclipse or command line? any good sample examples on this? what is sealing a jar file means?
please advise
how to make a jar file explicitly marked as executable while being creating it? any example on this say test.java has main method and i want to make that class as executable by default to begin with using eclipse or command line? any good sample examples on this? what is sealing a jar file means?
please advise
You can define your executable class in the manifest file using the Main-Class key. Then you package all your relevant classes, including the manifest, in a jar and you are good to go.
Here is a good tutorial that explains how to create an executable jar file step by step: http://www.mkyong.com/java/how-to-make-an-executable-jar-file/
Here is a good tutorial that explains how to create an executable jar file step by step: http://www.mkyong.com/java/how-to-make-an-executable-jar-file/
Packages within JAR files can be optionally sealed, which means that all classes defined in that package must be archived in the same JAR file. You might want to seal a package, for example, to ensure version consistency among the classes in your software.
You seal a package in a JAR file by adding the Sealed header in the manifest, also there is a easier way to seal the jar file in eclipse.
You can export a runnable jar as specified here: http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-37.htm
You can follow the sealing options in eclipse as shown in the link : http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-export-jar.htm
You seal a package in a JAR file by adding the Sealed header in the manifest, also there is a easier way to seal the jar file in eclipse.
You can export a runnable jar as specified here: http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-37.htm
You can follow the sealing options in eclipse as shown in the link : http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-export-jar.htm
ASKER
http://www.mkyong.com/java/how-to-make-an-executable-jar-file/
i created jar
C:\test\classes\com\mkyong \awt>javac AwtExample.java
C:\test\classes\com\mkyong \awt>jar -cvfm AwtExample.jar manifest.txt com/mkyong/
awt/*.class
java.io.FileNotFoundExcept ion: manifest.txt (The system cannot find the file spe
cified)
at java.io.FileInputStream.op en0(Native Method)
at java.io.FileInputStream.op en(FileInp utStream.j ava:195)
at java.io.FileInputStream.<i nit>(FileI nputStream .java:138)
at java.io.FileInputStream.<i nit>(FileI nputStream .java:93)
at sun.tools.jar.Main.run(Mai n.java:175 )
at sun.tools.jar.Main.main(Ma in.java:12 88)
C:\test\classes\com\mkyong \awt>jar -cvfm AwtExample.jar manifest.txt com/mkyong/
awt/*.class
com\mkyong\awt\*.class : no such file or directory
added manifest
C:\test\classes\com\mkyong \awt>
i see the jar created.
when i click it
i do not see the message
This JAR file is executable!
demo1.png
demo2.png
i created jar
C:\test\classes\com\mkyong
C:\test\classes\com\mkyong
awt/*.class
java.io.FileNotFoundExcept
cified)
at java.io.FileInputStream.op
at java.io.FileInputStream.op
at java.io.FileInputStream.<i
at java.io.FileInputStream.<i
at sun.tools.jar.Main.run(Mai
at sun.tools.jar.Main.main(Ma
C:\test\classes\com\mkyong
awt/*.class
com\mkyong\awt\*.class : no such file or directory
added manifest
C:\test\classes\com\mkyong
i see the jar created.
when i click it
i do not see the message
This JAR file is executable!
demo1.png
demo2.png
You missed step 2
ASKER
jar -cvfm AwtExample.jar manifest.txt com/mkyong/awt/*.class
why above command giving below error
ar -cvfm AwtExample.jar manifest.txt com/mkyong/
awt/*.class
java.io.FileNotFoundExcept ion: manifest.txt (The system cannot find the file spe
cified)
at java.io.FileInputStream.op en0(Native Method)
at java.io.FileInputStream.op en(FileInp utStream.j ava:195)
at java.io.FileInputStream.<i nit>(FileI nputStream .java:138)
at java.io.FileInputStream.<i nit>(FileI nputStream .java:93)
at sun.tools.jar.Main.run(Mai n.java:175 )
at sun.tools.jar.Main.main(Ma in.java:12 88)
why above command giving below error
ar -cvfm AwtExample.jar manifest.txt com/mkyong/
awt/*.class
java.io.FileNotFoundExcept
cified)
at java.io.FileInputStream.op
at java.io.FileInputStream.op
at java.io.FileInputStream.<i
at java.io.FileInputStream.<i
at sun.tools.jar.Main.run(Mai
at sun.tools.jar.Main.main(Ma
ASKER
You missed step 2
which step?
you mean below step
2. Manifest.txt
Create a manifest.txt file.
Manifest.txt
Main-Class: com.mkyong.awt.AwtExample
Uses Main-Class as the entry point of this Jar file, when you double click on this Jar file, the “AwtExample.class” main() method will be launched.
i did created manifest.txt at
C:\test\classes\com\mkyong
then did below step
C:\test\classes\com\mkyong
then step 3
C:\test\classes\com\mkyong
awt/*.class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i am currently using eclipse where i have java file and text file at below path
C:\Users\xyz\gpWorkspace_N eon_122220 16\Executa bleJar\src \com\mkyon g\awt\AwtE xample.jav a
C:\Users\xyz\gpWorkspace_N eon_122220 16\Executa bleJar\src \com\mkyon g\awt\mani fest.txt.t xt
jar.png
C:\Users\xyz\gpWorkspace_N
C:\Users\xyz\gpWorkspace_N
jar.png
ASKER
i am confused what is root is it same as below path
C:\Users\xyz\gpWorkspace_N eon_122220 16\Executa bleJar\src
or below path where .java file is sitting
C:\Users\xyz\gpWorkspace_N eon_122220 16\Executa bleJar\src \com\mkyon g\awt\AwtE xample.jav a
how to see where is bin directory and where .class generated by eclipse??
C:\Users\xyz\gpWorkspace_N
or below path where .java file is sitting
C:\Users\xyz\gpWorkspace_N
how to see where is bin directory and where .class generated by eclipse??
ASKER
i saw useful video here
https://www.youtube.com/watch?v=ynXjh6jbeQ0
https://www.youtube.com/watch?v=ynXjh6jbeQ0
ASKER
here is how i see finaly output
RunnableJArDemo.png
RunnableJArDemo.png
As for the command line, when using "jar.exe" to create the jar file, you can pass the "e" option to provide the package/class that contains the main method, or alternatively you can pass the "m" option to provide a correctly crafted MANIFEST.MF file the makes the jar executable.