Feb 5, 2012

Java compile, run, make executable (jar, exe)

To compile,
javac sourcecode.java
--> "sourcecode.class" file is formed

To run,
java sourcecode

It's easy to use a batch file to compile and run together.

in Netbeans,
menu --> run --> build project (F11)
then, xxx.jar file is created in /dist/ folder.
(if this doesn't give jar file, project properties --> build --> packaging --> build JAR after compiling)
(to make jnlp file (web start), project properties --> application --> web start --> enable web start, (allow offline signing))

To make executable, (after compiling)
1. make jar file
jar cvf filename.jar *.class   (or jar cvf filename.jar .)
(this command actually zip *.class files in the folder. *.jar is executable with JDK)
(if it includes sub-folder with folder name "image", jar cf filename.jar *.class -C image\ . )
(if you want to set manifest.mf and to add sub-folder, jar cfm filename.jar MANIFEST.MF *.class image)

If the created jar file is not executable, you may need to designate main class.
1) extract the created jar file (jar xvf filename.jar)
2) in text editor, add "Main-Class: NameOfMainClass" at the end of MANIFEST.MF file
3) make sure that the MANIFEST.MF file includes space line at the end
4) re-zip jar file with the MANIFEST.MF (jar umf MANIFEST.MF filename.jar)
Now, newly created jar file is executable

Note that if you want to check what is in a jar file, jar tvf filename.jar

2. make jar file executable (*.exe) in Windows
demand : jar file, JDK, jsmooth (http://jsmooth.sourceforge.net/download.php)
1) In jsmooth, skeleton --> skeleton selection --> windowed wrapper
2) executable --> executable binary --> set path in which created file will be located
3) set executable icon (jpg file)
4) application --> classpath --> select jar file to be executable
5) (optional) JVM selection --> set minimum/maximum java version (e.g., 1.5, 1.6)
6) (optional) JVM config --> set minimum/maximum memory to be used
7) menu --> project --> compile

No comments:

Post a Comment