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)
Related
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.
I wrote a Java GUI program in Netbeans IDE 8.0.1 called SampleChat and used the 'clean and build' function to create a jar file.
I went to the 'dist' directory that Netbeans created and double-clicked the jar file it produced.
A cmd window opened with the following line:
"Error: could not find or load main class.
However, when I opened cmd, changed to the same 'dist' directory and typed the command:
java -jar "SampleChat.jar"
the program ran just fine.
Can I not use Netbeans to create jar files that execute on double-click?
I did not use any pre-existing jar files to complete the program, hence, there was no 'lib' directory or anything like that.
I intend to share programs from a USB stick so it is vital that the only requirement is a JVM, mouse and maybe some fingers to double-click it.
An awkward solution I came up with is to write a batch file that runs the program, but it requires the jar file to not be moved around - screw that.
So how do I make my programs double-click executable?
I guess you are missing to create a manifest file. Therefore it's unknown what your mainclass is. In your java command executed from a terminal you defined the mainclass to execute.
From the tasks you mentioned I guess you are using gradle to build your jar?
Here http://www.gradle.org/docs/current/userguide/java_plugin.html in chapter 23.14.1
an example of an manifest is given.
If you add attributes 'Main-Class': 'com.project.MainClass' it should work.
BR
I have created an executable jar file and have tried both the extract and package option for generated .jar. It creates it but when clicking on it nothing happens. I tried naming it the same as the class file or the project file too. This didn't happen before I don't know why it stoped working. It won't run even a simple hello world file. However a .jar file I made a while ago will run. I compiled with Eclipse, and running it with java -jar in cmd does work. I am running Windows 07. Any help would be greatly appreciated
Edit: tried uninstalling and reinstalling Java and Java JDK
you should check the application used to open the jar file. it should be java or javaw and should be called with -jar parameter. What Operating system are you using?
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
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.