Tuesday, October 2, 2012

Run Java console application by double click

Assume you have written a console java application in Eclipse and want to distribute it to users as an executable. You export the class files as an executable JAR file from eclipse, however to run the JAR file you need to use Windows Command Prompt and type in the command
java -jar Filename.jar arguments

To make it more easily runnable - by a double click - we need to create a .bat file with the following contents  and present in the same folder as the JAR file.

@echo off
 set jarpath="JavaApp.jar"
 java -jar %jarpath% %CD%Config.txt
 PAUSE

Here, %CD% is a pseudo-variable which holds the working directory. It’s useful when you want to load a config file located in the same folder as your .bat file. You can also replace it with any arguments for the JAR file. PAUSE displays a localized version of “Press any key to continue…” message.

References:
http://blog.mwrobel.eu/how-to-properly-run-java-consol-application-with-a-doubleclick-windows/

No comments:

Post a Comment