Jar file won't run via command prompt or double-click - java

I recently finished a project which works as it is supposed to in my Eclipse IDE as both multiple files and as a single file.
Eclipse exports the jar file and only makes noise about the warnings.
When I go to run the jar file with a double-click, the cursor seems to flash to a hourglass for less than a second and then nothing. When I try to run the jar file from the command line with java -jar myJarFile.jar the command prompt window seems to wait a second and then brings the file path line and cursor with no errors and no other messages.
I have double checked both my Path variable and that I have the latest version of Java installed.
Any suggestions?

Check this thread: Failing to execute the jar file using java -jar command
Basically, what you need to do is when you run the jar, you need to specify the class to be executed in the command line, like so:
java -cp test.jar com.app.ClassName

Did you just make a jar file, or a runnable jar file? If you only did the former then it will not execute on a double click. So it's Right Click the project in Eclipse -> Export -> Runnable JAR File -> Choose the appropriate Launch Configuration -> Choose where you want it to go.

Confirm that your application code is in the jar file:
jar tf myJarFile.jar
Confirm that your application's main class is listed in the Main-Class attribute in the manifest, and that any other jar files needed by your application are listed in the Class-Path attribute:
jar xf myJarFile.jar META-INF/MANIFEST.MF
cat META-INF/MANIFEST.MF
Try running with verbose logging of classes being loaded; this should show if some necessary class can't be found:
java -verbose:class -jar myJarFile.jar

Related

How to create an executable jar file which can be distributed or packaged?

I am using getdown to create a means to update a java application.
When I have completed this tutorial, I tested if it works on command line as below:
% java -jar c:/downloads/getdown-X.Y.jar c:/netBeans/getdown/src
Thankfully, this works and launches the application. Great.
How do I make a jar file and distribute this?
I tried to make a jar file on this project but it didn't work, this project does not run. When I run this getdown-X.Y.jar on command line.
I think it still using the same file which I created before c:/netBeans/getdown/src. Eventually, it is failing to execute since it is missing the jar file. So, how to make this project into a jar file and distribute it.
I am not sure what OS you are working on.You can do this by creating an executable jar file. Please follow the steps here:
If you want to create a jar file with additional file. Here in below, if you want to create a jar file of imagine src.class with additonal text file with it which is readme.txt
c:\patel\projects\netbeans\getdown\src.class
c:\patel\projects\readme.txt
Run this command: jar -cvfm src.jar readme.txt netbeans\getdown\*.class
which is: c:\patel\projects\jar -cvfm src.jar readme.txt netbeans\getdown\*.class
Now your executable jar file is ready. To run this jar file:
run this on command prompt: java -jar src.jar

How can I execute a runnable .jar file by double clicking on it?

I have .jar file generated from three sources:
Netbeans
Eclipse (using export)
From cmd (jar cvmf MANIFEST.MF output.jar input1.class input2.class ...)
In the last process, I created a blank MANIFEST.MF and then the command.
When the file got archived in .jar, I opened it with WinZip and edited the MANIFEST.MF. I added Main-Class:input1.
All three sources run fine on cmd through java -jar output.jar. But nothing happens on double-clicking them.
I have tried this solution as well. I also have set the default for .jar as javaw.exe but still does not run.
How to fix this?

Java run jar file & include external jar

Is there a way to pass an external jar file when running a .jar application?
I'm trying to run my jar like this:
java -jar myJar.jar -cp externalJar.jar
The jar file executes fine but I want to look for classes in the external file. I can't include the other classes into my jar, because I want to be able to put any jar file in the same folder as my Jar file and look for classes in there.
The only way to do this right now is by running my app like this:
java -cp myJar.jar;externalJar.jar MainClass
I do not want to explicitly enter the path to my MainClass to run it's main method.
It really seems that the -cp option is completely ignored when you use the -jar option. At least this is what you can read on the manpage of java about the -jar option:
Execute a program encapsulated in a JAR file. The first argument is
the name of a JAR file instead of a startup class name. In order for
this option to work, the manifest of the JAR file must contain a line
of the form Main-Class: classname. Here, classname identifies the
class having the public static void main(String[] args) method that
serves as your application's starting point. See the Jar tool
reference page and the Jar trail of the Java Tutorial for information
about working with Jar files and Jar-file manifests.
When you use this option, the JAR file is the source of all user classes, and other user
class path settings are ignored.
Note that JAR files that can be run with the "java -jar" option can
have their execute permissions set so they can be run without using
"java -jar". Refer to Java Archive (JAR) Files.
I found this in this blogpost here: http://happygiraffe.net/blog/2009/04/30/java-jar-blats-your-classpath/
Did you try adding a specific folder to the classpath during startup and then add your jar file to the folder at later point ?

