I have a JAR file named helloworld.jar. In order to run it, I\'m executing the following command in a command-line window:
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.
Easiest route is probably upgrading or re-installing the Java Runtime Environment (JRE).
Or this:
C:\Program Files\Java\j2re1.4.2_04\bin\javaw.exe" -jar "%1" %
(Note: the part starting with 'javaw' must be exactly like that; the other part of the path name can vary depending on which version of Java you're using) then press the OK buttons until all the dialogs are closed.Which was stolen from here: http://windowstipoftheday.blogspot.com/2005/10/setting-jar-file-association.html
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
}
}
There are many methods for running .jar file on windows. One of them is using the command prompt.
Steps :
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.
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.