关于包的问题
// A short package demonstration.
package BookPack; //该文件属于BookPack包
class Book { //因此Book属于BookPace
private String title;
private String author;
private int pubDate;
Book(String t,String a,int d) {
title=t;
author=a;
pubDate=d;
}
void show() {
System.out.println(title);
System.out.println(author);
System.out.println(pubDate);
System.out.println();
}
}
class BookDmeo { //BookDemo也属于BookPace
public static void main(String[] args) {
Book books[]=new Book[5];
books[0]=new Book( "Java: A Beginner 's Guide ", "Schild ",2005);
books[1]=new Book( "Java : The Complete Reference ", "Schildt ",2005);
books[2]=new Book( "The Art of Java ", "Schildt and Holmes ",2003);
books[3]=new Book( "Red Storm Rising ", "Clancy ",1986);
books[4]=new Book( "On the Road ", "Kerouac ",1955);
for(int i=0;i <books.length;i++) books[i].show();
}
}
有人说编译一下会自动创建包名同名的文件夹.可我编译了没自动创建啊。
自己创建了一个和包相同的文件夹按照书上说的把这个原文件和两个编译后的.class文件都保存在包文件夹里,然后退到包的上一目录运行但运行出错.
C:\Program Files\Java\java lesson> java BookPack.BookDemo
Exception in thread "main "
java.lang.NoClassDefFoundError: BookPack/BookDemo
我的JDK是1.6的。 请高手们解释下啊:我实在是郁闷了.
------解决方案--------------------你要是用命令行的形式编译的话
javac -d 路径 文件名.java
这种形式才会创建包
------解决方案--------------------可能是环境变量的问题