views:

127

answers:

5

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 both 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

+3  A: 

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
oxbow_lakes
I can do it for my own system. But If I give to my friends who only want to run the application by double clicking on the jar file or the batch file then what I have to do
Deepak
*@Sunil* - see my edit. Note that your friends already have to have a java JRE installed (`java` on the path or a `JAVA_HOME` env variable set) for this to work
oxbow_lakes
Thanks, So as you mentioned that I have to paste only %JAVA_HOME\bin\java -jar ..\lib\myapp.jar in startup.bat or whether I need to wtite any other thing eg- java -jar MyApp.jar or i donot need.
Deepak
If you just write `java -jar ..\lib\myapp.jar` you are assuming that the user has a `PATH` variable which includes their JRE (i.e. the location of their `java` executable), which may not always be the case. By declaring `%JAVA_HOME%\bin\jar` you are assuming that the user has a `Java_HOME` variable set. In my opinion the latter is preferable because it is easier for the user to fix in my experience
oxbow_lakes
Thanks for help and giving me your precious time. I may disturb you If I found some problem.
Deepak
A: 

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.

michael.kebe
This is only "distributable" if *Sunil* puts the JAR file on some shared directory or if every user puts it in the same directory
oxbow_lakes
A: 

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.

MarkoU
A: 

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

St.Shadow
A: 

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.

fvu