Reputation: 7879
I'm learning how to run Java from the command line, and keep running into the same issue. The main()
method I'm running is in bin/edu/cuny/util/ConvertTestVectors.class
.
I set my directory to bin/cuny/
. When I run > java -cp . ConvertTestVectors
I get:
Error: Could not find or load main class ConvertTestVectors
When I run > java -cp . util/ConvertTestVectors
I get:
Exception in thread "main" java.lang.NoClassDefFoundError: util/ConvertTestVectors (wrong name: edu/cuny/util/ConvertTestVectors)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)
Could someone point me in the right direction? Thanks!
Upvotes: 0
Views: 360
Reputation: 2824
To run a main
method of some java class you have to specify the fully qualified name of this class in the command line. For example, if you have in your source:
package edu.cuny.util;
class ConvertTestVectors {
...
public static void main(String [] args) {
...
}
...
}
then to run this main
method you should use
java edu.cuny.util.ConvertTestVectors
If your package name is different, you should change the class name in command line accordingly.
Also, there is a CLASSPATH
variable, which determines the location where java looks for classes. It should contain the directory where the root of your java packages in located (the parent directory of edu
is case of the previous example). If it contains .
then you can just change directory to that root directory and run java command.
The parts of a fully qualified class name are separated by dots (.
) so you should not use slashes (/
) in class name
Upvotes: 2
Reputation: 4595
cd bin/edu/cuny && java util.ConvertTestVectors
You have to use the full package name.
Upvotes: 1