Adam_G
Adam_G

Reputation: 7879

Java: Issue running from command line

I'm writing a program in Eclipse, and running it from the command line. In an earlier version of the program, it took no arguements, and I could run it fine as > java foo. I've since added a couple of arguments, and need to run it as > java foo file1.txt file2.txt. When I run this, I get a java.lang.NoClassDefFoundError: error. Even when I include the classpath, i.e. > java foo file1.txt file2.txt -cp . it still doesn't work.

Could someone point me in the right direction?

EDIT Here is the complete stack trace

Exception in thread "main" java.lang.NoClassDefFoundError: edu/cuny/pausePred/TemplateToCharTestVector (wrong name: edu/cuny/pausepred/TemplateToCharTestVector)
        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)

Upvotes: 1

Views: 126

Answers (3)

Jim Garrison
Jim Garrison

Reputation: 86754

Exception in thread "main" java.lang.NoClassDefFoundError: 
  edu/cuny/pausePred/TemplateToCharTestVector 
  (wrong name: edu/cuny/pausepred/TemplateToCharTestVector)

Paths in java are case-sensitive. pausepred is not the same as pausePred

Upvotes: 1

L. Cornelius Dol
L. Cornelius Dol

Reputation: 64026

A common mistake for beginners in using Java is misunderstanding class names and the classpath.

A class name is a fully qualified thing which includes the package; the compiler lets you refer to a class using its base name, which is a convenience to keep programming sane. The actual name of your class is <package>.foo.

The classpath must include the root of any packages which you are using. So if your package for foo is edu.cuny.pausePred then the class name for foo is edu.cuny.pausePred.foo and the classpath must include the directory which contains edu, not the directory which contains foo.

You command line should be something like:

jave -cp the-directory-root-for-java-sources foo file1.txt file2.txt

noting that this assumes that the two data files are in the current directory.

As an aside, note that a class base name should be lead uppercase, so Foo, not foo.

Upvotes: 1

Joseph Farrugia
Joseph Farrugia

Reputation: 327

  • Write the full directory path then compile all classes
  • Write the full directory and run the class that contains the main() method

Upvotes: 1

Related Questions