0

Possible Duplicate:
NoClassDefFoundError , Java

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

I'm really lost. I have a directory foo/boo/ and in that I have Prog.class along with some other dot-class files. foo is a subdirectory of the current directory where I run the command from. I compiled it fine, and in my foo/boo/ directory I've checked to make sure that there is indeed a "Prog.class" along with all the other dot-class files I need. There is a main method in my Prog class, and I'm pretty sure this problem has nothing to do with my source code (although it could of course) because I was able to run Prog fine in eclipse, just not from my terminal (ssh-ing onto another machine).

Could someone try to decipher what all of that jumble might mean? I don't really understand. Thank you very much.

Community
  • 1
  • 1
Tim
  • 4,295
  • 9
  • 37
  • 49

3 Answers3

2
$ java -h
Usage: java [-options] class [args...]
           (to execute a class)

You are trying to specify the filesystem path to your class file, this is not possible. You need to specify the classpath correctly, so the class can be found by the classloader.

The classpath is a set of paths, where the java classloader looks for the classes to load. So specify the correct folders after the -cp parameter and it will be fine.

burna
  • 2,932
  • 18
  • 27
1

Start from outside of foo/boo/Prog, i.e. having current directory being the parent of foo and run as @grtt1 said.

SAMPLE THAT WORKS

suzan@nebulla:~/Test_Java_01$ ls
foo
suzan@nebulla:~/Test_Java_01$ ls foo
boo
suzan@nebulla:~/Test_Java_01$ ls foo/boo
Prog.class  Prog.java
suzan@nebulla:~/Test_Java_01$ cat foo/boo/Prog.java
package foo.boo;

public class Prog {
    public static void main(String[] args) {
        System.out.println("Hello world");
    }
}
suzan@nebulla:~/Test_Java_01$ java foo.boo.Prog
Hello world
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
  • I did just that and still got the same error. I was also running from the parent of foo/boo/Prog like you said in my original example. – Tim Feb 12 '12 at 16:16
  • do you have `package foo.boo;` line in the beginning of the `Prog.java` file? – Suzan Cioc Feb 12 '12 at 16:18
  • ah, do you have foo/boo/Prog as a DIRECTORY? i.e. do you have full path to file as foo/boo/Prog/Prog.class? – Suzan Cioc Feb 12 '12 at 16:23
  • yes I have package foo.boo; in the beginning of Prog.java, and no about the second question (I worded it confusingly in my post originally, now I've edited it to hopefully make it more clear). I only have foo/boo/ as a directory, and Prog.class resides in there. – Tim Feb 12 '12 at 16:31
  • Then... probably check operating system file access rights? Probably JVM can't see file while you can? Are you on Linux? – Suzan Cioc Feb 12 '12 at 16:39
  • Yea the system I'm remote accessing into is running Linux. I'm using an ssh terminal. – Tim Feb 12 '12 at 16:42
  • I am out :) Look the sample above to see how it works at my system and compare with yours. – Suzan Cioc Feb 12 '12 at 16:49
  • May be recompile a file, probably it is outdated? – Suzan Cioc Feb 12 '12 at 16:50
1

I'm probably misunderstanding (and don't have enough to just comment on questions) but are you saying you have a Prog directory, with a Prog.class in there? Wouldn't that make it foo.boo.Prog.Prog ?

matt freake
  • 4,877
  • 4
  • 27
  • 56
  • oh woops, I made that confusing the way I worded it, sorry. No, I don't have a prog directory, I have a directory foo/boo/ and in that I have Prog.class along with some other dot-class files. foo is a subdirectory of the current directory that I run the command from. – Tim Feb 12 '12 at 16:27
  • then check package directive; it determines logical location of the class; if you have no package directive you should run from the same directory as Prog.class and saying no path – Suzan Cioc Feb 12 '12 at 16:34