为什么Java找不到主类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2138572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why Java could not find the main class?
提问by Roman
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:
我刚刚从http://java.sun.com/docs/books/tutorial/uiswing/examples/events/KeyEventDemoProject/src/events/KeyEventDemo.java复制了 Key-Listener 代码。我能够使用“javac”命令来兼容它。但是当我尝试执行编译后的代码(输入“java KeyEventDemo”)时,我看到一条大消息:
Could not find the main class: KeyEventDemo. Program will exit.
Yesterday I had a similar problemon 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".
昨天我在 Windows Vista 上遇到了类似的问题(现在我在 Ubuntu 上)。在 Windows 中,我能够通过键入“java -cp . ProgramName”或通过向环境变量“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".
在 Ubuntu 上,第一个解决方案不起作用。我的意思是,当我输入“java -cp . KeyEventDemo”时,我仍然有问题。此外,在 Ubuntu 上,我只需键入“java ProgramName”即可运行其他程序。
So, can anybody tell me what is special about this KeyEventDemo? Why it does not wont to work and how it can be solved?
那么,有人能告诉我这个 KeyEventDemo 有什么特别之处吗?为什么它不起作用以及如何解决?
采纳答案by silk
It is because the KeyEventclass is in package events.
这是因为KeyEvent类在包events 中。
You either have to remove the package events;line from source code, or compile it with:
您要么必须删除包事件;从源代码行,或编译它:
javac -d . KeyEventDemo.java
回答by Jerome
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":
该程序不在默认包中,而是在包“events”中:java -cp . events.KeyEventDemo
从包含文件夹“events”的目录中使用:
+work +events -KeyEventDemo.class
回答by Michael Borgwardt
The class KeyEventDemo
is in a package events
To run it, you must be in the parentfolder of the events
folder that contains the class, and run it using its fully qualified name, including the package:
类KeyEventDemo
在包中events
要运行它,您必须在包含该类的文件夹的父文件events
夹中,并使用其完全限定名称运行它,包括包:
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.
类路径必须包含文件夹(或 JAR),它是代表包的文件夹层次结构的根;当前文件夹(我相信)是自动包含的。
回答by Julio M. Oliveira
Perhaps you compile and run with diferent java version. This is common when you try to execute an example at eclipse.
也许您使用不同的 Java 版本编译和运行。当您尝试在 eclipse 中执行示例时,这很常见。