When executing JAR, I get Java program completed with exit code 1 - java

I have a java classfile which has 'Hello World'.
I have created JAR file and Manifest file.
When I do a RUNJVA command with Classfile it works fine.
But when I try to use JAR file in RUNJVA, I don't see any output after the call.
Classpath is set properly.
I Just get 'Java program completed with exit code 1'
Thanks in Advance.

Make sure that you have a file META-INF/MANIFEST.MF in your jar file that contains at least something like Main-Class: full.qualified.name.of.MyClass and start it with java -cp {my-classpath-here} -jar myjarfile.jar

Related

Cannot run a .jar file

I'm having issues when I try to execute a .jar file. I always get the message from the command prompt "cannot find or load main class" every time I try to run the file I have just compiled.
I have tried the following(using the command prompt and saving the java code with Notepad++ and creating the manifest.txt file in the same folder where the .java code was saved):
javac classname.java
java cfm classname.jar manifest.txt classname.class
java -jar classname
I have also tried:
javac classname.java
java classname
And:
java packagename.classname
After compiling the .java file
I know there are hundreds of questions like this in the forums, but I have looked everywhere and I keep getting the same error. I just cannot run the .jar file even after I create it.
It's probably something I have missed but I am out of ideas.
Manifest:
Main-Class: classname
Let's see.
java -jar classname.jar
would work if your MANIFEST.MF calls out the correct class.
java -cp classname.jar my.package.MyClass
would work in any case.

Making a ImageJ .jar from command line

