简体   繁体   English

与ant和java的windows classpath问题

[英]windows classpath issue with ant and java

I've been fighting in these trenches for a while now and have yet to get this working. 我已经在这些战壕中战斗了一段时间,但尚未开始工作。 Using many examples here on SO as well as others like this blog or this SO post , I still cannot get past the windows classpath limit. 在SO以及其他类似博客此SO帖子中使用了很多示例,我仍然无法超越Windows类路径限制。 I'm currently just dealing with an ant task that runs a single main method in a java class within my source. 我目前正在处理一个在我的源代码中运行java类中的单个main方法的ant任务。 If I can get this working here, I can extrapolate elsewhere. 如果我能在这里工作,我可以在其他地方推断。

First, my original relevant build tasks 首先,我原来的相关构建任务

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

Next, my most recent attempt at a solution 接下来,我最近尝试解决方案

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

Basically, when running the first one, I get the "classpath is too long" issue in windows. 基本上,当运行第一个时,我在Windows中得到“类路径太长”的问题。 Running the second one, I get "could not find or load main class package.classname." 运行第二个,我得到“无法找到或加载主类package.classname。” I've gone through the MANIFEST.MF that is created in the jar and the package location is present. 我已经浏览了jar中创建的MANIFEST.MF,并且包位置存在。 From here went my travels down the rabbit hole to find what could be up. 从这里开始,我沿着兔子洞旅行,找到可能的东西。 I've tested different permutations of the manifest relative locations to no avail. 我已经测试了明显相对位置的不同排列无济于事。 I've even gone in and manually changed the manifest to an explicit file location (not relative to the jar) with no change in results. 我甚至进入并手动将清单更改为显式文件位置(不是相对于jar),结果没有变化。

To confirm that the overall code works, I can add my code.dir path into temp.classpath and get the too long error. 要确认整个代码是否有效,我可以将我的code.dir路径添加到temp.classpath并获取太长的错误。 I can also make the acme jar build with the classes within it and get past the issue with the missing class. 我也可以使用其中的类来构建acme jar,并通过缺少的类来解决问题。

Everywhere I read, this should work. 我读到的每个地方都应该有效。 Yet, here I am. 然而,我在这里。 I know that if the reference in the manifest can't be found, it silently skips it. 我知道如果找不到清单中的引用,它会默默地跳过它。 That is what it seems to be doing, yet I've tried everything to get it to see what's there to no avail. 这就是它似乎正在做的事情,但我已经尝试了一切来让它看到什么是无济于事的。 And thus, I turn to you SO contributors to either point out the silly mistake I've missed, or let me in on the secret knowledge that I have yet to learn. 因此,我向你们求助于指出我错过的愚蠢错误,或者让我了解我尚未学习的秘密知识。

Thanks in advance. 提前致谢。

You were perhaps closer with your original answer if you can at least achieve 'classpath is too long' answer. 如果你至少可以达到“类路径太长”的答案,你可能更接近原来的答案。

Have you tried nesting your paths in order to make them 'relevant' rather than 'absolute' and thus shorter? 您是否尝试过嵌套路径以使它们“相关”而不是“绝对”并因此缩短? perhaps using an alias such as the way you might list 'JAVA_HOME' in your PATH variable? 也许使用别名,例如你可能在PATH变量中列出'JAVA_HOME'的方式?

A way to test this quickly from the command line would be to edit the classpath and be sure to clear it out when you try to re-set it in code. 从命令行快速测试这种方法的方法是编辑类路径,并确保在尝试在代码中重新设置时清除它。 In windows that would look like the following: 在窗口中,如下所示:

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

This will update the PATH by appending the new path to the existing path value. 这将通过将新路径附加到现有路径值来更新PATH。 Typing the following command will print the new PATH in all future CMD windows; 键入以下命令将在以后的所有CMD窗口中打印新的PATH; NOT in the current CMD window: 不在当前的CMD窗口中:

C:\WINDOWS>CLASSPATH

Typing the following will give you a list of all the environment variables: 键入以下内容将为您提供所有环境变量的列表:

C:\WINDOWS>set

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM