[java] Running JAR file on Windows

I have a JAR file named helloworld.jar. In order to run it, I'm executing the following command in a command-line window:

java -jar helloworld.jar

This works fine, but how do I execute it with double-click instead? Do I need to install any software?

This question is related to java jar executable-jar

The answer is


If you need to run the jar file by double clicking on it, you have to create it as a "Runnable JAR". you can do it simply with your IDE.

If you're using eclipse, follow these steps :

    To create a new runnable JAR file in the workbench:

1.From the menu bar's File menu, select Export.
2.Expand the Java node and select Runnable JAR file. Click Next.
3.In the  Opens the Runnable JAR export wizard Runnable JAR File Specification page, select a 'Java Application' launch configuration to use to create a runnable JAR.
4.In the Export destination field, either type or click Browse to select a location for the JAR file.
5.Select an appropriate library handling strategy.
Optionally, you can also create an ANT script to quickly regenerate a previously created runnable JAR file.

more information can be found on Eclipse help Page: LINK


use .bat file:

Put your command in a .bat file. here, your command will be java -jar path\yourJarName.jar. Something like: java -jar C:\workspace\myApplication.jar

Save it and double click on bat file to run your jar.


In Windows Vista or Windows 7, the manual file association editor has been removed.

The easiest way is to run Jarfix, a tiny but powerful freeware tool. Just run it and your Java apps is back... double-clickable again.


If you need to distribute your .jar file and make it runnable at other people's Windows computers, you can make a simple .bat file like this in the command prompt:

java -jar MyJavaTool.jar

and place the .bat file in the same directory as your .jar file.


Create .bat file:

start javaw -jar %*

And choose app default to open .jar with this .bat file.

It will close cmd when start your .jar file.


There are many methods for running .jar file on windows. One of them is using the command prompt.

Steps :

  1. Open command prompt(Run as administrator)
  2. Now write "cd\" command for root directory
  3. Type "java jar filename.jar" Note: you can also use any third party apps like WinRAR, jarfix, etc.

Unfortunatelly, it is not so easy as Microsoft has removed advanced file association dialog in recent Windows editions. - With newer Windows versions you may only specify the application that is going to be used to open .jar file.

Fixing .jar file opening on Windows requires two steps.

  1. Open the Control Panel, and chose "Default Programs -> Set Associations". Find .jar extension (Executable JAR file) there, and pick Java as default program to open this extension. It will probably be listed as "Java Platform(SE)". A faster alternative perhaps is straightforward right-click on a .jar file, and then change associated program by clicking on the "Change..." button.

  2. Now open the regedit, and open the HKEY_CLASSES_ROOT\jarfile\shell\open\command key. Luckilly for us, we may specify parameters there for the (Default) value. On my Windows system it looks like: C:\app\32\jre7\bin\javaw.exe" -jar "%1" %* but in most cases it is the following string: C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*

NOTES:

  • Do not use java.exe there as it will open the shell window.
  • The jarfix tool mentioned in this thread most likely does nothing more than the registry modification for you. I prefer manual registry change method, as that implies that system administrator can "push" the registry change to all workstations in the network.

For compiling:

javac  -cp ".;./mysql-connector-java-5.0.8.jar;mybatis-3.0.1.jar;ibatis-2.3.0.677.jar" MainStart.java

For running:

java  -cp ".;./mysql-connector-java-5.0.8.jar;mybatis-3.0.1.jar;ibatis-2.3.0.677.jar" MainStart

I had the same problem in Windows 10. I fixed it using righ-click on the "helloworld.jar" and go to properties and click on change button under "Opens with:" and select "Look for another app on this PC". In the "Open with..." dialog box, go to your Java folder location on your PC and open corresponding jdk folder and then open the bin folder and select "javaw.exe" from there. Then next time your "helloworld.jar" will open the normal way.

Usual java location example : "C:\Program Files (x86)\Java\jdk1.8.0_111\bin".


Besides all of the other suggestions, there is one other thing you need to consider. Is your helloworld.jar a console program? If it is, then I don't believe you'll be able to make it into a double-clickable jar file. Console programs use the regular cmd.exe shell window for their input and output. Usually the jar "launcher" is bound to javaw.exe which doesn't create a command-shell window.


An interesting side effect of this causes a problem when starting runnable jar files in the command prompt.

If you try (in a command prompt):

jarfile.jar parameter

No joy, because this is being translated to the following (which doesn't work):

javaw.exe -jar jarfile.jar parameter

However, the following command does work:

java.exe -jar jarfile.jar parameter

If you change the association in file manager as described above to:

"C:\Program Files\Java\j2re1.4.2_04\bin\java.exe" -jar "%1" %*

Then you can type:

jarfile.jar parameter

in the command prompt and it will now work!

EDIT:(However you then get a black console window when you run a form based (non console) Java app, so this is not an ideal solution)

If you run these jar files by double clicking them in windows, no parameters will be passed so your Java code needs to handle the stack overflow exception and include a "press a key" function at the end or the window will just disappear.

In order to pass a parameter in windows you have to create a shortcut to the jar file, which includes the parameter in the target line (right click on the shortcut and select properties) you can not add parameters to the jar file icon itself in this way.

There isn't a single, consistent solution here, but you would have the same problem with any other console application.

There is a windows freeware application called "bat to exe" which you can use to create an exe file from a .bat file with the apropriate command line in it. you can also embed the jar file in the exe with this application, and make it clean it up when it has finished running, so this may be a more elegant solution.


You want to check a couple of things; if this is your own jar file, make sure you have defined a Main-class in the manifest. Since we know you can run it from the command line, the other thing to do is create a windows shortcut, and modify the properties (you'll have to look around, I don't have a Windows machine to look at) so that the command it executes on open is the java -jar command you mentioned.

The other thing: if something isn't confused, it should work anyway; check and make sure you have java associated with the .jar extension.


In regedit, open HKEY_CLASSES_ROOT\Applications\java.exe\shell\open\command

Double click on default on the left and add -jar between the java.exe path and the "%1" argument.


In Windows XP * you need just 2 shell commands:

   C:\>ftype myjarfile="C:\JRE1.6\bin\javaw.exe" -jar "%1" %* 
   C:\>assoc .jar=myjarfile  

obviously using the correct path for the JRE and any name you want instead of myjarfile.

To just check the current settings:

   C:\>assoc .jar  
   C:\>ftype jarfile  

this time using the value returned by the first command, if any, instead of jarfile.

* not tested with Windows 7


Create .bat file:

start javaw -jar %*

And choose app default to open .jar with this .bat file.

It will close cmd when start your .jar file.


An interesting side effect of this causes a problem when starting runnable jar files in the command prompt.

If you try (in a command prompt):

jarfile.jar parameter

No joy, because this is being translated to the following (which doesn't work):

javaw.exe -jar jarfile.jar parameter

However, the following command does work:

java.exe -jar jarfile.jar parameter

If you change the association in file manager as described above to:

"C:\Program Files\Java\j2re1.4.2_04\bin\java.exe" -jar "%1" %*

Then you can type:

jarfile.jar parameter

in the command prompt and it will now work!

EDIT:(However you then get a black console window when you run a form based (non console) Java app, so this is not an ideal solution)

If you run these jar files by double clicking them in windows, no parameters will be passed so your Java code needs to handle the stack overflow exception and include a "press a key" function at the end or the window will just disappear.

In order to pass a parameter in windows you have to create a shortcut to the jar file, which includes the parameter in the target line (right click on the shortcut and select properties) you can not add parameters to the jar file icon itself in this way.

There isn't a single, consistent solution here, but you would have the same problem with any other console application.

There is a windows freeware application called "bat to exe" which you can use to create an exe file from a .bat file with the apropriate command line in it. you can also embed the jar file in the exe with this application, and make it clean it up when it has finished running, so this may be a more elegant solution.


PreScript: If your prompt appears and disappears immediately, the reason it does so is that your program gets executed and auto shut. Try putting a scanner in the end to terminate and it'll keep your prompt waiting for input before terminating. (Or use delay maybe)

Was in the very same situation, where running .jar from cmd was working fine, but double clicking did nothing.

Solution: Open any text editor and write the command line: java -jar Example.jar Save the file as a .bat file. Run this bat file to get the needed output.

Taking it one step forward, you can convert this bat file to exe file using a simple GUI tool like Bat To Exe Converter.

Now you can share your .jar as a distribution in .exe file which anyone can use just make sure you keep all the files together. (Especially the .jar and .bat file cause .bat is only a cmd prompt)(How it feels logical)

I am fairly new to development and learning a lot. Please excuse for any mistakes if committed. Suggestions are welcome.


Steps:

1.) search for Java SE Runtime Environment on Google: https://www.google.com/search?q=Java+SE+Runtime+Environment

2.) install the appropriate version onto your computer


If you have a jar file called Example.jar, follow these rules:

  1. Open a notepad.exe
  2. Write : java -jar Example.jar
  3. Save it with the extension .bat
  4. Copy it to the directory which has the .jar file
  5. Double click it to run your .jar file

For compiling:

javac  -cp ".;./mysql-connector-java-5.0.8.jar;mybatis-3.0.1.jar;ibatis-2.3.0.677.jar" MainStart.java

For running:

java  -cp ".;./mysql-connector-java-5.0.8.jar;mybatis-3.0.1.jar;ibatis-2.3.0.677.jar" MainStart

In regedit, open HKEY_CLASSES_ROOT\Applications\java.exe\shell\open\command

Double click on default on the left and add -jar between the java.exe path and the "%1" argument.


If you use eclipse for making your java files, you can choose to export it as a runnable jar file. I did this with my programs and I can just click on the jar and it will run just like that. This will work on both windows, as well as os x.


Making a start.bat was the only thing that worked for me.

open a text document and enter. java -jar whatever yours is called .jar

save as start.bat in the same folder as the .jar file you want to execute. and then run the. bat


There are many methods for running .jar file on windows. One of them is using the command prompt.

Steps :

  1. Open command prompt(Run as administrator)
  2. Now write "cd\" command for root directory
  3. Type "java jar filename.jar" Note: you can also use any third party apps like WinRAR, jarfix, etc.

Besides all of the other suggestions, there is one other thing you need to consider. Is your helloworld.jar a console program? If it is, then I don't believe you'll be able to make it into a double-clickable jar file. Console programs use the regular cmd.exe shell window for their input and output. Usually the jar "launcher" is bound to javaw.exe which doesn't create a command-shell window.


You want to check a couple of things; if this is your own jar file, make sure you have defined a Main-class in the manifest. Since we know you can run it from the command line, the other thing to do is create a windows shortcut, and modify the properties (you'll have to look around, I don't have a Windows machine to look at) so that the command it executes on open is the java -jar command you mentioned.

The other thing: if something isn't confused, it should work anyway; check and make sure you have java associated with the .jar extension.


First set path on cmd(command prompt):

set path="C:\Program Files\Java\jre6\bin"

then type

java -jar yourProgramname.jar 

There is way without requiring user to do changes on his PC. Runtime.getRuntime.exec() allows us to start cmd.exe and execute commands inside of it. So, it's possible for java program to run itself in command prompt when user clicks .jar file.

public static void main(String[] args) throws IOException {
    if(args.length == 0) {
        Process p = Runtime.getRuntime().exec("cmd.exe /c start java -jar " + (new File(NameOfClass.class.getProtectionDomain().getCodeSource().getLocation().getPath())).getAbsolutePath() + " cmd");
    } else {
        //code to be executed
    }
}

Actually, I faced this problem too, I got around with it by making a .bat runner for my jar file

here is the code:

class FileHandler{
   public static File create_CMD_Rnner(){
      int exitCode = -1625348952;
      try{
           File runner = new File(Main.batName);
           PrintWriter printer = new PrintWriter(runner);
           printer.println("@echo off");
           printer.println("title " + Main.applicationTitle);
           printer.println("java -jar " + Main.jarName + " " + Main.startCode );
           printer.println("PAUSE");
           printer.flush();
           printer.close();
           return runner;
       }catch(Exception e){
           System.err.println("Coudln't create a runner bat \n exit code: " + exitCode);
           System.exit(exitCode);
           return null;
       }
   }
}



Then in Your Main application class do this:

public class Main{
    static String jarName = "application.jar";
    static String applicationTitle = "java Application";
    static String startCode = "javaIsTheBest";
    static String batName = "_.bat";


    public static void main(String args[]) throws Exception{
        if(args.length == 0 || !args[0].equals(startCode)) {
            Desktop.getDesktop().open(FilesHandler.create_CMD_Rnner());
            System.exit(0);
        }else{
            //just in case you wanted to hide the bat
            deleteRunner();
            // Congratulations now you are running in a cmd window ... do whatever you want
            //......
            System.out.println("i Am Running in CMD");
            //......
            Thread.sleep(84600);
        }
    }


    public static void deleteRunner(){
        File batRunner = new File(batName);
        if(batRunner.exists()) batRunner.delete();
    }
}


Please Note that

  1. this code (my code) works only with a jar file, not a class file.

  2. the jar file must have the same name as the String "jarName" is the Main class


You want to check a couple of things; if this is your own jar file, make sure you have defined a Main-class in the manifest. Since we know you can run it from the command line, the other thing to do is create a windows shortcut, and modify the properties (you'll have to look around, I don't have a Windows machine to look at) so that the command it executes on open is the java -jar command you mentioned.

The other thing: if something isn't confused, it should work anyway; check and make sure you have java associated with the .jar extension.


