Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ClassnotfoundException:在为JUNIT执行Ant脚本时?_Java_Ant_Junit - Fatal编程技术网

Java ClassnotfoundException:在为JUNIT执行Ant脚本时?

Java ClassnotfoundException:在为JUNIT执行Ant脚本时?,java,ant,junit,Java,Ant,Junit,我在为JUNIT类执行ant脚本时遇到以下错误。我的类编译成功。有什么问题?为什么找不到类文件?。请导游 My类路径环境变量: C:\Program Files\Java\jdk1.6.0_34\lib;D:\HR\jboss-5.0.0.GA\lib\endorsed;D:\HR\jboss-5.0.0.GA\lib;D:\HR\jboss-5.0.0.GA\client;D:\HR\apache-ant-1.6.5\lib;C:\Program Files\Java\jdk1.6.0_34\

我在为JUNIT类执行ant脚本时遇到以下错误。我的类编译成功。有什么问题?为什么找不到类文件?。请导游

My类路径环境变量:

C:\Program Files\Java\jdk1.6.0_34\lib;D:\HR\jboss-5.0.0.GA\lib\endorsed;D:\HR\jboss-5.0.0.GA\lib;D:\HR\jboss-5.0.0.GA\client;D:\HR\apache-ant-1.6.5\lib;C:\Program Files\Java\jdk1.6.0_34\bin
错误:

junit-report:
   [delete] Deleting directory D:\MyProject\folder\src\html-report
    [mkdir] Created dir: D:\MyProject\Test\src\html-report
    [mkdir] Created dir: D:\MyProject\Test\src\html-report\Junit
    [junit] Testsuite: test.com.folder.service.ejb.session.SL_ServiceTest
class
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec

    [junit]     Caused an ERROR
    [junit] test.com.folder.service.ejb.session.SL_ServiceTest
    [junit] java.lang.ClassNotFoundException: test.com.folder.service.ejb.sessio
.SL_ServiceTest.class
    [junit]     at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    [junit]     at java.security.AccessController.doPrivileged(Native Method)
    [junit]     at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    [junit]     at java.lang.Class.forName0(Native Method)
    [junit]     at java.lang.Class.forName(Class.java:247)

    [junit] Test test.com.folder.service.ejb.session.SL_ServiceTest
FAILED

BUILD SUCCESSFUL
Total time: 0 seconds
build.xml

<project name="MyProject" default="junit-report" basedir=".">
  <!-- Sets the property variables to point to respective directories -->
  <property name="junit-xml-dir" value="${basedir}/test-output/junitreports"/>
  <property name="report-dir" value="${basedir}/html-report" />

  <!-- Ant target to generate html report -->
  <target name="junit-report">
    <!-- Delete and recreate the html report directories -->
    <delete dir="${report-dir}" failonerror="false"/>
    <mkdir dir="${report-dir}" />
    <mkdir dir="${report-dir}/Junit" />
    <!-- Ant task to generate the html report.
    todir - Directory to generate the output reports

    fileset - Directory to look for the junit xml reports.

    report - defines the type of format to be generated.
      Here we are using "noframes" which generates a single html report.
     -->
         <junit>
        <classpath>
            <pathelement location="./junit-4.8.2.jar"/>
            <pathelement location="./ant-junit4.jar"/>
        </classpath>
        <formatter type="plain" usefile="false" /> <!-- to screen -->
        <formatter type="plain" /> <!-- to file -->
        <test name = "test.com.folder.service.ejb.session.SL_ServiceTest" todir="."/>
    </junit>
  </target>
</project>

当Java虚拟机(JVM)尝试加载特定类,但在类路径中找不到指定类时,会引发类NotFoundExceptionClassNotFoundException是一个已检查的异常,因此必须在方法或构造函数的throws子句中声明

如何处理类NotFoundException

1.验证所请求类的名称是否正确,以及类路径中是否存在相应的.jar文件。如果没有,你必须 显式地将其添加到应用程序的类路径中

2.如果指定的.jar文件存在于类路径中,那么应用程序的类路径将被覆盖,您必须找到 应用程序使用的确切类路径

3.如果异常是由第三方类引起的,则必须标识引发异常的类,然后添加缺少的类 类路径中的.jar文件


确保您的项目已经在项目的build/classes目录下创建了类文件,并像这样修改build.xml

 <project name="MyProject" default="junit-report" basedir=".">
    <property name="dir.build" value="build"/>
    <!-- Do something -->
    <path id="classpath.project">
      <pathelement path="${dir.build}"/>
    </path>
    <junit>   
       <classpath refid="classpath.project"/>
         <pathelement location="./junit-4.8.2.jar"/>
         <pathelement location="./ant-junit4.jar"/>
       </classpath>
       <formatter type="plain" usefile="false" /> <!-- to screen -->
       <formatter type="plain" /> <!-- to file -->
       <test name = "test.com.folder.service.ejb.session.SL_ServiceTest" todir="."/>
    </junit>

   </project>


我已经用我的类路径环境变量更新了我的问题。如果我错过了任何东西,请告诉我。test.com.folder.service.ejb.session.SL_ServiceTest.class未被JVM找到,我认为它与任何其他JAR无关。您是否将测试类放在junit ant任务的类路径中?希望这个链接会有帮助。否。我应该在类路径中添加它所在的类的路径吗?。我只是在类路径中添加了类文件的路径。仍然不起作用。