简体   繁体   English

JAR类路径和外部jar

[英]JAR classpath and external jars

I actualy have 2 problems 我实际上有2个问题

I use eclipse -> export project to generate a jar file for my simple desktop (GUI) program 我使用eclipse-> export项目为我的简单桌面(GUI)程序生成jar文件
It generates a jar file and an ant script. 它生成一个jar文件和一个ant脚本。

first problem: the generated jar works fine when double-clicked. 第一个问题:双击生成的jar即可正常工作。
When I use the generated ant script to generate the jar 当我使用生成的ant脚本生成jar时
by myself, it doesn't work. 我个人认为这是行不通的。 What can be wrong with a target like this (assuming that all dependencies are met) 这样的目标可能有什么问题(假设满足所有依赖性)

 <target name="create_run_jar">
        <jar destfile="G:/dev/myproj/myproj.jar">
            <manifest>
                <attribute name="Main-Class" value="view.myproj"/>
                <attribute name="Class-Path" value=". myproj_lib/grouplayout.jar"/>
            </manifest>
            <fileset dir="G:/dev/myproj/bin"/>
        </jar>
        <delete dir="G:/dev/myproj/myproj_lib"/>
        <mkdir dir="G:/dev/myproj/myproj_lib"/>
        <copy file="G:/dev/.metadata/.plugins/org.dyno.visual.swing/layoutext/grouplayout.jar" todir="G:/dev/myproj/myproj"/>
    </target>  

// nevemind // nevemind
//Second problem: //第二个问题:
//when I double click on the auto-generated jar file the program launches and works fine. //当我双击自动生成的jar文件时,程序将启动并运行良好。
//when I do java myjar from the command-line I get main class not found exception.. //当我从命令行执行java myjar时,未找到主类。
//weird huh? //很奇怪吧?

I suggest that you take the JAR files generated the two ways, use the jar command to expand them into temporary directories, and then use diff in recursive mode to compare them. 我建议您采用两种方式生成的JAR文件,使用jar命令将它们扩展到临时目录中,然后在递归模式下使用diff进行比较。

However, I suspect that @Pace has put his finger on the problem; 但是,我怀疑@Pace已将他的手指放在问题上了; ie that you are using relative paths in the Class-Path manifest entry and this is liable to cause problems. 也就是说,您在Class-Path清单条目中使用相对路径,这很容易引起问题。

java -jar <jar name>是执行jar的正确方法。

The ant target is creating a manifest with a classpath attribute. ant目标正在创建带有classpath属性的清单。 If you look at those paths you'll notice that they are relative to the current directory. 如果查看这些路径,您会注意到它们是相对于当前目录的。 When you execute java -jar from the command line are you in the... 当您从命令行执行java -jar时,您正在...

G:/dev/myproj

...directory? ...目录?

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

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