Is it normal that “java Hello3” (with Hello3.class a java class) gives me:
Exception in thread "main" java.lang.NoClassDefFoundError: Hello3 (wrong name: pp/Hello3)
Hello3.class is the compiled binary of Hello3.java. This java file is declared to be under some package “pp”, but it is not put in the “pp” directory.
package pp;
public class Hello3 {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
My command java Hello3.java is done at the same directory as Hello3.java. What’s more, even the command “java -cp . Hello3” doesnot work! Maybe a problem from mine??
Since I got your confirmation from the following answers, I may draw a naive conclusion here: it’s too easy to make a .class that never works. Simply declare your .java under package XXX but do not put it in the directory XXX. IMHO, I think this can be used as an attack!
You indeed need to have Hello3.class in a directory named
pp. The directory containingppshould be in the classpath. Suppose this directory is calledclasses:The command to launch your Java program should thus be
or, on Windows