简体   繁体   中英

(java)Could not find the main class

My code is compiled on OSX and it works fine(compiler version 1.6). But when I copy it to a remote ubuntu. I can not run it.

When I run

java TPCC.class

I got exception like this.(By the way TPCC.class is in default package)

Exception in thread "main" java.lang.NoClassDefFoundError: TPCC/class
Caused by: java.lang.ClassNotFoundException: TPCC.class
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    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: TPCC.class.  Program will exit.

And my java version on ubuntu:

java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b06)
Java HotSpot(TM) Client VM (build 17.0-b16, mixed mode, sharing)

It's also 1.6 and I think it fits the compiled binary. How can I fix this problem?

The problem is in the way you run the program. Compiled java called

ClassName.java

will be ClassName.class. You should run it like

"java ClassName"

. You shouldn't run it as

"java ClassName.class"

What happens here is this. When you run it as java ClassName.class, JVM try to look for ClassName called "class" in the package called "ClassName" that does not exist. So it will throw NoClassDefFoundError.

This is an invocation problem as the error shows: java should be used and not the filename of the class. The error message tries to say you this by stating that "TPCC/class" is not found.

(sorry for first adding comment, used the wrong field)

调用java时指定类路径:

java -cp <path-to-jar> TPCC

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