Java 编译 ant build.xml 时出现错误“包不存在”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19208718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:03:44  来源:igfitidea点击:

Getting Error "Package doesn't exists" while compiling ant build.xml

javaseleniumanttestng

提问by Swati Jha

I am new to ANT build and getting error "package org.testng.annotations does not exist" and "package org.openqa.selenium does not exist" on compiling ant build.xml.

我是 ANT 构建的新手,在编译 ant build.xml 时收到错误“包 org.testng.annotations 不存在”和“包 org.openqa.selenium 不存在”。

Here is build.xml

这是 build.xml

<project name="TestNGProject" default="usage" basedir=".">
<!-- ===============Initialize property============================ -->
<property environment="env"/>
<property name="ws.home" value="${basedir}"/
<property name="ws.jars" value="C:\Users\Documents\selenium-2.35.0\AllJars"/>
<property name="test.dest" value="${ws.home}/build"/>
<property name="test.src" value="${ws.home}/src"/>
<property name="ng.results" value="test-output"/>

<target name="setClassPath" unless="test.classpath">
   <path id="classpath_jars">
      <fileset dir="${ws.jars}" includes="*.jar"/>
   </path>
   <pathconvert pathsep="."
    property="test.classpath"
    refid="classpath_jars"/>
</target>

<target name="init" depends="setClassPath">
   <tstamp>
       <format property="start.time" pattern="MM/dd/yyyy hh:mm aa"/>
    </tstamp>

   <condition property="ANT"
       value="${env.ANT_HOME}/bin/ant.bat"
       else="${env.ANT_HOME}/bin/ant">
       <os family="windows" />
   </condition>

   <!--taskdef name="testng" classpath="${test.classpath}"
        classname="org.testng.TestNGAntTask" /-->

</target>

<!--all-->
<target name="all">
</target>

<!--clean-->
<target name="clean">
   <delete dir="${test.dest}"/>
</target>

<!--compile-->
<target name="compile" depends="init, clean">
   <delete includeemptydirs="true" quiet="true">
      <fileset dir="${test.dest}" includes="**/*"/>
   </delete>
   <echo message= "making dir..."/>
       <mkdir dir="${test.dest}"/>
   <echo message="classpath---------: ${test.classpath}"/>
   <echo message="compiling..."/>

   <javac
     includeantruntime="false"
     destdir="${test.dest}"
     srcdir="${test.src}"
     classpath="${test.classpath}"
    >
  </javac>
</target>

<!--build-->
<target name="build" depends ="init">
</target>

<target name="run" depends ="compile">

<testng classpath="${test.classpath}:${test.dest}" suitename="suite">
     <xmlfileset dir ="${ws.home}" includes="testng.xml"/>
</testng>

</target>

<target name="usage">
<echo>
   ant run will execute test case
</echo>
</target>

<path id="test.c">
   <fileset dir="${ws.jars}" includes="*.jar"/>
</path>

</project>

and here is the error list

这是错误列表

[javac] Compiling 2 source files to C:\Users\workspace\TestNGProject\build
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:3: error: package org.testng.annotations does not exist
[javac] import org.testng.annotations.Test;
[javac]                              ^
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:4: error: package org.testng.annotations does not exist
[javac] import org.testng.annotations.DataProvider;
[javac]                              ^
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:5: error: package org.testng.annotations does not exist
[javac] import org.testng.annotations.BeforeClass;
[javac]                              ^
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:6: error: package org.testng.annotations does not exist
[javac] import org.testng.annotations.AfterClass;
[javac]                              ^
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:7: error: package org.testng does not exist
[javac] import static org.testng.AssertJUnit.assertEquals;
[javac]                         ^
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:7: error: static import only from classes and interfaces
[javac] import static org.testng.AssertJUnit.assertEquals;
[javac] ^
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:9: error: package org.openqa.selenium does not exist
[javac] import org.openqa.selenium.By;
[javac]                           ^
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:10: error: package org.openqa.selenium does not exist
[javac] import org.openqa.selenium.WebDriver;
[javac]                           ^
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:11: error: package org.openqa.selenium does not exist
[javac] import org.openqa.selenium.WebElement;
[javac]                           ^
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:12: error: package org.openqa.selenium.firefox does not exist
[javac] import org.openqa.selenium.firefox.FirefoxDriver;
[javac]                                   ^
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:16: error: cannot find symbol
[javac]     private WebDriver driver;
[javac]             ^
[javac]   symbol:   class WebDriver
[javac]   location: class NewTestNG
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:17: error: cannot find symbol
[javac]       private WebElement FahrenheitInput;
[javac]               ^
[javac]   symbol:   class WebElement
[javac]   location: class NewTestNG
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:18: error: cannot find symbol
[javac]       private WebElement CelsiusInput;
[javac]               ^
[javac]   symbol:   class WebElement
[javac]   location: class NewTestNG
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:19: error: cannot find symbol
[javac]       private WebElement ConvertButton;
[javac]               ^
[javac]   symbol:   class WebElement
[javac]   location: class NewTestNG
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\sample.java
:3: error: package org.testng.annotations does not exist
[javac] import org.testng.annotations.Test;
[javac]                              ^
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:20: error: cannot find symbol
[javac]       @BeforeClass
[javac]        ^
[javac]   symbol:   class BeforeClass
[javac]   location: class NewTestNG
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:29: error: cannot find symbol
[javac]       @DataProvider(name = "CelsToFahrDP")
[javac]        ^
[javac]   symbol:   class DataProvider
[javac]   location: class NewTestNG
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:33: error: cannot find symbol
[javac]       @Test(dataProvider = "CelsToFahrDP")
[javac]        ^
[javac]   symbol:   class Test
[javac]   location: class NewTestNG
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:40: error: cannot find symbol
[javac]       @AfterClass
[javac]        ^
[javac]   symbol:   class AfterClass
[javac]   location: class NewTestNG
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\sample.java
:6: error: cannot find symbol
[javac]   @Test
[javac]    ^
[javac]   symbol:   class Test
[javac]   location: class sample
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:22: error: cannot find symbol
[javac]          driver = new FirefoxDriver();
[javac]                       ^
[javac]   symbol:   class FirefoxDriver
[javac]   location: class NewTestNG
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:25: error: cannot find symbol
[javac]          CelsiusInput = driver.findElement(By.xpath("//form[2]/cente
r[2]/input"));
[javac]                                            ^
[javac]   symbol:   variable By
[javac]   location: class NewTestNG
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:26: error: cannot find symbol
[javac]          FahrenheitInput = driver.findElement(By.xpath("//form[2]/ce
nter[2]/input[2]"));
[javac]                                               ^
[javac]   symbol:   variable By
[javac]   location: class NewTestNG
[javac] C:\Users\workspace\TestNGProject\src\TestNGPackage\NewTestNG.j
ava:27: error: cannot find symbol
[javac]          ConvertButton = driver.findElement(By.xpath("//form[2]/cent
er[2]/input[3]"));
[javac]                                             ^
[javac]   symbol:   variable By
[javac]   location: class NewTestNG
[javac] 24 errors

I have also set the JAVA_HOME C:\Program Files\Java\jdk1.7.0_25\bin and ANT_HOME C:\Users\Documents\selenium-2.35.0\apache-ant-1.9.2

我还设置了 JAVA_HOME C:\Program Files\Java\jdk1.7.0_25\bin 和 ANT_HOME C:\Users\Documents\selenium-2.35.0\apache-ant-1.9.2

Can anyone tell me how to get rid of this error.

谁能告诉我如何摆脱这个错误。

采纳答案by VGR

I believe the problem is here:

我相信问题出在这里:

<pathconvert pathsep="."
    property="test.classpath"
    refid="classpath_jars"/>

I'm not aware of any environment where "."is a valid path separator. The result is that ${test.classpath}is not a valid path. That is why your TestNG and Selenium classes are not accessible by the compiler.

我不知道有任何"."有效路径分隔符的环境。结果是这${test.classpath}不是一个有效的路径。这就是编译器无法访问您的 TestNG 和 Selenium 类的原因。

You might as well make test.classpathidentical to classpath_jars.

您也可以test.classpathclasspath_jars.

(I would have spotted this much sooner if your XML were properly indented.)

(如果您的 XML 正确缩进,我会更早地发现这一点。)