11

Below is what I tried in linux terminal: compiled Test.java, run Test.class, and got an error. Then, I tried the same command with "-classpath ." option and "-cp ." option, but also failed.

/testpackage$ cat Test.java 
package testpackage;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("May I take your order?");
    }

}
/testpackage$ javac Test.java 
/testpackage$ java testpackage.Test
Exception in thread "main" java.lang.NoClassDefFoundError: testpackage/Test
Caused by: java.lang.ClassNotFoundException: testpackage.Test
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: testpackage.Test. Program will exit.

/testpackage$ java -cp . testpackage.Test
Exception in thread "main" java.lang.NoClassDefFoundError: testpackage/Test
Caused by: java.lang.ClassNotFoundException: testpackage.Test
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: testpackage.Test. Program will exit.

/testpackage$ java -classpath . testpackage.Test
Exception in thread "main" java.lang.NoClassDefFoundError: testpackage/Test
Caused by: java.lang.ClassNotFoundException: testpackage.Test
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: testpackage.Test. Program will exit.
/testpackage$ 

But if I remove the package "testpackage" and recompile the source code, the resulting class file is executed well.

/testpackage$ cat Test.java
//package testpackage;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("May I take your order?");
    }

}
/testpackage$ javac Test.java
/testpackage$ java Test
May I take your order?
/testpackage$

What's wrong with my code, or execution command? Please help me. Thank you. :)

David Johns
  • 1,201
  • 5
  • 15
  • 34

3 Answers3

18

You need to run the commands from one directory higher.

A class in package foo must live in directory foo. Package foo.bar must be in directory foo/bar and so on.

So, your structure should have a file called /path/to/code/testpackage/Test.java and your working directory should be /path/to/code. You can then run:

javac testpackage/Test.java

java -cp . testpackage.Test

and everything should work.

Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
  • Thanks! I didn't know this simple rule, and spent about a few hours searching for the problem. :-p Thanks again! – David Johns Mar 27 '11 at 02:14
  • 1
    Ok, apparently I experienced an anomaly. After removing all *.class files that may have been there from before converting into a package, then trying again, it works. Something tripped up javac. – trusktr Oct 02 '13 at 08:53
  • Would the downvoter care to mention what they disagree with in this answer? – Cameron Skinner Oct 03 '13 at 02:57
0

When you have a package name, the fully resolved class name is testpackage.Test. That's what java.exe expects to see.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

You can't work with "testpackage" as your current directory. You would need to run it as

java testpackage.Test

from the directory of which "testpackage" is a subdirectory.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186