javac错误:“包x不存在”在“import x” [英] javac error: "package x does not exist" at "import x"

查看:313
本文介绍了javac错误:“包x不存在”在“import x”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用命令提示符用以下命令编译我的java文件'check4PrimeTest.java':


javac - classpath。:junit.jar check4PrimeTest.java


我得到以下错误:


错误:package junit.framework不存在import junit.framework。*;


我不知道为什么我得到这个错误,因为我有进口junit.framework。*在我的程序。



下面是我的代码:

  package check4prime; 
// check4PrimeTest.java

//导入
import junit.framework。*;

public class check4PrimeTest extends TestCase {

//初始化一个要使用的类。
private check4Prime check4prime = new check4Prime();

//构造函数
public check4PrimeTest(String name){
super(name);
}

//主入口点
public static void main(String [] args){
System.out.println(Starting test ... );
junit.textui.TestRunner.run(suite());
System.out.println(Test finished ...);
} // end main()

//测试用例1
public void testCheckPrime_true(){
assertTrue(check4prime.primeCheck(3));
}

//测试用例2,3
public void testCheckPrime_false(){
assertFalse(check4prime.primeCheck(0));
assertFalse(check4prime.primeCheck(1000));
}

//测试用例7
public void testCheck4Prime_checkArgs_char_input(){
try {
String [] args = new String [1]
args [0] =r;
check4prime.checkArgs(args);
fail(should raise an Exception。);
} catch(异常成功){
//成功测试
}
} // end testCheck4Prime_checkArgs_char_input()

//测试用例5
public void testCheck4Prime_checkArgs_above_upper_bound(){
try {
String [] args = new String [1];
args [0] =10001;
check4prime.checkArgs(args);
fail(should raise an Exception。);
} catch(异常成功){
//成功测试
}
} // end testCheck4Prime_checkArgs_upper_bound()

//测试用例4
public void testCheck4Prime_checkArgs_neg_input(){
try {
String [] args = new String [1];
args [0] = - 1;
check4prime.checkArgs(args);
fail(should raise an Exception。);
} catch(异常成功){
//成功测试
}
} // end testCheck4Prime_checkArgs_neg_input()

//测试用例6
public void testCheck4Prime_checkArgs_2_inputs(){
try {
String [] args = new String [2];
args [0] =5;
args [1] =99;
check4prime.checkArgs(args);
fail(should raise an Exception。);
} catch(异常成功){
//成功测试
}
} // end testCheck4Prime_checkArgs_2_inputs

//测试用例8
public void testCheck4Prime_checkArgs_0_inputs(){
try {
String [] args = new String [0];
check4prime.checkArgs(args);
fail(should raise an Exception。);
} catch(异常成功){
//成功测试
}
} // end testCheck4Prime_checkArgs_0_inputs

// JUnit必需的方法。
public static Test suite(){
TestSuite suite = new TestSuite(check4PrimeTest.class);
return suite;
} // end suite()

} // end check4PrimeTest


解决方案

您收到此错误是因为您尝试导入包而不告诉您系统包装所在的位置。下面是关于告诉你的系统包在哪里的说明:


你的javac目标不指定任何东西,除了源和
目标目录 - 它不添加任何类路径条目;您将需要
为相应的JUnit jar文件添加条目。有关更多详细信息,请参阅javac任务
文档。您可能需要将
JUnit的路径指定为类路径属性,嵌套元素或对其他地方声明的
路径的引用。






资料来源:在Eclipse中使用Ant运行JUnit测试的问题。初级问题



提示> javac -classpath。; $ JUNIT_HOME\junit4.xxjar test.java



编辑:JUNIT INSTALLATION =http://junit.sourceforge.net/doc/faq/faq.htm#started =nofollow>此处):



pre> 1。将junit.zip分发文件解压缩到称为%JUNIT_HOME%的目录。

2.将JUnit添加到类路径(在命令行shell中键入以下内容):`set CLASSPATH =%CLASSPATH%;%JUNIT_HOME%\junit.jar`



Unix(bash)



要在Unix上安装JUnit,请按照下列步骤:

  1。将junit.zip分发文件解压缩到称为$ JUNIT_HOME的目录。 

2.将JUnit添加到类路径(在终端中键入以下内容):

