开发者

How to make a distributable batch file of my jar

Hi I have created a java application and created a jar file. Then I created a batch file eg-> startup.bat The contents of this file is java -jar MyApp.jar

Now if I double click the batch file then my java application works because bo开发者_如何转开发th the jar file and batch file belong to same directory. Now I copied the batch file to desktop and run then My application did not run.

Also I want My batch file automatically set the class path.

How to create a batch file with all the above feature

Thanks Sunil Kumar Sahoo


If the jar is an executable JAR (which it seems to be from your example), you should be able to just double-click the JAR file. If this does not work, you shoud associate the java executable as the program with which to open a file with the .jar extension. See Tools > Folder Options > File Types from an explorer window.

In order that a program be "distributable", it would normally be packaged (i.e. the JAR file and startup scripts) in some ZIP archive and then extracted to a folder on a user's PC. Your startup script should provide a relative path to the JAR file. For example if you have organized the contents of your ZIP into lib and bin folders:

MyProject
  +--- lib (contains jars)
  +--- bin (contains .bat script)

In this case, the startup.bat file should reference the Jar as:

%JAVA_HOME\bin\java -jar ..\lib\myapp.jar

This requires that any users:

  • Have a JRE installed on their system (unless you package that up as well)
  • Have java on their path or a JAVA_HOME environment variable set


The problem is that the batch file doesn't know where the jar is, it is looking relatively from its own path. You could copy the jar file to your desktop, too.

If just want an icon on your desktop, make a shortcut to the batch file rather than a copy.


Aslo if you application is quite complex (size more than 1+GByte) - you can distribute it with jre included.


Because it's a bit long to copy in here, I'd suggest that you have a look at what Squirrel SQL does. And you'll have a very nice cross-db tool as an extra bonus ;-)

They enable the tool to be started in two possible ways:

  • by starting the jar itself, classpath info will then be fetched from the manifest file (see here for the details, look up download extensions). In that case, after unzipping the hierarchy of files as explained by other posters, just make a shortcut on the desktop and you're all set
  • with a batch file. You'll see that the batchfile solution is quite complicated, especially the construction of the classpath.

So I'd go with the first solution.


I understand that you would like to distribute your application to multiple machines? A (very) simple solution would be to create a zip/tar/whatever archive that has all the needed jars (including MyApp.jar) and the startup script.

In the script you can use the -cp option to set the classpath to the jars:

java -cp needed.jar:needed2.jar -jar MyApp.jar

As said above, making a shortcut to the desktop is a better option than copying the script itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