Creating a exc jar file in eclipse ?

so I created a jar file, clicked on export and etc
but when I double click it doesnt do anything
I dont see my classes in the folder
all i see is
junit
org.hamcrest.core_1.1.0.v2009050107100…
what am i doing wrong ?
If your jar is a valid executable
then this should work
java -jar your.jar
Jar files don't aren't always run by double-clicking.
In order to run the jar file, run the command java -jar yourjarfile.jar while in the same directory as the jar file. This assumes that your PATH system variable is set properly.
If you receive an error such as "Failed to load Main-Class manifest attribute from yourjarfile.jar" it means that there is no main class defined in your jar file, and the java interpreter doesn't know where to start.
You can make sure that a main class is specified in Eclipse by exporting as a "Runnable JAR file" and making sure you select a launch configuration that you use to run your program.
In order to list the contents of the jar file, run the command jar tvf yourjarfile.jar while in the same directory as the jar file.
In order to extract the contents of the jar file, run the command jar xvf yourjarfile.jar while in the same directory as the jar file.

Why has it failed to load main-class manifest attribute from a JAR file?

I have created a JAR file in this way jar cf jar-file input-files. Now, I'm trying to run it. Running it does not work (jre command is not found):
jre -cp app.jar MainClass
This does not work either:
java -jar main.jar
(Failed to load Main-Class manifest attribute from main.jar).
I also found out that
To run an application packaged as a
JAR file (version 1.2 -- requires
Main-Class manifest header)
What is the "Main-Class manifest header"? How do I create it and where do I put it?
I'm not sure I believe your symptoms:
If the jre command isn't found, then running jre -cp app.jar should give the same error
Just adding a JAR file to the classpath shouldn't give the error you're seeing
I'd expect you to see this error if you run:
java -jar app.jar
The Main-Class header needs to be in the manifest for the JAR file - this is metadata about things like other required libraries. See the Sun documentation for how to create an appropriate manifest. Basically you need to create a text file which includes a line like this:
Main-Class: MainClass
Then run
jar cfm app.jar manifest.txt *.class
set the classpath and compile
javac -classpath "C:\Program Files\Java\jdk1.6.0_updateVersion\tools.jar" yourApp.java
create manifest.txt
Main-Class: yourApp newline
create yourApp.jar
jar cvf0m yourApp.jar manifest.txt yourApp.class
run yourApp.jar
java -jar yourApp.jar
You can run with:
java -cp .;app.jar package.MainClass
It works for me if there is no manifest in the JAR file.
I got this error, and it was because I had the arguments in the wrong order:
CORRECT
java maui.main.Examples tagging -jar maui-1.0.jar
WRONG
java -jar maui-1.0.jar maui.main.Examples tagging
The easiest way to be sure that you have created the runnable JAR file correctly, with the appropriate manifest file, is to use Eclipse to build it for you. In your Eclipse project, you basically just select File/Export from the menu, and follow the prompts.
That way, you can be sure that your JAR file is correct and will know to look elsewhere if there is still an issue. The process is described in full in FAQ How do I create an executable JAR file for a stand-alone SWT program?.
I was getting the same error when i ran:
jar cvfm test.jar Test.class Manifest.txt
What resolved it was this:
jar cvfm test.jar Manifest.txt Test.class
My manifest has the entry point as given in oracle docs (make sure there is a new line character at the end of the file):
Main-Class: Test
Try
java -cp .:mail-1.4.1.jar JavaxMailHTML
no need to have manifest file.
I discovered that I was also having this error in NetBeans.
I hope the following is helpful.
Make sure that when you go to Project Configuration you set the main class you intend for running.
Do a Build or Clean Build
Place the jar file where you wish and try: java -jar "YourProject.jar" again at the command line.
This was the problem I was getting because I had other "test" programs I was using in NetBeans and I had to make sure the Main Class under the Run portion of the Project configuration was set correctly.
many blessings,
John P
I faced the same problem. This unix command is not able to find the main class. This is because the runtime and compile time JDK versions are different. Make the jar through eclipse after changing the java compiler version. The following link helped me.
http://crunchify.com/exception-in-thread-main-java-lang-unsupportedclassversionerror-comcrunchifymain-unsupported-major-minor-version-51-0/
Try running the jar created after this step and then execute it
If your class path is fully specified in manifest,
maybe you need the last version of java runtime environment.
My problem fixed when i reinstalled the jre 8.
If you using eclipse, try below:
1. Right click on the project -> select Export
2. Select Runnable Jar file in the select an export destination
3. Enter jar's name and Select "Package required ... " (second radio button) -> Finish
Hope this helps...!

Categories