运行ant junit的时候抛出ClassNotFoundException [英] ClassNotFoundException when running ant junit

查看:147
本文介绍了运行ant junit的时候抛出ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行到这个的ClassNotFoundException 问题下面运行的的Junit 任务的时候,但是这是从运行命令Ant构建非基于eclipse的项目提示,构建文件下面粘贴。唯一的例外是在下面。我做了类似的设置<一的href=\"http://stackoverflow.com/questions/24830303/classnotfoundexception-when-running-ant-junit\">here它不工作虽然,任何指针将是AP preciated。谢谢。

 从父加载器加载的类org.apache.tools.ant.util.StringUtils(parentFirst)
    [JUnit的]测试工具:SampleTest
从父加载器加载的类org.apache.tools.ant.util.FileUtils(parentFirst)
    [JUnit的]测试运行:1,失败:0,错误:1,经过时间:0秒
    [JUnit的]
    [JUnit的]导致了错误
    [JUnit的] SampleTest
    [JUnit的]抛出java.lang.ClassNotFoundException:SampleTest
    [JUnit的]在java.lang.ClassLoader.loadClass(ClassLoader.java:235)
    [JUnit的]在java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
    [JUnit的]在java.lang.Class.forName0(本机方法)
    [JUnit的]在java.lang.Class.forName(Class.java:219)
    [JUnit的]

在build.xml

 &LT;项目名称=Java项目构建默认为测试&GT;
    &LT;属性名=project.local.directory值=。 /&GT;
    &LT;属性名=src.path值=$ {} project.local.directory / SampleUnitTest / COM/&GT;
    &LT;属性名=lib.path值=$ {} project.local.directory / APP-INF / lib目录/&GT;
    &LT;属性名=dest.path值=$ {} project.local.directory / SampleUnitTest /目标/&GT;
    &LT;属性名=junit.output.dir值=$ {} project.local.directory / JUnit的/&GT;    &所述;路径ID =MyProject.classpath&GT;
        &LT; pathelement位置=$ {} lib.path /ant-junit.jar/&GT;
        &LT; pathelement位置=$ {} lib.path /junit.jar/&GT;
        &LT; pathelement位置=$ {} lib.path /SampleUnitTest.jar/&GT;
        &LT; dirset DIR =$ {} project.local.directory / SampleUnitTest&GT;
            &LT;包括姓名=目标/&GT;
        &LT; / dirset&GT;
    &LT; /路径&GT;
    &所述;路径ID =lib.classpath&GT;
        &LT; pathelement位置=$ {} lib.path /ant-junit.jar/&GT;
        &LT; pathelement位置=$ {} lib.path /junit.jar/&GT;
        &LT;文件集DIR =$ {} lib.path&GT;
            &LT;包括姓名=** / *罐子。/&GT;
        &LT; /文件集&GT;
    &LT; /路径&GT;
    &lt;目标名称=测试描述=测试的java文件取决于=打造&GT;
        &LT; MKDIR DIR =$ {} junit.output.dir/&GT;
        &LT;&JUnit的GT;
            &LT;类路径REFID =MyProject.classpath&GT;
            &LT; /类路径&GT;
            &LT; batchtest todir =$ {} junit.output.dir&GT;
                &LT;格式化类型=普通usefile =FALSE/&GT;
                &LT;文件集DIR =$ {} src.path&GT;
                    &LT;包括姓名=** / * *测试的.java/&GT;
                &LT; /文件集&GT;
            &LT; / batchtest&GT;
        &LT; / JUnit的&GT;
    &LT; /目标与GT;    &lt;目标名称=构建描述=测试的java文件取决于=干净&GT;
        &LT; MKDIR DIR =$ {} dest.path/&GT;
        &LT; javac的SRCDIR =$ {} src.pathDESTDIR =$ {} dest.pathclasspathref =lib.classpath&GT;
        &LT; / javac的&GT;
        &LT;罐子destfile =$ {} lib.path /SampleUnitTest.jarBASEDIR =$ {} dest.path/&GT;
    &LT; /目标与GT;    &lt;目标名称=干净描述=测试的java文件&GT;
        &LT;删除DIR =$ {} dest.path&GT;
        &LT; /删除&GT;
    &LT; /目标与GT;
&LT; /项目&GT;


更新

 包COM;进口com.tds.metasolv.common.util.CommonUtilities;
进口com.tds.metasolv.common.util.specific.PortAddressUtilities;
进口的junit.framework.TestCase;公共类SampleTest扩展TestCase的
{
    PortAddressUtilities utils的= NULL;    保护无效设置()抛出异常{
        utils的= PortAddressUtilities.getInstance(CommonUtilities.getInstance());
    }    公共无效testGetPONPortNumber(){
        字符串结果= utils.getPONPortNumber(N10-1-2-3-4);
        assertTrue(预计3,却得到了+结果+,result.equals(3)中的结果。);
        结果= utils.getPONPortNumber(N10-1-2-3);
        assertTrue(预计3,却得到了+结果+,result.equals(2)中的结果。);
    }
}


解决方案

问题是对于src.dir 您已经定义

 &LT;属性名=src.path值=$ {} project.local.directory / SampleUnitTest / COM/&GT;

