There are two pieces of package (package) Time1.class in the folder D:\Java\testImport\com\deitel\javafp\ch08, Then I created a new class: Time1packagetest.java, which contains the main method, and references package time1.class. And put it in D:\Java\testImport2
Then I execute D:\JAVA\TESTIMPORT2>JAVAC-CP D:\Java\testImport without prompting for an error
Then execute D:\JAVA\TESTIMPORT2>JAVA-CP D:\Java\testImport time1packagetest
Hint Error
Code:
Exception in thread ' main ' java.lang.NoClassDefFoundError:Time1PackageTest
caused by: Java.lang.ClassNotFoundException:Time1PackageTest at
java.net.urlclassloader$1.run (Unknown Source)
at Java.security.AccessController.doPrivileged (Native method) at
Java.net.URLClassLoader.findClass (Unknown SOURCE) at
Java.lang.ClassLoader.loadClass (Unknown source) at
Sun.misc.launcher$appclassloader.loadclass ( Unknown source) at
Java.lang.ClassLoader.loadClass (Unknown source) could not find the
main class: Time1packagetest. Program would exit.
So try D:\java\testimport2>java time1packagetest
Code:
Exception in thread ' main ' java.lang.noclassdeffounderror:com/deitel/javafp/ch0
8/time1
at Time1packagetest.main (time1packagetest.java:9)
caused by:java.lang.ClassNotFoundException: Com.deitel.javafp.ch08.Time1 at
java.net.urlclassloader$1.run (Unknown Source)
at Java.security.AccessController.doPrivileged (Native method) at
Java.net.URLClassLoader.findClass (Unknown SOURCE) at
Java.lang.ClassLoader.loadClass (Unknown source) at
Sun.misc.launcher$appclassloader.loadclass ( Unknown source) at
Java.lang.ClassLoader.loadClass (Unknown source)
... 1 more
Time1code:
[Code]
Package com.deitel.javafp.ch08;
public class Time1 {
private int hour;
private int minute;
private int second;
public void settime (int h, int m, int s)
{
Hour = ((h>=0 && H <) h:0);
Minute = ((m>=0 && m<60)? m:0);
Second = ((s>=0 && s<60)? s:0);
}
Public String touniversalstring ()
{
Return String.Format ("%02d:%02d:%02d", hour, minute, second);
}
Public String to1string ()
{
Return String.Format ("%02d:%02d:%02d%s", (hour==0| | hour==12) 12:hour%12, minute, second, hour>=12? " PM ":" AM ");
}
}
[/code]
Time1packagetest Code:
[Code]
Import com.deitel.javafp.ch08.Time1;
public class Time1packagetest {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Time1 time1 = new Time1 ();
System.out.println (Time1.touniversalstring ());
}
}
[/code]
The following is a foreigner's answer:
Originally Posted by oszc then I execute D:\JAVA\TESTIMPORT2>JAVA-CP D:\Java\testImport time1packagetest
The error occur:
Code:
Exception in thread ' main ' java.lang.NoClassDefFoundError:Time1PackageTest
caused by: Java.lang.ClassNotFoundException:Time1PackageTest at
java.net.urlclassloader$1.run (Unknown Source)
at Java.security.AccessController.doPrivileged (Native method) at
Java.net.URLClassLoader.findClass (Unknown SOURCE) at
Java.lang.ClassLoader.loadClass (Unknown source) at
Sun.misc.launcher$appclassloader.loadclass ( Unknown source) at
Java.lang.ClassLoader.loadClass (Unknown source) could not find the
main class: Time1packagetest. Program would exit.
This is because your directory for the Com.deitel.javafp.ch08.Time1 class in the class path, and don ' t include The current directory (ie the Time1packagetest Class). So it fails the Time1packagetest class.
Originally Posted by
OSZCThen I execute D:\java\testimport2>java time1packagetest
Error again:
Code:
Exception in thread ' main ' java.lang.noclassdeffounderror:com/deitel/javafp/ch0
8/time1
at Time1packagetest.main (time1packagetest.java:9)
caused by:java.lang.ClassNotFoundException: Com.deitel.javafp.ch08.Time1 at
java.net.urlclassloader$1.run (Unknown Source)
at Java.security.AccessController.doPrivileged (Native method) at
Java.net.URLClassLoader.findClass (Unknown SOURCE) at
Java.lang.ClassLoader.loadClass (Unknown source) at
Sun.misc.launcher$appclassloader.loadclass ( Unknown source) at
Java.lang.ClassLoader.loadClass (Unknown source)
... 1 more
In this case you are are running without any defined classpath as it defaults to current directory (.). This means it can find time1packagetest, since it's in the current directory, but now it can ' t find the Com.deitel.javafp . ch08. Time1 class.
You need both:
-CP.;D: \ Java\testimport
My own summary.
Contains two paths, separated by the-CP, with the sequence of two paths that do not affect the proper execution of the program, that is, the location of the referenced package file (package) and the reference file (import) are listed, and the order does not affect the program's operation. The commands for the full run are:
D:\JAVA\TESTIMPORT2>JAVA-CP D:\Java\testImport; Time1packagetest