简体   繁体   English

由 Eclipse 创建的 jar 文件无法运行

[英]jar file created by Eclipse doesn't run

I recently installed the last version of Eclipse on my pc running on Windows 10. Every time I try to export the simplest Java project in a runnable jar file, the jar file doesn't work. I recently installed the last version of Eclipse on my pc running on Windows 10. Every time I try to export the simplest Java project in a runnable jar file, the jar file doesn't work.

Example:例子:

import javax.swing.JOptionPane;
public class Try {
    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "Some message", "title!!", JOptionPane.INFORMATION_MESSAGE);
    }
}

File> Export> Java > Runnable Jar File All names/ Folders been set OK Package required libraries into generated JAR (but I tried every other switch )> Finish The jar file is created but doesn't make anything at all. File> Export> Java > Runnable Jar File All names/ Folders been set OK Package required libraries into generated JAR (but I tried every other switch )> Finish The jar file is created but doesn't make anything at all. Any suggestions?有什么建议么?

Not sure if this is your issue, but your jar needs to contain a manifest file to indicate which class within the jar is the application's entry point.不确定这是否是您的问题,但您的 jar 需要包含一个清单文件以指示 jar 中的哪个 class 是应用程序的入口点。 So it may be worth checking whether Eclipse is adding that correctly.因此,可能值得检查 Eclipse 是否正确添加。 Looks like some manual steps may be involved .看起来可能涉及一些手动步骤。 The file (META-INF/MANIFEST.MF) should contain contents such as:文件 (META-INF/MANIFEST.MF) 应包含以下内容:

Manifest-Version: 1.0
Created-By: 1.8.0_112 (Oracle Corporation)
Main-Class: MyClass

For reference, some guidance on adding the manifest (outside of Eclipse) can be found here .作为参考,可以在此处找到有关添加清单(在 Eclipse 之外)的一些指南。

FYI, if you do end up adding a manifest manually, be sure to follow this advice from that page to add a new line at the end of the file.仅供参考,如果您最终手动添加清单,请务必按照该页面的建议在文件末尾添加新行 Missing it, as I have, can lead to much frustration:就像我一样,错过它会导致很多挫败感:

Warning: The text file must end with a new line or carriage return.警告:文本文件必须以换行符或回车符结尾。 The last line will not be parsed properly if it does not end with a new line or carriage return.如果最后一行没有以新行或回车结束,则不会正确解析。

You would create a file, say Manifest.txt , containing this (plus a "new line"):您将创建一个文件,例如Manifest.txt ,其中包含以下内容(加上“新行”):

Main-Class: Try

Then create the jar:然后创建 jar:

jar cfm anyname.jar Manifest.txt Try.class

And then run the jar by double clicking it or by running this:然后通过双击或运行以下命令运行 jar:

java -jar anyname.jar

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

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