java - FindBugs 找不到 org.apache.bcel.classfile.ClassFormatException

标签 java linux ant fedora findbugs

我安装了 Fedora 20 findbugs RPM,并设置了我的 Ant build.xml 文件:

<property name="findbugs.home" location="/usr/share/findbugs"/>

<target name="findbugs" description="static bytecode analysis">
    <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask">
    </taskdef>

    <findbugs home="${findbugs.home}" output="xml" outputFile="bcel-fb.xml">
        <sourcePath path="${src.dir}"/>
        <fileset dir="${build.dir}">
            <include name="**/*.class"/>
        </fileset>
    </findbugs>
</target>

这给了我错误:

java.lang.IllegalArgumentException: Can't find findbugs.jar in /usr/share/findbugs/lib

所以我将 /usr/share/findbugs/lib 设为 /usr/share/java 的符号链接(symbolic link),其中 findbugs.jar生活。这让我更进一步,但现在它吐了出来:

findbugs:
 [findbugs] Executing findbugs from ant task
 [findbugs] Running FindBugs...
 [findbugs] Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/bcel/classfile/ClassFormatException

strace 显示它找到并打开了 /usr/share/java/findbugs-bcel.jar,所以我不知道为什么它不能加载类.显式添加 /usr/share/java/findbugs-bcel.jar 到 taskdef 的类路径不会改变任何东西。

注意:我并不是要在 BCEL 上运行 FindBugs。 FindBugs 使用 BCEL 进行分析,而我自己的代码根本不使用 BCEL。

最佳答案

你需要一个 <Auxclasspath/>指向编译时使用的类路径的子实体。 Findbugs 使用内置的 *.class文件,并需要源代码来查明错误所在的源代码、您正在分析的类以及所有第三方 jar 的类路径,以便能够理解代码:

 <findbugs
     home="${findbugs.home}" 
     output="xml" 
     outputFile="bcel-fb.xml">
     <auxClasspath refid="main.classpath"/>  <!-- Third party jars classpath   -->
     <class location="${main.destdir}"/>     <!-- Class files you're analyzing -->
     <sourcePath path="${main.srcdir}"/>     <!-- Source directory             -->
 </findbugs>

顺便说一句,你应该把 findbugs jar 放在你的项目中(我的偏好是在 ${basedir}/antlib/findbugs 下)并在你的 <taskdef/> 中使用它:

<taskdef name="findbugs" 
    classname="edu.umd.cs.findbugs.anttask.FindBugsTask">
    <classpath>
       <fileset dir="${basedir}/antlib/findbugs"/>
    </classpath>
</taskdef>

这样,Findbugs 就是您项目的一部分。 checkout 您的项目的任何人都将自动拥有 findbugs jar,并且无需下载 jar 并将其安装在 $ANT_HOME/lib 中即可运行您的 findbugs 任务。 .

我使用任何可选的 Ant 任务 jar 来执行此操作,例如 PMD、Checkstyle 和 Ant-Contrib。它使其他人能够更容易地运行我的构建,并且我花更少的时间试图向人们解释他们可以从哪里获得那个 jar,以及如何安装它。另外,如果您使用像 Jenkins 这样的持续构建系统,您就不必在添加新的可选 jar 时随时使用服务器。

关于java - FindBugs 找不到 org.apache.bcel.classfile.ClassFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21795698/

相关文章:

java - 如何从我生成的数据中获取标准差

linux - 想要将命令 echo ^`date` 的结果分配给一个变量。但是当我尝试这样做时,我没有得到异常(exception)的结果

java - Ant 支持基于文件的依赖关系吗?

gradle - 将FMPP与Gradle一起使用

java - IntelliJ Idea和Netbeans如何构建java项目?

java - 以编程方式清除缓存的后台进程

java - 在哪里可以查看 Maven 插件的默认执行计划?

c++ - 如何向 perforce-jam 构建系统解释它应该使用哪个编译器(gcc)

php - PHP 函数是否区分大小写,如果不是...?

java - 我需要将XML文件转换为CSV,但必须只读取一次文件,并且记录标记中的标题可能会改变,有什么想法吗?