简体   繁体   中英

Unable to run compiled .classes from the command line

I am having some issues running my compiled java code from the command line. I have written it and compiled it using the IntelliJ IDE (where everything runs fine if done within the IDE), but wish to now run it from the command line.

Compiling from the command like (using javac) also works fine, but running (with java) does not.

I am almost certain this is a classpath issue but cannot seem to fix it. From my searching prior to posting this I found a post telling me to run the "set PATH=\\%PATH\\%;"C:\\Program Files\\Java\\jdk1.6.0_21\\bin" command and then try running java. I have also tried various arguements I have found for -cp and -classpath. The error is :

Exception in thread "main" java.lang.NoClassDefFoundError: Share/class
Caused by: java.lang.ClassNotFoundException: Share.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: Share.class.  Program will exit.

You're doing:

java -cp ... Share.class

Do

java -cp ... Share

Or if it's in a package

java -cp ... path.to.Share

You should not be supplying the class file as an argument, you should be supplying the fully qualified class name.

If your class is in the current directory and uses the default (empty) package, it will just be

java -cp . Share

or

java Share

The classpath is not used to point to the java executable, it's used to point to the various directories/jar files which contain your class files (at the root of the package structure).

See also

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