与ant和java的windows classpath问题

时间:2014-05-29 22:59:55

标签: java ant jar classpath manifest

我现在已经在这些战壕中战斗了一段时间,但尚未开始工作。在SO以及this blogthis SO post等其他示例中使用了很多示例,我仍然无法超越Windows类路径限制。我目前只处理一个在我的源代码中运行java类中的单个main方法的ant任务。如果我能在这里工作,我可以在其他地方推断。

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

<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>

基本上,当运行第一个时,我得到&#34;类路径太长&#34;在Windows中的问题。运行第二个,我得到&#34;无法找到或加载主类package.classname。&#34;我已经浏览了jar中创建的MANIFEST.MF并且包位置存在。从这里开始,我沿着兔子洞旅行,找到可能的东西。我已经测试了相关位置的不同排列无效。我甚至进去并手动将清单更改为显式文件位置(不是相对于jar),结果没有变化。

要确认整个代码是否有效,我可以将我的code.dir路径添加到temp.classpath并获取太长的错误。我也可以使用其中的类构建acme jar,并通过缺少的类来解决问题。

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

提前致谢。

1 个答案:

答案 0 :(得分:0)

如果你至少可以达到“类路径太长”的答案,你可能更接近原来的答案。

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

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

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

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

C:\WINDOWS>CLASSPATH

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

C:\WINDOWS>set