`export CLASSPATH = $ CLASSPATH:$ JUNIT_HOME / junit.jar`



I'm trying to compile my java file 'check4PrimeTest.java' using the command prompt with the following command:

javac -classpath .:junit.jar check4PrimeTest.java

I get the following error:

error: package junit.framework does not exist import junit.framework.*;

I'm not sure why i get this error as i have import junit.framework.* in my program.

below is my code:

package check4prime;
// check4PrimeTest.java

//Imports 
import junit.framework.*;

public class check4PrimeTest extends TestCase {

    //Initialize a class to work with. 
    private check4Prime check4prime = new check4Prime();

    //constructor 
    public check4PrimeTest (String name) { 
        super(name);
    }

    //Main entry point 
    public static void main(String[] args) {
        System.out.println("Starting test...");
        junit.textui.TestRunner.run(suite());
        System.out.println("Test finished...");
    } // end main() 

    //Test case 1 
    public void testCheckPrime_true() {
        assertTrue(check4prime.primeCheck(3));
    }

    //Test cases 2,3 
    public void testCheckPrime_false() {
        assertFalse(check4prime.primeCheck(0));
        assertFalse(check4prime.primeCheck(1000));
    }

    //Test case 7 
    public void testCheck4Prime_checkArgs_char_input() { 
        try {
            String [] args= new String[1];
            args[0]="r";
            check4prime.checkArgs(args);
            fail("Should raise an Exception.");
        } catch (Exception success) { 
            //successful test
        }
    } //end testCheck4Prime_checkArgs_char_input() 

    //Test case 5 
    public void testCheck4Prime_checkArgs_above_upper_bound() {
        try { 
            String [] args= new String[1];
            args[0]="10001";
            check4prime.checkArgs(args);
            fail("Should raise an Exception.");
        } catch (Exception success) { 
            //successful test
        }
    } // end testCheck4Prime_checkArgs_upper_bound() 

    //Test case 4 
    public void testCheck4Prime_checkArgs_neg_input() {
        try { 
            String [] args= new String[1];
            args[0]="-1";
            check4prime.checkArgs(args);
            fail("Should raise an Exception.");
        } catch (Exception success) { 
            //successful test
        }
    } // end testCheck4Prime_checkArgs_neg_input()

    //Test case 6
    public void testCheck4Prime_checkArgs_2_inputs() {
        try { 
            String [] args= new String[2];
            args[0]="5";
            args[1]="99";
            check4prime.checkArgs(args);
            fail("Should raise an Exception.");
         } catch (Exception success) {
            //successful test 
         } 
    } // end testCheck4Prime_checkArgs_2_inputs 

    //Test case 8 
    public void testCheck4Prime_checkArgs_0_inputs() {
        try { 
            String [] args= new String[0];
            check4prime.checkArgs(args);
            fail("Should raise an Exception.");
        } catch (Exception success) { 
            //successful test
        } 
    } // end testCheck4Prime_checkArgs_0_inputs 

    //JUnit required method. 
    public static Test suite() { 
        TestSuite suite = new TestSuite(check4PrimeTest.class);
        return suite;
    } //end suite() 

} //end check4PrimeTest

解决方案

You are getting this error because you're trying to import a package without telling your system where the package is located. Here are instructions on telling your system where the package is located:

Your javac target doesn't specify anything apart from the source and target directory - it doesn't add any classpath entries; you'll need to add an entry for the appropriate JUnit jar file. See the javac task documentation for more details. You may want to specify the path to JUnit as a classpath attribute, a nested element, or a reference to a path declared elsewhere.

Source: problem running JUnit tests with Ant in Eclipse. Beginner question

prompt> javac -classpath .;$JUNIT_HOME\junit4.x.x.jar test.java

EDIT: JUNIT INSTALLATION (from here):

Windows

To install JUnit on Windows, follow these steps:

1. Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%.

2. Add JUnit to the classpath (type the following into a command line shell): `set CLASSPATH=%CLASSPATH%;%JUNIT_HOME%\junit.jar`

Unix (bash)

To install JUnit on Unix, follow these steps:

1. Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.

2. Add JUnit to the classpath (type the following into terminal):

`export CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit.jar`

这篇关于javac错误:“包x不存在”在“import x”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