java - ant 和 java 的 Windows 类路径问题

标签 java ant jar classpath manifest

我已经在这些战壕中战斗了一段时间,但还没有开始工作。在 SO 以及其他类似 this blog 上使用许多示例或 this SO post ,我仍然无法超越 Windows 类路径限制。我目前正在处理一个 ant 任务,它在我的源代码中的 java 类中运行一个 main 方法。如果我能在这里发挥作用,我就可以在其他地方进行推断。

首先是我原来的相关构建任务

<path id="code.classpath">
    <path id="code.classpath">
    <path refid="jars.code"/>
    <path refid="jars.common"/>
    <path refid="jars.servlet-api"/>
    <dirset dir="${code.dir}" excludes="xlib/scripts/**"/>
</path>
<target name="code.exec" description="Execute a class file">
    <echo>${code}</echo>
    <input addproperty="code.exec.class">Enter full class name (e.g. ${atmr.pkg}.FooBar):</input>
    <input addproperty="code.exec.args">Enter arguments:</input>
    <java classname="${code.exec.class}"
          fork="true"
          dir="${code.src.dir}"
          failonerror="true"
          classpathref="code.classpath">
        <jvmarg value="-Xmx4048M"/>
        <jvmarg value="-ea"/>
        <jvmarg value="-Dlog4j.configuration=file:${basedir}/log4j.xml"/>
        <syspropertyset refid="proxy.properties"/>
        <assertions refid="code.exec.assertions"/>
        <arg line="${code.exec.args}"/>
    </java>
</target>

接下来,我最近的解决方案尝试

<path id="code.source">
    <dirset dir="${code.dir}" excludes="xlib/scripts/**"/>
</path>

<target name="code.classpath.acme">
    <manifestclasspath property="jar.classpath" jarfile="${app.dir}/acme.jar">
        <classpath refid="code.source"/>
    </manifestclasspath>

    <jar destfile="${app.dir}/acme.jar" index="true">
        <manifest>
            <attribute name="Class-Path" value="${jar.classpath}"/>
        </manifest>
    </jar>
</target>

<path id="temp.classpath">
    <pathelement path="${app.dir}/acme.jar"/>
</path>
<target name="code.exec" description="Execute a class file" >
    <property name="myclasspath" refid="temp.classpath"/>
    <echo>${code}</echo>
    <echo>${basedir}</echo>
    <echo>${myclasspath}</echo>
    <input addproperty="code.exec.class">Enter full class name (e.g. ${atmr.pkg}.FooBar):</input>
    <input addproperty="code.exec.args">Enter arguments:</input>
    <java classname="${code.exec.class}"
          fork="true"
          dir="${code.src.dir}"
          failonerror="true"
          classpathref="temp.classpath">
        <jvmarg value="-Xmx4048M"/>
        <jvmarg value="-ea"/>
        <jvmarg value="-Dlog4j.configuration=file:${basedir}/log4j.xml"/>
        <syspropertyset refid="proxy.properties"/>
        <assertions refid="code.exec.assertions"/>
        <arg line="${code.exec.args}"/>
    </java>
</target>

基本上,当运行第一个时,我在 Windows 中遇到“类路径太长”的问题。运行第二个,我得到“无法找到或加载主类 package.classname”。我已经浏览了在 jar 中创建的 MANIFEST.MF 并且存在包位置。从这里开始,我沿着兔子洞旅行,寻找可能发生的事情。我已经测试了 list 相对位置的不同排列但无济于事。我什至进入并手动将 list 更改为明确的文件位置(不相对于 jar),结果没有任何变化。

为确认整个代码有效,我可以将我的 code.dir 路径添加到 temp.classpath 中并得到太长的错误。我还可以使用其中的类构建 acme jar,并解决缺少类的问题。

无论我在哪里阅读,这都应该有效。然而,我在这里。我知道如果找不到 list 中的引用,它会默默地跳过它。这就是它似乎正在做的事情,但我已经尽一切努力让它看看那里有什么无济于事。因此,我请求 SO 贡献者指出我遗漏的愚蠢错误,或者让我了解我尚未学习的 secret 知识。

提前致谢。

最佳答案

如果您至少可以实现“类路径太长”的答案,那么您可能更接近您的原始答案。

您是否尝试过嵌套路径以使它们“相关”而不是“绝对”并因此更短?也许使用别名,例如您可能在 PATH 变量中列出“JAVA_HOME”的方式?

从命令行快速测试它的一种方法是编辑类路径,并确保在您尝试在代码中重新设置它时将其清除。在如下所示的窗口中:

C:\WINDOWS>setx CLASSPATH "CLASSPATH;C:\some_new_path"

这将通过将新路径附加到现有路径值来更新 PATH。输入以下命令将在所有 future 的 CMD 窗口中打印新的 PATH;不在当前的 CMD 窗口中:

C:\WINDOWS>CLASSPATH

键入以下内容将为您提供所有环境变量的列表:

C:\WINDOWS>set

关于java - ant 和 java 的 Windows 类路径问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23944387/

相关文章:

java - 将 Maven 构建生成的 SQL 脚本放在哪里?

java - 强制 Storm 使用 fat jar 依赖而不是类路径定义的依赖

java - 为 Pig 设置 eclipse

java - Lagom Cassandra 驱动程序

java - 如何在游戏完成第一关后自动进入下一关?

java - 项目可交付成果中的 .classpath 文件

java - 我可以通过cmd运行.jar文件,但不能双击它们

java - 如何在 Thymeleaf 中正确序列化对象?

java - 设置自定义 findBugs 规则,使用 qulice-maven-plugin 禁用failesOnViolation

java - ant 目标 vssget 不起作用