GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes classpath and package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "classpath and package" Watch "classpath and package" New topic
Author

classpath and package

Xinbo Cheng
Greenhorn

Joined: Jan 05, 2001
Posts: 15
Hi, I'm have some trouble with package. What did I do wrong?
I have two files called Test1.java and Test2.java, both files belongs to package xinbo; they are sit in C:\CoreJavaBook\xinbo
package name: xinbo
My path: c:\jdk1.2\bin
classpath: c:\CoreJavaBook
Now I can compile Test1 with no trouble and the .class files
showed up in C:\CoreJavaBook\xinbo
when I try to run by java Test1, I get an exception.
----------------------------------------------------------------
C:\CoreJavaBook\xinbo>java Test1
Exception in thread "main" java.lang.NoClassDefFoundError: Test1 (wrong name: xinbo/Test1)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:403)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:10
1)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:248)
at java.net.URLClassLoader.access$1(URLClassLoader.java:216)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:191)
at java.lang.ClassLoader.loadClass(ClassLoader.java:280)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:275)
at java.lang.ClassLoader.loadClass(ClassLoader.java:237)
-----------------------------------------------------------------
The files are like (note Test1.java and Test2.java are separate files):
package xinbo;
public class Test1
{
public static void main(String args[])
{
System.out.println("OK!!");
Test2.printLine("Package compilation!");
}
}

package xinbo;
public class Test2
{
public static void main(String args[])
{
System.out.println("OK!!");

}
static void printLine(String msg)
{
System.out.println(msg);
}
}
Anand Krishna
Greenhorn

Joined: Jan 14, 2001
Posts: 5
You are trying to execute the class from the package subfolder and that is why you are getting this error. When you compile a java class with package information, it keeps this in the class header information in the produced byte code.
You should execute you class from C:\CoreJavaBook rather than C:\CoreJavaBook\xinbo with following comand line:
java xinbo.Test1
Hope this helps.
Xinbo Cheng
Greenhorn

Joined: Jan 05, 2001
Posts: 15
Thanks a lot, Anand! It works perfectly with java xinbo.Test1
I really appreciate your help!
Xinbo
 
GeeCON Prague 2014
 
subject: classpath and package