Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to run compiled .classes from the command line

Tags:

java

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.
like image 739
Drake Avatar asked Feb 26 '23 15:02

Drake


1 Answers

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

  • java - the Java application launcher (manual for invoking java)
like image 125
Mark Peters Avatar answered Mar 08 '23 10:03

Mark Peters