简体   繁体   中英

In Java: Could not find the main class

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM