在Java中:找不到主类
当我编写一个独立的客户端并尝试执行它时, 它给了我以下错误:
在 Windows 中运行:
Exception in thread "main" java.lang.NoClassDefFoundError: TestClient
Caused by: java.lang.ClassNotFoundException: TestClient
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: TestClient. Program will exit.
这是在 Linux 中
Exception in thread "main" java.lang.NoClassDefFoundError: TestClient (wrong name: com/tata/bayer/service/TestClient)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: TestClient. Program will exit.
现在我得到了
C:\Softwares\apache-tomcat-6.0.33\webapps\tkweb-ws-12.0\WEB-INF\classes\com>java com.tata.bayer.service.TestClient
Exception in thread "main" java.lang.NoClassDefFoundError: com/tata/bayer/service/TestClient
Caused by: java.lang.ClassNotFoundException: com.tata.bayer.service.TestClient
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: com.tata.bayer.service.TestClient Program will exit.
When I wrote a standalone client and tried to execute it,
it's giving me the following errors:
Running in Windows:
Exception in thread "main" java.lang.NoClassDefFoundError: TestClient
Caused by: java.lang.ClassNotFoundException: TestClient
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: TestClient. Program will exit.
This is in Linux
Exception in thread "main" java.lang.NoClassDefFoundError: TestClient (wrong name: com/tata/bayer/service/TestClient)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: TestClient. Program will exit.
Now i am getting
C:\Softwares\apache-tomcat-6.0.33\webapps\tkweb-ws-12.0\WEB-INF\classes\com>java com.tata.bayer.service.TestClient
Exception in thread "main" java.lang.NoClassDefFoundError: com/tata/bayer/service/TestClient
Caused by: java.lang.ClassNotFoundException: com.tata.bayer.service.TestClient
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: com.tata.bayer.service.TestClient Program will exit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您应该
从包含“com”的目录运行,并具有适当的目录层次结构,最终通向
TestClient.class
。您尚未表明此代码是您自己编译的还是别人提供给您的。如果您自己编译它,您通常应该组织源代码,以便它也反映包结构。因此,您可能会运行:
或者可能:
如果这没有帮助,请提供更多详细信息。
It looks like you should be running
from a directory containing "com", with an appropriate directory hierarchy eventually leading to
TestClient.class
.You haven't shown whether you're compiling this code yourself or whether it's been given to you. If you are compiling it yourself, you should usually organize the source code so that it reflects the package structure too. So you might run:
Or maybe:
If that doesn't help, please provide more details.
看起来您想使用 com/tata/bayer/service/TestClient 而不是 TestClient。尝试使用完整路径。
Looks like you want to use
com/tata/bayer/service/TestClient
and notTestClient
. Try using the full path.