Unable to run jar file in Windows by double clicking - java

I am trying to run a very simple program by double clicking my Jar file with javaw.
It runs fine from Netbeans AND from the command line.
My registry path to javaw:
"C:\Program Files\Java\jre1.8.0_102\bin\javaw.exe" -jar "%1" %*
What is going wrong here? How can I make this work when I double click the Jar?

Make sure your JAR file is an executable JAR; I'm not entirely sure how to export an executable JAR from NetBeans (I use Eclipse), but this article seems to explain it pretty well.
Make sure javaw.exe is the default program for running JARs. This question addresses that.
There's also a Java Tutorial on making and using JARs, and several other StackOverflow questions about it.

Related

Can't execute a .jar packaged application

I'm having troubles with my jar files here. I'm kind of a beginner in Java and I wanted to make a .jar file in order to share my little first program with a friend of mine.
It's a 2 classes program, they depend on each other and work together when compiling and running. I created a MANIFEST.MF file and created a .jar using the command line.
All seem to work, when I type: java -jar myApp.jar it works. But when I double-click my myApp.jar file, I expect a terminal window to open and print out some things and nothing happens...
(I'm on Mac OS X Yosemite)

Executable jar howto

I have the code on github,
https://github.com/Quillion/Game
And I would like to make it executable. jarIt.bat is the thing that makes the jar.
path C:\Program Files\Java\jdk1.7.0_04\bin;%path%
cls
javac *java
echo Main-Class: Main> manifest.txt
jar cvfm Main.jar manifest.txt *.class
java -jar Main.jar
del *.class
I can execute it on my computer using the run.bat file,
path C:\Program Files\Java\jdk1.7.0_04\bin;%path%
cls
java -jar Main.jar
but if I transfer it to another computer or Linux, I still have to use the commands to execute it. How does one create a jar file so that it can be double clicked and executed?
I am somewhat bad at the whole jar explanation, so if someone can even find a good link to share that explains everything I will be really grateful.
I have tried reading numerous explanations, but none explain how to do it properly. I know that I can accomplish this using eclipse, but I would like to learn the know how and to understand how to make one myself without any help of IDE.
Unfortunately, not all OS support running the jar by a double click ootb. You'd be off best by generating scripts for all environments that you need to support. You can do that with Maven and the plugin that does the scripting for you:
http://mojo.codehaus.org/appassembler/appassembler-maven-plugin/
Btw, your jar should already run by double click in a modern windows installation! You did it quite right.

How to run Java applications without using an IDE or the command prompt

This is more curiosity than a problem:
I was recently wondering if there was a way to run compiled Java applications without using the cmd or an IDE such as Eclipse. I use Eclipse, but it isn't very useful if you want to run the program independently. Can you save Java files in Windows Explorer so you can create a shortcut for them? If so, how? Is there some sort of special extension to the file? I've heard of .JAR files, but I'm not sure what they are. Can anyone tell me how to do it?
.JAR files are archives containing - amongst other things - your compiled classes and a manifest file. You may set the main entry point of your application in that manifest. See Setting an Application's Entry Point.
Normally if you double click a jar file in windows it will be opened by javaw.exe -jar <yourFile.jar>. javaw.exe will lookup the manifest and try start the main class defined there.
create the jar file for java application using following syntax jar -cvf .jar . then use javaw.exe -jar

How can an executable .jar file be run without the command prompt?

I have a very basic .jar file that successfully runs, though I can only seem to run it by doing one of two things:
Using the command prompt and entering a command such as java -jar test.jar
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?
Double click on it
use something to wrap the JAR file into a common exe (e.g. http://launch4j.sourceforge.net/)
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.
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.
My JARs are associated with 7Zip. To run an executable JAR, right click the file name and select Open With > Java.
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.

Java create executable cmd line program(windows)

I'm new to java and have recently created a stress testing application to test server configurations. Its very simple and all is done within cmd line. I used eclipse to create the jar file and that seems to have worked fine.
The problem that I am running into is making this executable. If I use java -jar in windows cmd line to execute the program, it runs fine. However, I need to be able to run it by "double clicking" the jar file(right now I click on it and nothing happens) or create a .exe which defeats the purpose of java, but this will only be used in windows.
When I click on the jar now nothing happens, but when using the java - jar in cmd it works. Not all of the computers have java in the cmd line, but have it installed. I'm not sure why a cmd window doesnt pop when clicking on the jar?
Again I'm new and any help is much appreciated!!
Create a sortcut icon that will do java -jar yourFile.jar
In windows, you can associate the jar file with the JRE jar runner. Take a look at this post, which explains your options pretty well.
Make a bat file for Windows. You can do this by the following:
#echo off
java -jar YourJarName.jar
Save this in a text file with the .bat extension.
It should run the JAR once double clicked if the JAR file is in the same directory as the .bat file. Otherwise you will have to navigate to the JAR file relative to where the .bat file is located.
You said you didn't want an exe but not sure if this will be ok for you. It shouldn't be a problem for someone to click the .bat file first and will work in all cases under Windows.
Hope this helps.
If you want to get really awesome with it and have it show up in your Task Manager with an app.exe naming and handle any startup options, you should read into JNI. JNI will allow you to wrap the starting and stopping of a Java app using a windows executable and it is actually very simple to implement.
If you want something as simple as a windows exe launcher, there are also tools out there such as Launch4j will create exe wrappers for you.

Categories

Resources