0

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!

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

That's why the OP used -cp . ;)
1

cd bin/edu/cuny && java util.ConvertTestVectors

You have to use the full package name.

5 Comments

Thanks. But that's what gives me the second error, the NoClassDefFoundError.
You said you got the second error from util/ConvertTestVectors, not util.ConvertTestVectors.
I changed the / to a . and it didn't matter.
What package is the class in then?
Got it! The other answer, which the user deleted, solved the problem. When I run java edu.cuny.util.ConvertTestVectors from bin it takes care of it. Thanks so much for your help, though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.