0

If I double-click on my jar file, I get "Could not find the main class: HelloWorld. Program will exit."

When I run it from the command line as "java -jar HelloWorld.jar" it works fine.

It also runs fine from the Eclipse.

Other jar files, not written by me, run fine from the double click.

I have set CLASSPATH variable to "." and added the jdk path to the PATH variable.

I have checked that jars are associeted with the javaw.exe

"the jar that I can't run from the double click & the project folder with the source from the Eclipse "

"friend's jar that I can run from the double click"

To make a jar file I'm using Eclipse->File->Export->Java->Runnable JAR file->Current project's launch configuration & the first radio button->finish

Also, other people get the same error when trying to run my jar.

None
  • 37
  • 1
  • 7
  • 2
    Had you tried manually creating your jar file from the command prompt, by providing the manifest file defining your Main-Class in it. Try that. Regards – nIcE cOw Jan 07 '12 at 07:33
  • just did, got the same error when double clicked and it worked when I run it from the command promt – None Jan 07 '12 at 07:46
  • What's present in the jar file, and what does the manifest look like? Can you put it anywhere online so that we can take a look? – Jon Skeet Jan 07 '12 at 07:49
  • Jon, can you see the first link in my question with the jar-fle itself and the Eclips project folder with the source code inside? – None Jan 07 '12 at 07:53
  • [here is the link, if you can't see it in the actual question](http://dl.dropbox.com/u/20447449/HelloWorld.zip) – None Jan 07 '12 at 07:57
  • @Makc : It's working fine, but since your jar contains nothing other than system.out.println("HelloWorld!");, that's why there is not a way other than command prompt to tell if it's working or not. But since i am using JDK 1.7, i created it and it is running fine. Regards – nIcE cOw Jan 07 '12 at 08:03
  • @GagandeepBali : no, it's not fine. I have a windowed, JFrame, application that has the exact same problem: it creates a window and runs fine if I run it from the command prompt, but fails from the double click. And I think that HelloWorld app shouldn't get "Could not find the main class" error if double clicking on it ether way. – None Jan 07 '12 at 08:13
  • so, the fact that only thing it does is printing to the standart output does not mean that if you will run it from the double click it will give you a error "Could not find the main class". it should send the "Hello World!" to the standart output and terminate with no errors. – None Jan 07 '12 at 08:19
  • @Makc : I mean that jar file is working fine at my end, when i create it manually, since my version is 1.7u2. So i don't think it's an issue. Regards – nIcE cOw Jan 07 '12 at 08:26
  • @GagandeepBali : so, you don't get any errors when double clicking it? hm... I will update my 1.7.0_02 java then and see if that will help. btw, do you get the error running my jar, not one that you created and not from the command prompt of course. – None Jan 07 '12 at 08:36
  • huh, 7u2 is the 1.7.0_02. maybe there is something wrong with the Eclipse? because I creating jar from there. anyway, will reinstall jdk and Eclipse. – None Jan 07 '12 at 08:40
  • @Makc : Might be the version of jdk used by your eclipse is different from the one at your classpath variable. That can cause such errors though. Do check that. It's working without error at my end. Regards. – nIcE cOw Jan 07 '12 at 08:42
  • @GagandeepBali : checked, versions are the same. does [that](http://dl.dropbox.com/u/20447449/TouhouLiteDemo.jar) runs from the double click fine? it's the windowed application that I mentioned before and because of which created the question. – None Jan 07 '12 at 09:00
  • @Makc : Do this on your regedit, HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts, here find key .jar and delete it, now try to start your HelloWorld.jar file. that might can help too. Regards – nIcE cOw Jan 07 '12 at 10:28
  • not sure if that will help. I wrote that I already checked if the right program is used to run the jar-files, and other jars run perfectly... – None Jan 07 '12 at 13:21
  • @GagandeepBali : didn't help. alright, thank you for trying, don't want to bother you anymore, I will figure out this one day =) – None Jan 07 '12 at 13:28
  • @GagandeepBali : just set up VirtualBox with win7 x64 and installed jdk there - everything work just fine! I simply will take out whole this java thing with all of its' roots from my system and reinstall it - should help) thanks a lot! – None Jan 07 '12 at 14:11
  • @Makc : Your Welcome. Hope it works for you. Regards – nIcE cOw Jan 07 '12 at 17:59

1 Answers1

4

Creating a jar-file is not very difficult. But creating a startable jar-file needs more steps: create a manifest-file containing the start class, creating the target directory and archiving the files.

echo Main-Class: oata.HelloWorld>myManifest
md build\jar
jar cfm build\jar\HelloWorld.jar myManifest -C build\classes .
java -jar build\jar\HelloWorld.jar

Check: http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html

sbose
  • 1,791
  • 5
  • 24
  • 46
  • my class name is not "oata.HelloWorld", it is "HelloWorld", therefore get "Error: Could not find or load main class oata.Helloworld". – None Jan 07 '12 at 07:54
  • CHECK OUT THE link and make the changes accordingly. I recommend using command-line first to test your jar app. – sbose Jan 07 '12 at 08:02
  • How is that related to me? I'm creating jar file from the Eclipse, not from the command promt with the ant, is not like I'm doing something wrong in the command promt. – None Jan 07 '12 at 08:07
  • @Makc trust me it's easier to use the command prompt to do this – Jesus Ramos Jan 07 '12 at 10:12
  • @JesusRamos I'm not trusting you – None Jan 07 '12 at 10:23
  • @Makc That's cool don't trust me or the guy who tried to give you an answer that actually works either. We're just out to get you. – Jesus Ramos Jan 07 '12 at 10:35