FR24601
FR24601

Reputation: 63

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 :)

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!

Upvotes: 6

Views: 66534

Answers (6)

Amila Jayawardhana
Amila Jayawardhana

Reputation: 168

The manifest 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.

Upvotes: 1

ihaxx
ihaxx

Reputation: 1

Also add a classpath attribute to the manifest and put a dot for the value. This will tell the v.m to look at the current directory for classes.

Upvotes: 0

haskovec
haskovec

Reputation: 370

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.

Upvotes: 2

scorpdaddy
scorpdaddy

Reputation: 303

Suggest checking your manifest.

http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

Upvotes: 0

hgrey
hgrey

Reputation: 3093

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.

Upvotes: 13

Mads
Mads

Reputation: 724

Normally you would have to specify which class within the jar file has the main method. If you check the bat file which works, you'd probably see what you are missing in order to run it.

Upvotes: 1

Related Questions