简体   繁体   中英

“Couldn't find the main class” on HelloWorld

I started with Java yesterday. I installed Eclipse, JDK and JRE and I did the HelloWorld tutorial. I followed the tutorial and it runs correctly on Eclipse. Now I want to transform it on a runnable .jar file, so I go to Export, i create it, and it apperas on the desktop.

So when I try to doubleclick it (opening it with javaw), it returns the busy cursor icon and nothing happens. On the other hand, if I open it form cmd java -jar HelloWorld.jar , it works properly. Is it normal? What can I do?

Just like Pietu1998 said, javaw.exe will actually run your program but not open a console window. This is because javaw.exe is meant to run GUIs which obviously does not need a command prompt. See this question for what is the difference between java.exe and javaw.exe .

Even

javaw -jar HelloWorld.jar

on the command prompt will look like nothing happens. Redirect the output to a file and you'll see that your program actually ran:

javaw -jar HelloWorld.jar > HelloWorld.txt

The output of your program will be in the file HelloWorld.txt .

Usually all java programs do not rely on just double clicking to run but come with a kind of a launcher which prepares the environment - notably setting up the classpath - for your app an then runs it. It is platform dependent and if the platform allow to run it using double click then it will be run that way. Such a launcher could be just a simple script: .bat , .cmd , .sh or an executable .exe . For example, if you look at the folder where you have installed eclipse then you'll find a eclipse.exe file (assuming your platform is windows). Creating such a launcher is also easy and is explained in this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM