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

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

问题描述

我正在尝试使用命令提示符和以下命令编译我的 java 文件check4PrimeTest.java":

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

javac -classpath .:junit.jar check4PrimeTest.java

javac -classpath .:junit.jar check4PrimeTest.java

我收到以下错误:

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

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

我不知道为什么会出现这个错误,因为我的程序中有 import junit.framework.*.

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

下面是我的代码:

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:

您的 javac 目标除了源和目标目录 - 它不添加任何类路径条目;你需要为相应的 JUnit jar 文件添加一个条目.查看 javac 任务文档了解更多详情.您可能想要指定路径JUnit 作为类路径属性、嵌套元素或对在别处声明的路径.

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.

  • javac 任务文档
  • 来源:使用 Ant 运行 JUnit 测试的问题蚀.初学者问题

    <代码>提示>javac -classpath .;$JUNIT_HOMEjunit4.x.x.jar test.java

    JUNIT 安装(来自此处):

    JUNIT INSTALLATION (from here):

    视窗

    要在 Windows 上安装 JUnit,请按照下列步骤操作:

    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)

    Unix (bash)

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

    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 不存在";在“进口x"处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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