How to Create Executable jar file from .java file. Having only one java file dm.java which created using awt and applet. I need to create executable jar with icon or with im
the easiest is to export to jar file using eclipse. otherwise, you will need to use java -jar command.
to make that double click happen, double clickin on the jar file will automatically the jar. otherwise, u can always create a shortcut (assumin ur on win) to the jar file. and then u can double click that shortcut to open up ur jar file
Using Eclipse, right click on project ->'Export'->'Java'->'Runnable Jar File', fill in all fields and press 'Finish'. Most jars exported with this method can be double clicked to run, however some require starting from command line.
It is possible to use Java Web Start to launch applets free-floating on the desk top. JWS can install desktop shortcuts & menu items for apps., both with icons.
JWS uses Jar files only. To Jar your applet, do something like..
prompt>dir
dm.java
prompt>javac dm.java // compile the source
prompt>dir
dm.class
dm$1.class
dm.java
prompt>jar -cf *.class dm.jar // archive the classes
prompt>dir
dm.class
dm$1.class
dm.jar
dm.java
prompt>
It is not strictly necessary to include a manifest in a JWS application, and there is almost no point for a JWS applet. The applet class is instead specified in the JNLP file used to launch the applet. The JNLP file also contains information on the shortcuts and icons.
To do this you have to include file MANFEST.MF into META-INF directory of your jar. The manifest must contain attribute Main-Class
. Its value is the fully qualified class name of your main class, i.e. class that is the entry point to your application.
See http://download.oracle.com/javase/1.4.2/docs/guide/jar/jar.html for details.
Icon is different. If you are speaking about icon that appears on the left upper corner of your application window use frame.setIconImage(image)
.
If you are asking about the icon that appears on your command window this is platform specific and could be achieved by running script.
When you create a executable .jar with a manifest that contains Main-Class: , it will work by double clicking BUT ONLY if you have the correct file associations setup on your system. For this to be correct, you need to install a RECENT version of Java. Some of the older versions of Java wont setup the file association that your end-user needs. You can guarantee it will work only by providing them a batch script that creates the file association, like so:
@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
:: this .bat script creates a file association for executable .jar files
ECHO Creating .jar file association...
ECHO JAVA_HOME is %JAVA_HOME%
IF NOT DEFINED JAVA_HOME GOTO :FAIL
REG ADD "HKCR\jarfile" /ve /t REG_SZ /d "Executable Jar File" /f
REG ADD "HKCR\jarfile\shell" /ve /f
REG ADD "HKCR\jarfile\shell\open" /ve /f
ECHO REG ADD "HKCR\jarfile\shell\open\command" /ve /t REG_SZ /d "\"%JAVA_HOME%\bin\javaw.exe\" -jar \"%%1\" %%*" /f
REG ADD "HKCR\jarfile\shell\open\command" /ve /t REG_SZ /d "\"%JAVA_HOME%\bin\javaw.exe\" -jar \"%%1\" %%**" /f
REG ADD "HKLM\jarfile" /ve /t REG_SZ /d "Executable Jar File" /f
REG ADD "HKLM\SOFTWARE\Classes\jarfile\shell" /ve /f
REG ADD "HKLM\SOFTWARE\Classes\jarfile\shell\open" /ve /f
REG ADD "HKLM\SOFTWARE\Classes\jarfile\shell\open\command" /ve /t REG_SZ /d "\"%JAVA_HOME%\bin\javaw.exe\" -jar \"%%1\" %%*" /f
ECHO Finished creating .jar file association for executable .jar files.
PAUSE
GOTO EOF
:FAIL
ECHO Script failed. JAVA_HOME not defined.
PAUSE