开发者

Why Java could not find the main class?

I have just copied Key-Listener code from http://java.sun.com/docs/books/tutorial/uiswing/examples/events/KeyEventDemoProject/src/events/KeyEventDemo.java. I was able to compalie it with the "javac" command. But when I try to execute the compiled code (typing "java KeyEventDemo") I have a large message in the end of which I see:

Could not find the main class: KeyEventDemo.  Program will exit.

Yesterday I had a similar problem on Windows Vista (now I am on Ubuntu). In the Windows I was able to solve the problem by typing "java -cp . ProgramName" or alternatively by adding new values ("." and "..")to the environment variable "classpath".

On Ubuntu the first solution does not work. I mean, when I type "java -cp . KeyEventDemo" I still have the problem. Moreover, on Ubuntu I was able to run other programs just typing "java ProgramName".

So, can anybody tell me what is special about this KeyEventDemo? Why it does not wont to work and how it c开发者_如何学Pythonan be solved?


The class KeyEventDemo is in a package events To run it, you must be in the parent folder of the events folder that contains the class, and run it using its fully qualified name, including the package:

java events.KeyEventDemo

The classpath must contain the folder (or JAR) that's the root of the folder hierarchy that represents the packages; the current folder is (I believe) included automatically.


This program is not in the default package, but in the package "events": use java -cp . events.KeyEventDemo from the directory containing the folder "events":

   +work
    +events
     -KeyEventDemo.class


It is because the KeyEvent class is in package events.

You either have to remove the package events; line from source code, or compile it with:

javac -d . KeyEventDemo.java


Perhaps you compile and run with diferent java version. This is common when you try to execute an example at eclipse.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