Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAR file: Could not find main class

Okay, I have a strange problem. I wanted to run one of my programs as a .jar file, but when I open it by double-clicking it, I get an error message like "Could not find main class, program is shutting down". I'm pretty sure I did everything right, the jar should work afaik. I also tried other programs, it's the same with every single one. (I'm creating the .jar's through BlueJ) There is no problem when I run them through a .bat . And here comes the strangest thing of all: The .jar's have worked some time ago (one or two months I guess), and I don't remember doing anything different. It's the same BlueJ-Version. Okay, maybe Java updated and something got messed up... I googled, but I couldn't find a solution. (some people seem to have a similar problem, and it seems to be only them who can't run their .jar's; they uploaded them and other people say the .jar's run fine.) What could be the problem? How can I solve it?

I'd really appreciate some help here. Thank you :)

ApertureT3CH

EDIT: okay guys, you're making me unsure here. Imma check the manifest again, at this unholy time ( 1:34 am ) :P

EDIT2: This is my MANIFEST.MF Manifest-Version: 1.0 Class-Path: Main-Class: LocalChatClientGUI [empty line] [empty line]

The Main class is correct.

EDIT3: Thanks to hgrey: There is nothing wrong with the jar. I can run it from a bat file, which actually should not be different from double-clicking the jar, right? Yet I get the error when clicking it, and it works fine through the bat.

EDIT4: I finally solved the problem. I re-installed the JRE and now it works, although I can't see any version differences.

Thanks to everyone!

like image 226
FR24601 Avatar asked Apr 13 '12 23:04

FR24601


2 Answers

When java runs the jar it will look at manifest.mf file inside the jar's META-INF directory and will read MainClass attribute in there. If it is not specified you will get the message you describe. So, try to include correct manifest.mf into the jar. Generation of manifest.mf is supported by popular java build tools like Ant or Maven.

like image 80
hgrey Avatar answered Oct 16 '22 15:10

hgrey


Does your program use any external jar files? If so you need to set the classpath attribute inside of the manifest file as well as the main-class.

like image 39
haskovec Avatar answered Oct 16 '22 14:10

haskovec