I have modified a ImageJ source code and i have compiled from command line, using the javac command, but Im trying to create a .jar file and I need help.
It is the first time that I'm trying to compile and create .jar from command line. When I created the .jar, I did the next command:
jar cf test.jar ImageJ/ij/*
Directory "ImageJ/ij/" contains the compiled code.
It doesn't fail but when I'm trying to execute the .jar It returns the error "Failed to load Main-Class manifest attribute from test.jar". When I execute a .jar I always do "java -jar file.jar" but, in this case, It doesn't work.
If I execute "java -cp test.jar ij.ImageJ" (ij.ImageJ is the Main Class) It works but I need to execute a macro in Batch mode and... It fails.
java -cp test.jar ij.ImageJ -batch ../MacrosIJ/helloWorld.ijm
Exception in thread "main" java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
...
If I create the .jar in Eclipse It works perfectly, Can someone explain what i am doing wrong?.
If someone can help I will be grateful. Thanks!!
The recommended way to build ImageJ from source is using Maven:
git clone https://github.com/imagej/ImageJA.git
mvn
cd target
java -cp ij-1.50.jar ij.ImageJ

Java running external jar without IDE?

I've written a java program that reads an excel file with jxl.jar. It's currently working, but I have to use cmd to run the program. Double clicking the jar file does not appear to be working. These are the commands I use to compile and run the code:
javac -classpath C:/workspace/jxl.jar:. main.java GUi.java
jar cvfm run.jar manifest.txt Main.class GUI.class GUI$1.class GUI$2.class GUI$3.class Main$1MyCustomTableCellRenderer.class Main$1YourTableCellRenderer.class Main$MyCustomTableCellRenderer.class
java -cp run.jar Main
I'm not really sure why it's any different from double clicking it. I've compiled the jxl file into the run.jar file so I don't understand why it doesn't work?
An Example directly from Java Tutorial on Adding Classes to the JAR File's Classpath as suggested by #MadProgrammer in his comment.
We want to load classes in MyUtils.jar into the class path for use in MyJar.jar. These two JAR files are in the same directory.
We first create a text file named Manifest.txt with the following contents:
Class-Path: MyUtils.jar
Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
We then create a JAR file named MyJar.jar by entering the following command:
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
This creates the JAR file with a manifest with the following contents:
Manifest-Version: 1.0
Class-Path: MyUtils.jar
Created-By: 1.7.0_06 (Oracle Corporation)
The classes in MyUtils.jar are now loaded into the class path when you run MyJar.jar.
Read more...
Try this in command prompt:
Run the below command if you bundled the jar with all libraries
java -jar MyJar.jar
If you need to add jars at the time of execution please add all the jars at command line, as follows;
java -cp /path/jxl.jar;myJar.jar Class-Name-Main

.jar error - could not find or load main class

I tried to put HelloWorld in a .jar file and running it, but it doesn't work. I created the java file and typed in the program, and then wrote in cmd:
javac HelloWorld.java
java HelloWorld
and it worked. Then I entered
echo Main-Class: HelloWorld >manifest.txt
jar cvfm HelloWorld.jar manifest.txt HelloWorld.class
and got the output
added manifest
adding: HelloWorld.class(in = 426) (out= 288)(deflated 32%)
I then entered
java -jar HelloWorld.jar
HelloWorld.jar
and the first line worked, while the second line gave me an error:
Error: Could not find or load main class path\HelloWorld.jar
which is the same output I got (in a rapidly closing window) when I tried to open it with the java.exe file in 64 bit jre7\bin, jdk1.7.0_51\bin, jdk1.7.0_51\jre\bin, as well as 32 bit jre7\bin. I've uninstalled and reinstalled both my jre and jdk and recreated my .class and .jar files, but the problem persists. I'm on win8.
Edit: I tried to do as aetheria suggested, but no luck. I put HelloWorld.java in path\com\stackoverflow\user\blrp, compiled it, and it worked by entering
java com.stackoverflow.user.blrp.HelloWorld
in path. I then created the manifest and jar by:
(echo Manifest-Version: 1.0
echo Class-Path: .
echo Main-Class: com.stackoverflow.user.blrp.HelloWorld) >manifest.txt
jar cvfm HelloWorld.jar manifest.txt com\stackoverflow\user\blrp\HelloWorld.class
and got the output
added manifest
adding: com/stackoverflow/user/blrp/HelloWorld.class(in = 454) (out= 312)(deflat
ed 31%)
but still, java -jar HelloWorld.jar worked and HelloWorld.jar didn't (same error). I also tried not doing the package thing, just the Class-Path in manifest, same result.
(Also, I solved the problem prior to asking the question by use of a .bat file, but it'd still be sweet to get that jar working.)
Thanks jbaliuka for the suggestion. I opened the registry editor (by typing regedit in cmd) and going to HKEY_CLASSES_ROOT > jarfile > shell > open > command, then opening (Default) and changing the value from
"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
to
"C:\Program Files\Java\jre7\bin\java.exe" -jar "%1" %*
(I just removed the w in javaw.exe.) After that you have to right click a jar -> open with -> choose default program -> navigate to your java folder and open \jre7\bin\java.exe (or any other java.exe file in you java folder). If it doesn't work, try switching to javaw.exe, open a jar file with it, then switch back.
I don't know anything about editing the registry except that it's dangerous, so you might wanna back it up before doing this (in the top bar, File>Export).
I found this question when I was looking for the answer to the above question. But in my case the issue was the use of an 'en dash' rather than a 'dash'. Check which dash you are using, it might be the wrong one. I hope this answer speeds up someone else's search, a comment like this could have saved me a bit of time.
You can always run this:
java -cp HelloWorld.jar HelloWorld
-cp HelloWorld.jar adds the jar to the classpath, then HelloWorld runs the class you wrote.
To create a runnable jar with a main class with no package, add Class-Path: . to the manifest:
Manifest-Version: 1.0
Class-Path: .
Main-Class: HelloWorld
I would advise using a package to give your class its own namespace. E.g.
package com.stackoverflow.user.blrp;
public class HelloWorld {
...
}
Had this problem couldn't find the answer so i went looking on other threads, I found that i was making my app with 1.8 but for some reason my jre was out dated even though i remember updating it. I downloaded the lastes jre 8 and the jar file runs perfectly. Hope this helps.

Error: Unable to access jarfile Click-The-Block.jar

I have made a simple game that I want to convert into a runnable jar so I can show others and launch it without Eclipse.
In Eclipse I:
Right clicked on Project
Export
Java > Exectuable Jar File
Launch Configuration: CTB (1) - Click The Block
It made a jar with a MANIFEST.MF containing:
Manifest-Version: 1.0
Class-Path: .
Main-Class: uk.co.robertmerriman.ctb.main.CTB
This was all extracted to my desktop in Click-The-Block.jar
When I double click, nothing happens.
When I type "java -jar Click-The-Block.jar" into CMD, I get the following error:
Error: Unable to access jarfile Click-The-Block.jar.
Make your jar file name without spaces locally, then write in cmd like the following:
java -jar YourjarFileNameWithoutSpaces.jar
Looks like an OS issue. If you are running windows 07 or vista try running the same in administrator mode.
Also make sure that when you are running the command
java -jar Click-The-Block.jar
You are running it from the directory where the jar is located.
if your java.exe is in c:\java\bin\java.exe and your Click-The-Block.jar is in c:\dev\
Then try out the following command
>
c:\java\bin\java -jar c:\dev\Click-The-Block.jar
Also try renaming your jar file with no cps letter, i know this will not have any effect but there's no harm in trying.
If you are still not able to execute the jar then try downloading an executable jar file from the net and see if you are able to run it using the standard java command. If you still get the same error which means the problem is not with your jar but somthing to do with how java is configured in the system. In that case you can go ahead and reinstall a fresh jdk and try al over again.
Hope it helps ...
I had the same issue with my executable jar generated by maven-shade-plugin "DCD-Desktop-1.0.jar" , I removed the dashes and renamed it to "dcd.jar" and it worked fine .

Categories

Resources