.jar error - could not find or load main class - java

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.

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.

What is the proper entry point, when compiling java to jar?

So, when I write my java file:
public class Program
{
public static void main(String[] args)
{
System.out.println("Serious business logic.");
}
}
Then in windows cmd, I compile this way:
javac Program.java
jar cfe Program.jar Program Program.class
java -jar Program.jar
It's fine, and the result is:
"Serious business logic."
When in Netbeans I create a Project, it adds this line:
package program;
And I can not compile in cmd, only inside the IDE.
I've tried manifest.txt, UTF8 encoding without BOM, plus linebreak at the and of file.
Manifest.txt:
Main-Class: program.Program
"jar cvfm Program.jar Manifest.txt Program.class"
and without manifest.txt, just in cmd program.Program
"jar cfe Program.jar program.Program Program.class"
When I tried to run:
java -jar Program.jar
it results in:
"Error: Could not find or load main class program.Program"
I've already checked the following websites:
http://www.skylit.com/javamethods/faqs/createjar.html
https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
and haven't got any idea how to do. Could you please help me?
How do I compile with package keyword? What is the proper entry point?
Thanks!
(ps jre1.8.0_91 ; jdk1.8.0_66 should I use same 32 or 64 bit for both of jre and jdk?)
Make sure when you are compiling your program as a JAR, Program.class is inside of a folder called program. The package keyword that Netbeans has added at the beginning of your script is telling the executable that it is inside of a folder called program. If you are just adding the class file without making sure it is in the correct package (folder), it will not run properly because it does not know where to find it. Your command should be changed to:
jar cvfm Program.jar Manifest.txt program
where program is a folder containing Program.class. Your manifest may be left alone but also needs to be included with compilation.

Launch executable jar file

I have a problem with an executable jar file. When I launch my jar file with Windows command line, all is good :
java -jar updater.jar
When I double click on my jar file, nothing happens.
This problem appeared when I added graphic support to my application (org.eclipse.jface and org.eclipse.swt).
My MANIFEST.MF :
Main-Class: Updater
Class-Path: libs/
Thank you in advance
It will not open with double click, because that would be like you typing javaw updater.jar in cmd.
The thing you need to do is write a wrapper for the thing. Maybe VisualBasic as it is easy to understand. You can write an updater in it and use AmazonAWS to host your update files. That's what I do, and I reccomend you to do that too.
Solution (thx a_horse_with_no_name for clues) :
With an application which use platform dependent component (like org.eclipse.swt.win32.win32.x86_64_3.102.1.v20130827-2048.jar), set ftype jarfile to JRE that correspond to your architecture (32 or 64 bit) in commands line prompt (run as administrator) :
ftype jarfile="C:\Program Files (x86)\Java\jre7\bin\javaw.exe" -jar "%1" %*
or
ftype jarfile="C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
All works fine now, thank you !

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 .

Java: Could not find the main class. Program will exit

Works on my PC. Doesn't work on the 2 others I tried: they get the entitled error.
Manifest.txt:
Manifest-Version: 1.0
Class-Path: .
Main-Class: core.Initiator
^ extra line at the end.
Hierarchy:
Project.jar
core
Initiator
...
images
...
META-INF
MANIFEST.MF
CreateJar.bat:
cd bin
jar cvfm ..\Project.jar ..\Manifest.txt *
pause
Run.bat:
#echo off
start javaw -jar Project.jar
From the comments in the actual question. This could be because of the relative paths that you are using.
Also, does your jar file contain the images or is it just lying somewhere in the workspace? This would again suggest that the relative paths may be wrong.
The computers with the error needed to update their JDK.

Categories

Resources