您所创建的类是在包COM ,表示将使用被引用的类 com.SampleTest

为了解决这个异常,你需要添加一个src文件夹并移动 COM / Test.java 到该文件夹​​,使上述定义的 src.path 会像

 &LT;属性名=src.path值=$ {} project.local.directory / SampleUnitTest / src目录/&GT;

,改变 MyProject.classpath 在构建XML如下

 &LT;路径ID =MyProject.classpath&GT;
    &LT; pathelement位置=$ {} lib.path /ant-junit.jar/&GT;
    &LT; pathelement位置=$ {} lib.path /junit.jar/&GT;
    &LT; pathelement位置=$ {} lib.path /SampleUnitTest.jar/&GT;
    &LT; pathelement位置=$ {} project.local.directory / SampleUnitTest /目标/&GT;
&LT; /路径&GT;

I'm running into this ClassNotFoundException issue when running Junit task below, but this is non eclipse based project running an ant build from command prompt, the build file is pasted below. The exception is below. I have done similar to that of setup here it does not work though, any pointers will be appreciated. Thanks.

Class org.apache.tools.ant.util.StringUtils loaded from parent loader (parentFirst)
    [junit] Testsuite: SampleTest
Class org.apache.tools.ant.util.FileUtils loaded from parent loader (parentFirst)
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] 
    [junit]     Caused an ERROR
    [junit] SampleTest
    [junit] java.lang.ClassNotFoundException: SampleTest
    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
    [junit]     at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
    [junit]     at java.lang.Class.forName0(Native Method)
    [junit]     at java.lang.Class.forName(Class.java:219)
    [junit] 

Build.xml

<project name="Java project build" default="test">
    <property name="project.local.directory" value="." />
    <property name="src.path" value="${project.local.directory}/SampleUnitTest/com" />
    <property name="lib.path" value="${project.local.directory}/APP-INF/lib" />
    <property name="dest.path" value="${project.local.directory}/SampleUnitTest/target" />
    <property name="junit.output.dir" value="${project.local.directory}/junit" />

    <path id="MyProject.classpath">
        <pathelement location="${lib.path}/ant-junit.jar" />
        <pathelement location="${lib.path}/junit.jar" />        
        <pathelement location="${lib.path}/SampleUnitTest.jar" />           
        <dirset dir="${project.local.directory}/SampleUnitTest">
            <include name="target" />
        </dirset>       
    </path>
    <path id="lib.classpath">
        <pathelement location="${lib.path}/ant-junit.jar" />
        <pathelement location="${lib.path}/junit.jar" />
        <fileset dir="${lib.path}">
            <include name="**/*.jar" />
        </fileset>
    </path>
    <target name="test" description="Tests the java files" depends="build">
        <mkdir dir="${junit.output.dir}" />
        <junit>
            <classpath refid="MyProject.classpath">
            </classpath>
            <batchtest todir="${junit.output.dir}">
                <formatter type="plain" usefile="false" />
                <fileset dir="${src.path}">
                    <include name="**/*Test*.java" />
                </fileset>
            </batchtest>
        </junit>
    </target>

    <target name="build" description="Tests the java files" depends="clean">
        <mkdir dir="${dest.path}" />
        <javac srcdir="${src.path}" destdir="${dest.path}" classpathref="lib.classpath">
        </javac>        
        <jar destfile="${lib.path}/SampleUnitTest.jar" basedir="${dest.path}"/>
    </target>

    <target name="clean" description="Tests the java files">
        <delete dir="${dest.path}">
        </delete>
    </target>
</project>


Update

package com; 

import com.tds.metasolv.common.util.CommonUtilities;
import com.tds.metasolv.common.util.specific.PortAddressUtilities;
import junit.framework.TestCase;

public class SampleTest extends TestCase
{ 
    PortAddressUtilities utils = null;

    protected void setUp() throws Exception{
        utils = PortAddressUtilities.getInstance(CommonUtilities.getInstance());
    }

    public void testGetPONPortNumber(){
        String result = utils.getPONPortNumber("N10-1-2-3-4");
        assertTrue("Expected 3, but got "+result +" in the result. ", result.equals("3"));
        result = utils.getPONPortNumber("N10-1-2-3");
        assertTrue("Expected 3, but got "+result +" in the result. ",result.equals("2"));
    }
} 

解决方案

Problem is the src.dir you have defined

<property name="src.path" value="${project.local.directory}/SampleUnitTest/com"/>

The class you have created is in package com that means the class will be referenced using com.SampleTest

In order to fix this anomaly you need to add a src folder and move your com/Test.java to that folder so that the above defined src.path will be like

<property name="src.path" value="${project.local.directory}/SampleUnitTest/src"/>

And also change MyProject.classpath in build xml as below

<path id="MyProject.classpath">
    <pathelement location="${lib.path}/ant-junit.jar" />
    <pathelement location="${lib.path}/junit.jar" />        
    <pathelement location="${lib.path}/SampleUnitTest.jar" />           
    <pathelement location="${project.local.directory}/SampleUnitTest/target"/>
</path>

这篇关于运行ant junit的时候抛出ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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