在Java中:找不到主类

发布于 2024-12-18 05:38:20 字数 2504 浏览 1 评论 0原文

当我编写一个独立的客户端并尝试执行它时, 它给了我以下错误:

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回梦里 2024-12-25 05:38:20

看起来您应该

java com.tata.bayer.service.TestClient

从包含“com”的目录运行,并具有适当的目录层次结构,最终通向 TestClient.class

您尚未表明此代码是您自己编译的还是别人提供给您的。如果您自己编译它,您通常应该组织源代码,以便它也反映包结构。因此,您可能会运行:

javac com/tata/bayer/service/*.java
java com.tata.bayer.service.TestClient

或者可能:

# Keep source and class files separate...
javac -d bin com/tata/bayer/service/*.java
java -classpath bin com.tata.bayer.service.TestClient

如果这没有帮助,请提供更多详细信息。

It looks like you should be running

java com.tata.bayer.service.TestClient

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:

javac com/tata/bayer/service/*.java
java com.tata.bayer.service.TestClient

Or maybe:

# Keep source and class files separate...
javac -d bin com/tata/bayer/service/*.java
java -classpath bin com.tata.bayer.service.TestClient

If that doesn't help, please provide more details.

热血少△年 2024-12-25 05:38:20

看起来您想使用 com/tata/bayer/service/TestClient 而不是 TestClient。尝试使用完整路径。

Looks like you want to use com/tata/bayer/service/TestClient and not TestClient. Try using the full path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文