我有两个java文件分别位于不同的包中,其中Student.java在d:\ceshi\studentinfo, StudentTest.java在d:\ceshi\test中。其中StudentTest用到了studentinfo中的类Student.但是在test目录编译的时候总是提示软件包studentinfo不存在。(Student中已有语句package studentinfo;)请各位高手指教是什么原因?
StudentTest代码如下:package test;import junit.framework.*;
import studentinfo.*;public class StudentTest extends TestCase {
public void testCreate() {
final String firstStudentName = "Jane Doe";
Student firstStudent = new Student(firstStudentName);
assertEquals(firstStudentName, firstStudent.getName()); final String secondStudentName = "Joe Blow";
Student secondStudent = new Student(secondStudentName);
assertEquals(secondStudentName, secondStudent.getName());
}
}
StudentTest代码如下:package test;import junit.framework.*;
import studentinfo.*;public class StudentTest extends TestCase {
public void testCreate() {
final String firstStudentName = "Jane Doe";
Student firstStudent = new Student(firstStudentName);
assertEquals(firstStudentName, firstStudent.getName()); final String secondStudentName = "Joe Blow";
Student secondStudent = new Student(secondStudentName);
assertEquals(secondStudentName, secondStudent.getName());
}
}
d:\ceshi\studentinfo
d:\ceshi\test都放到 CLASSPATH里面就可以了如果是经常用的项目,可以考虑放到环境变量里面,请参考 http://www.java2000.net/viewthread.jsp?tid=387
另:你是不是在命令行自己编译啊!?为何不换一个工具呢,比如Eclipse
Test.java在包com.biao.normal中
project directory: JavaDemo
JavaDemo/com/biao/normal/Test.java
JavaDemo/pack.jar
Test.java要用到pack.jar中的com/biao/pack/PackageDemo.java
怎样写classpath和javac, java来运行Test.java?
JavaDemo/runandcompile.bat:
set classpath=.;./pack.jar
javac com/biao/normal/Test.java
java com/biao/normal/Test
其中最关键的一点是set classpath=.;./pack.jar中的".;"是必不可少的,少了就非常可能程序能编译成功,但是就是运行不了,原因不明。
在Eclipse中引入外部包:
project/properties/librarys/add extenal jars...可以参考一下上面的这种方法.