【问题标题】:Error! The first argument to the non-static Java function 'replace' is not a valid object reference错误!非静态 Java 函数“replace”的第一个参数不是有效的对象引用
【发布时间】:2012-05-15 19:01:34
【问题描述】:

我试图让 ANT 在 Eclipse 中创建 JUNIT 测试的 HTML 报告,但在创建 ANT 构建后,我在运行它时收到以下错误:

[junitreport] Processing C:\Documents and Settings\Administrator\workspace\Home\junit\TESTS-TestSuites.xml to C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\null785926900
[junitreport] Loading stylesheet jar:file:/C:/ANT/apache-ant-1.8.3/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] : Error! The first argument to the non-static Java function 'replace' is not a valid object reference.
[junitreport] : Error! Cannot convert data-type 'void' to 'reference'.
[junitreport] : Fatal Error! Could not compile stylesheet
[junitreport] Failed to process C:\Documents and Settings\Administrator\workspace\Home\junit\TESTS-TestSuites.xml

我需要做什么来解决这个问题?

这是我尝试运行的 Build.xml 的部分:

<target name="Home">
    <mkdir dir="${junit.output.dir}"/>
    <junit fork="yes" printsummary="withOutAndErr">
        <formatter type="xml"/>
        <test name="Home" todir="${junit.output.dir}"/>
        <classpath refid="Home.classpath"/>
    </junit>
</target>
<target name="junitreport">
    <junitreport todir="${junit.output.dir}">
        <fileset dir="${junit.output.dir}">
            <include name="TEST-*.xml"/>
        </fileset>
        <report format="frames" todir="${junit.output.dir}"/>
    </junitreport>
</target>

【问题讨论】:

  • 第二,不贴代码失败,我们怎么知道是什么问题?
  • 在将 ANT 构建指向 JDK 而不是 JRE 后,我能够获得 HTML 报告。
  • 您如何将 ANT 构建指向 JDK 而不是 JRE?

标签: eclipse ant junit


【解决方案1】:

当我收到此错误时,我必须在 Eclipse 中右键单击我的 build.xml 文件,选择“Run as Ant build...”选项(菜单中的第 3 个),然后单击 JRE 选项卡并选择“在与工作区相同的 JRE 中运行”选项,然后继续运行脚本。由于某种原因,这解决了问题。老实说,我不知道为什么。

【讨论】:

  • 也为我工作。谢谢:-)
【解决方案2】:

Eclipse 错误已明确记录在 Bug 384757。分析表明,Oracle 引入了该错误。我需要 Java 版本 7,但 JunitReport 需要 Java 版本 5 和 Java 版本 6 的早期版本。显而易见的方法是将“bootclasspath”参数添加到 Ant junitreport 任务中,而 Java 问题由 Oracle 在未来的版本中修复。不幸的是 Ant 任务 junitreport 不支持 bootclasspath 选项。这是支持蚂蚁的人能做到的吗?

【讨论】:

    【解决方案3】:

    我在 Ubuntu 12.04 上使用 *java 1.7.0_51* 在 Eclipse SDK Juno 上运行我的 java 程序时遇到了这种情况。我接受 Bhagyaraj 的建议,即使用 apache-ant-1.9.2 代替。 (ps.Eclipse Juno默认的ant版本是*org.apache.ant_1.8.3*,我用的是apache-ant-1.9.3)

    您可以按照我的步骤进行设置。

    1. 下载 apache-ant-1.9.3-bin.zip 并解压到您想要的位置。
    2. 打开 Eclipse 并选择 Window --> Preferences
    3. 您可以看到“首选项”窗口。在左侧请选择 ant --> runtime
    4. 现在将焦点放在窗口的右侧。确保您现在位于 Classpath 选项卡上并选择 Ant Home Entries(Default)。右侧的按钮是 Ant Home...现在可以点击了。
    5. 毫无疑问,点击Ant Home...按钮并选择你的ant-1.9.3所在的文件夹。在这一步中,如果您选择了错误的文件夹,Eclipse 可能会显示类似 Specified Ant home does not contain a "lib" directory 之类的错误消息。如果选择正确的目录,Ant Home Entries(Default) 将变为 **Ant Home Entries()
    6. 别忘了点击应用。

    更改这些后,当您右键单击 build.xml 文件并选择 Run --> 1 Ant Build 时,将不会显示错误消息,因为您的 ant 现在是 1.9.3。

    我也下载了 apache-ant-1.8.4 来运行我的构建文件,但错误无法解决。

    【讨论】:

      【解决方案4】:

      我用的是jdk1.6.45和最新的apache-ant-1.9.2-bin\

      问题解决了。

      根据 MikeBach 的说明

      这是错误#, 请阅读: https://bugs.eclipse.org/bugs/show_bug.cgi?id=384757 已在 Ant 中修复。

      问候, 巴格亚拉杰

      【讨论】:

      • 还可以尝试:在命令提示符下运行您的 build.xml 文件,以便更快地解决。(ANT: JAVA:1.70_45. and ant-1.9.2)它真的很顺利,生成了报告.
      【解决方案5】:

      如果您在 JDK 1.7 中使用 Eclipse/JUnit 报告,请使用 JDK 1.6。 JDK 1.7 中存在与您描述的类似的 Eclipse/JUnit 报告的错误。这可能是一个类似的报告:https://netbeans.org/bugzilla/show_bug.cgi?id=201022

      【讨论】:

        【解决方案6】:

        我已尝试使用几种不同的 JRE/JDK 设置(全部为 1.6 和 1.7),但每种设置都有各种问题(此问题或无法访问 jar 中的 XSL 文件)。

        这显然不是一个完美的解决方案,但如果 JUnit 报告在构建文件中是“原始的”——未包含在目标中,我似乎不会遇到这些问题。

        糟糕的解决方法显然是目标是 Ant 的一个重要方面,但有时用于报告构建目标/依赖项并不是那么关键的任务。这对我来说还不是什么大问题,因为我以一种一次性的方式运行这些报告,而不是作为构建的一部分。

        【讨论】:

          【解决方案7】:

          Bug 是在 Java 7u5 according to this comment 中引入的。 另一个comment 解释了如何解决它,更改bootclasspath。 (免责声明:我没有尝试测试这个。)

          不过,这个解决方案对我来说效果很好

          • 下载ant-1.9.4
          • Eclipse>>首选项>>Ant>>运行时>>AntHomeEntries
          • 删除所有 1.8.x 类路径条目
          • 改为添加 1.9.4 中的所有库

          根据蚂蚁Changes,这是fixed since 1.9.1

          【讨论】:

            猜你喜欢
            • 2011-04-25
            • 2014-11-28
            • 2015-03-28
            • 2015-01-26
            • 1970-01-01
            • 1970-01-01
            • 2011-02-16
            • 1970-01-01
            相关资源
            最近更新 更多