当我编写独立客户端并尝试执行它时, 它给了我以下错误:
在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.
答案 0 :(得分:3)
看起来你应该正在运行
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
如果这没有帮助,请提供更多详细信息。
答案 1 :(得分:0)
您希望使用com/tata/bayer/service/TestClient
而不是TestClient
。尝试使用完整路径。