If you need to run the jar file by double clicking on it, you have to create it as a "Runnable JAR". you can do it simply with your IDE.

If you're using eclipse, follow these steps :

    To create a new runnable JAR file in the workbench:

1.From the menu bar's File menu, select Export.
2.Expand the Java node and select Runnable JAR file. Click Next.
3.In the  Opens the Runnable JAR export wizard Runnable JAR File Specification page, select a 'Java Application' launch configuration to use to create a runnable JAR.
4.In the Export destination field, either type or click Browse to select a location for the JAR file.
5.Select an appropriate library handling strategy.
Optionally, you can also create an ANT script to quickly regenerate a previously created runnable JAR file.

more information can be found on Eclipse help Page: LINK


Actually, I faced this problem too, I got around with it by making a .bat runner for my jar file

here is the code:

class FileHandler{
   public static File create_CMD_Rnner(){
      int exitCode = -1625348952;
      try{
           File runner = new File(Main.batName);
           PrintWriter printer = new PrintWriter(runner);
           printer.println("@echo off");
           printer.println("title " + Main.applicationTitle);
           printer.println("java -jar " + Main.jarName + " " + Main.startCode );
           printer.println("PAUSE");
           printer.flush();
           printer.close();
           return runner;
       }catch(Exception e){
           System.err.println("Coudln't create a runner bat \n exit code: " + exitCode);
           System.exit(exitCode);
           return null;
       }
   }
}



Then in Your Main application class do this:

public class Main{
    static String jarName = "application.jar";
    static String applicationTitle = "java Application";
    static String startCode = "javaIsTheBest";
    static String batName = "_.bat";


    public static void main(String args[]) throws Exception{
        if(args.length == 0 || !args[0].equals(startCode)) {
            Desktop.getDesktop().open(FilesHandler.create_CMD_Rnner());
            System.exit(0);
        }else{
            //just in case you wanted to hide the bat
            deleteRunner();
            // Congratulations now you are running in a cmd window ... do whatever you want
            //......
            System.out.println("i Am Running in CMD");
            //......
            Thread.sleep(84600);
        }
    }


    public static void deleteRunner(){
        File batRunner = new File(batName);
        if(batRunner.exists()) batRunner.delete();
    }
}


Please Note that

  1. this code (my code) works only with a jar file, not a class file.

  2. the jar file must have the same name as the String "jarName" is the Main class


Besides all of the other suggestions, there is one other thing you need to consider. Is your helloworld.jar a console program? If it is, then I don't believe you'll be able to make it into a double-clickable jar file. Console programs use the regular cmd.exe shell window for their input and output. Usually the jar "launcher" is bound to javaw.exe which doesn't create a command-shell window.


Steps:

1.) search for Java SE Runtime Environment on Google: https://www.google.com/search?q=Java+SE+Runtime+Environment

2.) install the appropriate version onto your computer


You want to check a couple of things; if this is your own jar file, make sure you have defined a Main-class in the manifest. Since we know you can run it from the command line, the other thing to do is create a windows shortcut, and modify the properties (you'll have to look around, I don't have a Windows machine to look at) so that the command it executes on open is the java -jar command you mentioned.

The other thing: if something isn't confused, it should work anyway; check and make sure you have java associated with the .jar extension.


There is way without requiring user to do changes on his PC. Runtime.getRuntime.exec() allows us to start cmd.exe and execute commands inside of it. So, it's possible for java program to run itself in command prompt when user clicks .jar file.

public static void main(String[] args) throws IOException {
    if(args.length == 0) {
        Process p = Runtime.getRuntime().exec("cmd.exe /c start java -jar " + (new File(NameOfClass.class.getProtectionDomain().getCodeSource().getLocation().getPath())).getAbsolutePath() + " cmd");
    } else {
        //code to be executed
    }
}

I had the same problem in Windows 10. I fixed it using righ-click on the "helloworld.jar" and go to properties and click on change button under "Opens with:" and select "Look for another app on this PC". In the "Open with..." dialog box, go to your Java folder location on your PC and open corresponding jdk folder and then open the bin folder and select "javaw.exe" from there. Then next time your "helloworld.jar" will open the normal way.

Usual java location example : "C:\Program Files (x86)\Java\jdk1.8.0_111\bin".


Besides all of the other suggestions, there is one other thing you need to consider. Is your helloworld.jar a console program? If it is, then I don't believe you'll be able to make it into a double-clickable jar file. Console programs use the regular cmd.exe shell window for their input and output. Usually the jar "launcher" is bound to javaw.exe which doesn't create a command-shell window.


