2

I have a very basic .jar file that successfully runs, though I can only seem to run it by doing one of two things:

  1. Using the command prompt and entering a command such as java -jar test.jar
  2. Creating a shortcut with the path being java -jar C:\Users\Nick\Documents\test.jar

Is there a way to run a .jar file without having to do either of these two things, IE a way to run it from within Windows Explorer?

Edit: My .jar file looks like this:

Manifest-Version: 1.0 Rsrc-Class-Path: ./ Class-Path: . Rsrc-Main-Class: base.MainClass Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader 

I used Eclipse to create this. Should I edit the Main-Class to just be base.MainClass, and remove anything with Rsrc in it?

2
  • 7
    If Java is properly installed, you can simply double click the jar file Commented May 4, 2012 at 19:09
  • You could try editing the registry manually, or using a program like Types ( izt.name/soft/types ) to change the file association for the ".jar" extension. Then, you could set them up to open with "java -jar jarFileNameHere.jar" Commented May 4, 2012 at 19:12

4 Answers 4

5

Note: When you double-click on a JAR and the JAR just prints out something on the console, the window could close immediately after the execution. In this case you won't see much. But your program was executed correctly and just the window was just closed after the execution. Try to open a JFrame in your application, then you should see the frame when you double-click on the JAR.

Sign up to request clarification or add additional context in comments.

3 Comments

"Try to open a JFrame in your application" If the app. is based on a frame, it can be launched from a desktop shortcut (complete with icon) using Java Web Start.
I have it based on an AWT frame, but double-clicking the .jar file doesn't appear to do anything, while launching it from from the console it will open the empty frame I am testing with. What would cause this?
How are you creating the JAR file? With Eclipse? Open your JAR file with a ZIP-program and have a look at the manifest file. How does it look?
5

Make a .bat file in the distribution directory.

@echo off start javaw -Dfile.encoding=UTF8 -jar test.jar exit 

This .bat file will also fix problems with special characters working in eg. netbeans, but not when double clicking the .jar

If you don't need support for special characters you can leave out the "-Dfile.encoding=UTF8" part.

Comments

2

My JARs are associated with 7Zip. To run an executable JAR, right click the file name and select Open With > Java.

Comments

1

Installing Java should create a shortcut for your OS, so that you can open executable jars by double click.

If it doesn't work for you, you have to investigate how to do it for your version of the OS.

I guess for Windows it is right-clicking on the app, and then configuring the "open with ..." dialog.

The program to run is afaik:

javaw -jar "%*" 

where you have to specify the whole path to javaw, if it isn't in the PATH.

If the Java program expects command line arguments itself, for example a program to rotate an image might expect image files as arguments, so you can draw them with the mouse on the jarfile, therefore you specify the windows syntax for "all parameters" which is "%*" or something similar. The manual of your OS should answer the question.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.