【问题标题】:windows classpath issue with ant and javaant和java的windows类路径问题
【发布时间】:2014-07-19 14:39:42
【问题描述】:

我已经在这些战壕中战斗了一段时间,但还没有完成这项工作。在 SO 上使用许多示例以及 this blogthis 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 并且存在包位置。从这里开始,我沿着兔子洞旅行,寻找可能发生的事情。我已经测试了清单相对位置的不同排列,但无济于事。我什至已经进入并手动将清单更改为显式文件位置(与 jar 无关),结果没有任何变化。

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

在我阅读的任何地方,这都应该有效。然而,我在这里。我知道如果清单中的引用找不到,它会默默地跳过它。这就是它似乎正在做的事情,但我已经尝试了一切来让它看看有什么无济于事。因此,我求助 SO 贡献者,要么指出我错过的愚蠢错误,要么让我了解我尚未学习的秘密知识。

提前致谢。

【问题讨论】:

    标签: java ant jar classpath manifest


    【解决方案1】:

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

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

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

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

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

    C:\WINDOWS>CLASSPATH
    

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

    C:\WINDOWS>set
    

    【讨论】:

    • 我认为您不太了解这个问题...使数千条单独的路径缩短几个字符并不是我需要的解决方案。只有在我创建另一个将长度推高的类之前,它才会“起作用”。我需要将所有这些单独的路径压缩成一个(或少数)路径,这些路径指向实际包含所有这些单独类的 jar。
    猜你喜欢
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多