简体   繁体   English

无法为Eclipse项目创建可运行的jar

[英]Not able to create runnable jar for eclipse project

I have created small java application in eclipse. 我在Eclipse中创建了小型Java应用程序。 Everything is working fine when I am creating runnable jar from IDE. 从IDE创建可运行的jar时,一切工作正常。 but somehow I am not able to create it via terminal (fyi I am using Mac). 但是以某种方式我无法通过终端创建它(仅供参考,我使用Mac)。

Following is my project directory structure 以下是我的项目目录结构

./bin (Contains all compiled classes)
./lib (Contains all the third party lib I am using lib1.jar,lib2.jr, lib3.jar)
./src (Contains my java class = myProject.java)
manifest.txt (Contains 2 line, first line is Main-Class:myProject and second line is empty)

Following is the command that I used for compiling my classes 以下是我用于编译类的命令

javac -d bin -sourcepath src -cp lib/lib1.jar:lib/lib2.jar:lib/lib3.jar src/myProject.java

To create jar file I am using following command 要创建jar文件,我正在使用以下命令

jar cvfm myProject.jar manifest.txt bin/*.class lib/*.jar

When I run this command I am getting invalid header field. 当我运行此命令时,我得到的标题字段无效。 Following is complete error message. 以下是完整的错误消息。

java.io.IOException: invalid header field
    at java.util.jar.Attributes.read(Attributes.java:393)
    at java.util.jar.Manifest.read(Manifest.java:180)
    at java.util.jar.Manifest.<init>(Manifest.java:50)
    at sun.tools.jar.Main.run(Main.java:149)
    at sun.tools.jar.Main.main(Main.java:1147)

I am not sure where I am going wrong. 我不确定我要去哪里错。 Can anyone point what mistake I am making or whether do I need to some more stuff ? 谁能指出我在犯什么错误,还是我需要更多的东西?

Thanks in advance. 提前致谢。

The JAR File Specification can be of help here. JAR文件规范可以在这里提供帮助。

I think the problem behind the “invalid header field” message is that the value of the header field must begin with whitespace; 我认为“无效标头字段”消息背后的问题是标头字段的值必须以空格开头; try manifest.txt containing 尝试manifest.txt包含

Main-Class: myProject

instead. 代替。

As for accessing the classes in the lib/*.jar files, Java won't find classes inside jar files inside jar files—you can actually skip specifying lib/*.jar on the jar command line. 至于访问lib/*.jar文件中的类,Java不会在jar文件内的jar文件中找到类-您实际上可以跳过在jar命令行上指定lib/*.jar的方法。

Here are two possibilities: 这有两种可能性:

  • Add

     Class-Path: lib/lib1.jar lib/lib2.jar lib/lib3.jar 

    to manifest.txt . manifest.txt The disadvantage to this is that if you distribute your jar and the library jars are not at the correct relative paths, there will be a ClassNotFoundError. 这样做的缺点是,如果您分发jar且库jar不在正确的相对路径上,则会出现ClassNotFoundError。

  • Unzip all the .class files from inside lib/*.jar into a temporary directory and add them to myProject.jar . lib/*.jar内部将所有.class文件解压缩到一个临时目录中,并将它们添加到myProject.jar The advantage to this is that the jar is completely self-contained; 这样做的好处是罐子是完全独立的。 the disadvantage is that it is a larger file that is more complicated and takes more time to create. 缺点是它是一个较大的文件,较复杂,创建时间也较长。 You could use a shell script along these lines: 您可以按照以下方式使用shell脚本:

     mkdir lib/expanded cd lib/expanded for J in ../*.jar; do unzip "${J}" rm -rf META-INF done zip -R ../../myProject.jar * cd .. && rm -rf expanded 

    The META-INF directory is deleted after extracting each lib/*.jar file so that unzip won't ask whether to overwrite, and so that it doesn't overwrite the manifest of myProject.jar . 提取每个lib/*.jar文件后,将删除META-INF目录,以便unzip不会询问是否要覆盖,并且它也不会覆盖myProject.jar的清单。

Reading through the jar manfile, I guess you could try something like this: 仔细阅读jar文件,我想您可以尝试如下操作:

jar cvmfe manifest.txt myProjecy.jar myProject bin/*.class lib/*.jar

Where myProject is your entry class name. 其中myProject是您的输入类别名称。 If that doesn't work, try removing the -m option and manifest.txt completely, I guess the -e option should take of the MainClass entry anyway. 如果那不起作用,请尝试完全删除-m选项和manifest.txt ,我想-e选项还是应该占用MainClass条目。

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

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