Most of users of Windows platform doesn't likes ".bat" files of Java applications and their consoles. They want standard ".exe" with its own icon, tray, services. Everybody knows that the client is right and we as developers have to satisfy all his wishes. Therefore I will share some tips with you and I will teach you how to make our Java applications more attractive. The first thing is to learn how to make exe file with its own icon that will start our main class. The second thing is how to use tray bar of Windows and to allow our Java application to be minimized there, to set its own menu and to receive events back in our java code. The third thing is how to make our Java application to act as normal Windows Service. To follow all these steps we need just one thing - download J2Exe Project which is free tool for noncommercial use. J2Exe contains 4 modules: J2Exe , J2TrayExe, J2WinService , J2TrayWinService.
Example1) Generate standard exe Download J2Exe. Lets suppose that we have starting script "start.bat" and "server.jar" with main class "server.MyServer". Starting script is in "bin" folder, while the "server.jar" is in "lib" folder. We want to replace "start.bat" file with "MyServer.exe" file. The "MyServer.exe" file will reside in "bin" folder too. The starting script looks like this: cd ..\lib java -Djava.compiler=NONE -classpath server.jar server.MyServer parameter1 parameter2 So, start J2Exe. You will see that "Input" is selected. Fill necessary information: Java parameters: -Djava.compiler=NONE -classpath server.jar Main class: server.MyServer Application parameters: parameter1 parameter2 Working directory: %APP_HOME%/../lib Then select "Output", choose type of application: "JDK with Console", "JDK with Managed Console" or "JDK without Console", click over icon if you want to specify your own icon, specify where "MyServer.exe" file should be saved including absolute path to "bin" folder. And finally click "Generate". Now you can find generated file and start it.
2) Minimize into tray Download J2TrayExe. Repeat the same actions as with J2Exe and you will receive exe. Console variant has separate button to minimize into tray. The other variants "JDK with Managed Console" and "JDK without Console" will use close button of frame to minimize into tray. Two additional classes - WinTray.java and WinTrayListener.java, are provided to create tray menu and to receive events back in Java code. The class WinTray.java contains static method "main" with an example how to use these two classes. Remember, no additional natives are required.
3) Create Windows service Download J2WinService. Repeat the same actions as with J2Exe. Fill additional information about service - name and discription, service type and start type. Service name: MyServer Service discription: MyServer Description Interact with the desktop: checked Service type: Service that runs in its own process Start type: Service started on demand or Service started automatically during system startup. To install this service write "MyServer /install" in the command prompt. To uninstall the service write "MyServer /uninstall".
Alexi Jordanov, Project Manager in ProSyst Bulgaria
http://alexiworld.tripod.com
alexiworld@yahoo.com