6

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!

FR24601
  • 63
  • 1
  • 1
  • 4

6 Answers6

13

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.

hgrey
  • 3,033
  • 17
  • 21
  • 1
    I checked for that manifest and it's good, the correct main class is specified. – FR24601 Apr 13 '12 at 23:30
  • Can you try to run the it with `java -jar jarfilename` and post the exception here if you get one, together with contents of manifest file? – hgrey Apr 13 '12 at 23:40
  • what the... This works... I have no idea why. But this executes the jar normally... I'm out of i ideas – FR24601 Apr 13 '12 at 23:44
  • Probably your extension association got screwed. Try changing this to the same java you have in path. You can do it with explorer on windows. – hgrey Apr 14 '12 at 00:17
  • That's possible.. My pc crashes a lot lately, maybe this kinda messed the file extension up. Thanks, I'm going to try that tomorrow. – FR24601 Apr 14 '12 at 00:29
  • I set it to the Java.exe in the `path` directory for java, nothing changed. I also tried the jar.exe but that's the cmdline/batch thing so it didn't work either. Any other suggestions? – FR24601 Apr 14 '12 at 06:43
  • @ApertureT3CH: This is not a good move. Asking for a solution, and does not share it with us... – Makuna Jan 17 '19 at 06:27
2

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.

haskovec
  • 370
  • 3
  • 12
  • note: If the application use jars (e.g. lib/) and some are missing (or wrong version), this error may happen as well (regardless of the classpath). – lepe Apr 23 '19 at 04:31
1

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.

Mads
  • 724
  • 3
  • 10
1

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.

Micah Stairs
  • 235
  • 2
  • 11
Amila
  • 168
  • 1
  • 11
0

Suggest checking your manifest.

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

scorpdaddy
  • 303
  • 3
  • 14
0

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.

ihaxx
  • 1
  • 2