gpt4 book ai didi

java - FindBugs 拒绝在类路径上找到 bcel jar

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:23:23 25 4
gpt4 key购买 nike

我一生都在尝试让 FindBugs (2.0.1) 作为我的命令行 Ant 构建的一部分运行。我下载了 FindBugs JAR 并将其解压缩到/home/myuser/java/repo/umd/findbugs/2.0.1/findbugs-2.0.1:

enter image description here

如您在屏幕截图中所见,在/home/myuser/java/repo/umd/findbugs/2.0.1/findbugs-2.0.1/lib 下有一个名为 bcel-1.0.jar,如果你打开它,你会看到我已经深入到一个名为 org.apache.bcel.classfile.ClassFormatException 的类。坚持这个想法。

然后我将/home/myuser/java/repo/umd/findbugs/2.0.1/findbugs-2.0.1/lib/findbugs-ant.jar 复制到 ${env.ANT_HOME}/lib 以使其可访问从命令行运行的 Ant 版本(而不是 Eclipse 内置的 Ant 实例)。

我的项目目录结构如下:

/home/myuser/sandbox/workbench/eclipse/workspace/myapp/
src/
main/
java/
test/
java/
build/
build.xml
build.properties
gen/
bin/
main/ --> where all main Java class files compiled to
test/ --> where all test Java class files compiled to
audits/
qual/
staging/

build.xml 中:

<project name="myapp-build" basedir=".." default="package"
xmlns:fb="antlib:edu.umd.cs.findbugs">

<path id="findbugs.source.path">
<fileset dir="src/main/java">
<include name="**.*java"/>
</fileset>
<fileset dir="src/main/test">
<include name="**.*java"/>
</fileset>
</path>

<taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask"
uri="antlib:edu.umd.cs.findbugs"/>

<!-- Other Ant target omitted for brevity. -->

<target name="run-findbugs">
<!-- Create a temp JAR that FindBugs can use for analysis. -->
<property name="fb.tmp.jar" value="gen/staging/${ant.project.name}-findbugs-temp.jar"/>
<echo message="Creating ${fb.tmp.jar} for FindBugs."/>
<jar destfile="gen/staging/${ant.project.name}-findbugs-temp.jar">
<fileset dir="gen/bin/main" includes="**/*.class"/>
<fileset dir="gen/bin/test" includes="**/*.class"/>
</jar>

<echo message="Conducting code quality tests with FindBugs."/>
<fb:findbugs home="/home/myuser/java/repo/umd/findbugs/2.0.1/findbugs-2.0.1"
output="html" outputFile="gen/audits/qual/findbugs.html" stylesheet="fancy-hist.xsl" failOnError="true">
<sourcePath refid="findbugs.source.path"/>
<class location="${fb.tmp.jar}"/>
</fb:findbugs>
</target>

<target name="echoMsg" depends="run-findbugs">
<echo message="The build is still alive!!!"/>
</target>
</project>

但是当我从命令行运行 ant -buildfile build.xml echoMsg 时,FindBugs 中出现错误:

run-findbugs:
[echo] Creating gen/staging/myapp-build-findbugs-temp.jar for FindBugs.
[jar] Building jar: /home/myuser/sandbox/workbench/eclipse/workspace/myapp/gen/staging/myapp-build-findbugs-temp.jar
[echo] Conducting code quality tests with FindBugs.
[fb:findbugs] Executing findbugs from ant task
[fb:findbugs] Running FindBugs...
[fb:findbugs] Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/bcel/classfile/ClassFormatException
[fb:findbugs] Caused by: java.lang.ClassNotFoundException: org.apache.bcel.classfile.ClassFormatException
[fb:findbugs] at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
[fb:findbugs] at java.security.AccessController.doPrivileged(Native Method)
[fb:findbugs] at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
[fb:findbugs] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
[fb:findbugs] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[fb:findbugs] at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
[fb:findbugs] Could not find the main class: edu.umd.cs.findbugs.FindBugs2. Program will exit.
[fb:findbugs] Java Result: 1
[fb:findbugs] Output saved to gen/audits/qual/findbugs.html

echoMsg:
[echo] The build is still alive!!!

让我惊讶的是:

  • 即使使用 failOnError="true",即使遇到此运行时异常,FindBugs 也不会停止构建
  • 最后一段输出“Output saved to gen/audits/qual/findbugs.html”是个谎言! gen/audits/qual什么都没有!
  • bcel-1.0.jar 绝对位于 FindBugs 主目录下,就像 lib/目录中的所有其他 JAR 一样。

请注意:findbugs-ant.jar肯定是复制到ANT_HOME/lib;否则我会得到一个失败的构建,提示它找不到 Ant 任务。作为完整性检查,我继续执行此操作(我从 ANT_HOME/lib 中删除了 findbugs-ant.jar 并构建失败)。这个构建不会失败(它成功了!)。它只是不运行 findbugs。

有人能看出这里发生了什么吗?提前致谢!

最佳答案

您可以使用 jvm 的 -verbose:class 参数调试 BCEL 的加载位置。

要将此参数传递给运行 findbugs 的 jvm,请使用 find bugs 插件上的 jvmargs 标志

jvmargs

Optional attribute. It specifies any arguments that should be passed to the Java virtual machine used to run FindBugs. You may need to use this attribute to specify flags to increase the amount of memory the JVM may use if you are analyzing a very large program.

您是如何填充 find bugs lib jar 的?当我下载 findbugs.zip 时,我得到一个 lib 目录,它看起来与您显示的非常不同。特别是,我的包含一个版本为 5.3 的 bcel,而不是您显示的 1.0。

关于java - FindBugs 拒绝在类路径上找到 bcel jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12825551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com