Making a start.bat was the only thing that worked for me.

open a text document and enter. java -jar whatever yours is called .jar

save as start.bat in the same folder as the .jar file you want to execute. and then run the. bat


I´m running Windows 7 x64 and was unable to use any of these fixes.

This one worked for me afterall:

http://thepanz.netsons.org/post/windows7-jar-file-association-broken-with-nokia-ovi

There is an archive which you can download containing a .bat file to run, but check the path of the actual javaw.exe!!!!


Another way to run jar files with a click/double-click, is to prepend "-jar " to the file's name. For example, you would rename the file MyJar.jar to -jar MyJar.jar.

You must have the .class files associated with java.exe, of course. This might not work in all cases, but it has worked most times for me.


First set path on cmd(command prompt):

set path="C:\Program Files\Java\jre6\bin"

then type

java -jar yourProgramname.jar 

Another way to run jar files with a click/double-click, is to prepend "-jar " to the file's name. For example, you would rename the file MyJar.jar to -jar MyJar.jar.

You must have the .class files associated with java.exe, of course. This might not work in all cases, but it has worked most times for me.


If you have a jar file called Example.jar, follow these rules:

  1. Open a notepad.exe
  2. Write : java -jar Example.jar
  3. Save it with the extension .bat
  4. Copy it to the directory which has the .jar file
  5. Double click it to run your .jar file

I´m running Windows 7 x64 and was unable to use any of these fixes.

This one worked for me afterall:

http://thepanz.netsons.org/post/windows7-jar-file-association-broken-with-nokia-ovi

There is an archive which you can download containing a .bat file to run, but check the path of the actual javaw.exe!!!!


If you use eclipse for making your java files, you can choose to export it as a runnable jar file. I did this with my programs and I can just click on the jar and it will run just like that. This will work on both windows, as well as os x.


use .bat file:

Put your command in a .bat file. here, your command will be java -jar path\yourJarName.jar. Something like: java -jar C:\workspace\myApplication.jar

Save it and double click on bat file to run your jar.


In Windows XP * you need just 2 shell commands:

   C:\>ftype myjarfile="C:\JRE1.6\bin\javaw.exe" -jar "%1" %* 
   C:\>assoc .jar=myjarfile  

obviously using the correct path for the JRE and any name you want instead of myjarfile.

To just check the current settings:

   C:\>assoc .jar  
   C:\>ftype jarfile  

this time using the value returned by the first command, if any, instead of jarfile.

* not tested with Windows 7


If you need to distribute your .jar file and make it runnable at other people's Windows computers, you can make a simple .bat file like this in the command prompt:

java -jar MyJavaTool.jar

and place the .bat file in the same directory as your .jar file.


In Windows Vista or Windows 7, the manual file association editor has been removed.

The easiest way is to run Jarfix, a tiny but powerful freeware tool. Just run it and your Java apps is back... double-clickable again.


PreScript: If your prompt appears and disappears immediately, the reason it does so is that your program gets executed and auto shut. Try putting a scanner in the end to terminate and it'll keep your prompt waiting for input before terminating. (Or use delay maybe)

Was in the very same situation, where running .jar from cmd was working fine, but double clicking did nothing.

Solution: Open any text editor and write the command line: java -jar Example.jar Save the file as a .bat file. Run this bat file to get the needed output.

Taking it one step forward, you can convert this bat file to exe file using a simple GUI tool like Bat To Exe Converter.

Now you can share your .jar as a distribution in .exe file which anyone can use just make sure you keep all the files together. (Especially the .jar and .bat file cause .bat is only a cmd prompt)(How it feels logical)

I am fairly new to development and learning a lot. Please excuse for any mistakes if committed. Suggestions are welcome.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to jar

Running JAR file on Windows 10 Add jars to a Spark Job - spark-submit Deploying Maven project throws java.util.zip.ZipException: invalid LOC header (bad signature) java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory Maven Error: Could not find or load main class Where can I download mysql jdbc jar from? Access restriction: The type 'Application' is not API (restriction on required library rt.jar) Create aar file in Android Studio Checking Maven Version Creating runnable JAR with Gradle

Examples related to executable-jar

Running JAR file on Windows 10 How do I tell Spring Boot which main class to use for the executable jar? Reference jars inside a jar Run java jar file on a server as background process What causes "Unable to access jarfile" error? "Could not find the main class" error when running jar exported by Eclipse how to check the version of jar file? How to run a class from Jar which is not the Main-Class in its Manifest file How to make an executable JAR file? how to run or install a *.jar file in windows?