为什么从 Eclipse 导出的可运行 JAR 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6371674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:27:30  来源:igfitidea点击:

Why doesn't a runnable JAR exported from Eclipse work?

javaeclipsejarexecutable-jar

提问by Cobra_Fast

I have a project which runs fine from within Eclipse. But when I export it to a runnable JAR with dependencies packed into it it won't run. The error, when running the JAR from console is:

我有一个在 Eclipse 中运行良好的项目。但是当我将它导出到一个可运行的 JAR 并将依赖项打包到它时它不会运行。从控制台运行 JAR 时的错误是:

EDIT (didnt run it with -jar before):

编辑(之前没有用 -jar 运行它):

INFO: Loading XML bean definitions from class path resource [applicationContext-
framework.xml]
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOExc
eption parsing XML document from class path resource [applicationContext-framewo
rk.xml]; nested exception is java.io.FileNotFoundException: class path resource
[applicationContext-framework.xml] cannot be opened because it does not exist
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:349)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:310)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
        at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:113)
        at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:80)
        at org.springframework.context.support.AbstractRefreshableApplicationCon
text.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
        at org.springframework.context.support.AbstractApplicationContext.obtain
FreshBeanFactory(AbstractApplicationContext.java:422)
        at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:352)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:93)
        at com.horstmann.violet.UMLEditorApplication.getApplicationContext(UMLEd
itorApplication.java:111)
        at com.horstmann.violet.UMLEditorApplication.<init>(UMLEditorApplication
.java:94)
        at com.horstmann.violet.UMLEditorApplication.main(UMLEditorApplication.j
ava:84)
        ... 5 more
Caused by: java.io.FileNotFoundException: class path resource [applicationContex
t-framework.xml] cannot be opened because it does not exist
        at org.springframework.core.io.ClassPathResource.getInputStream(ClassPat
hResource.java:143)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:336)
        ... 20 more 

but the file is in resources/from the JAR-root.

但该文件resources/来自 JAR 根目录。

What am I missing or doing wrong?

我错过了什么或做错了什么?

回答by Alf

Try to click on the option “Package required libraries into generated JAR”.

尝试单击选项“将所需的库打包到生成的 JAR 中”。

A JAR file is a regular compressed archive. Make sure that all the required libraries in your MANIFEST.MF file are present within the generated file.

JAR 文件是常规压缩存档。确保 MANIFEST.MF 文件中所有必需的库都存在于生成的文件中。

回答by Michael Borgwardt

Try to run the JAR from the command line - it will most likely display a useful error message.

尝试从命令行运行 JAR - 它很可能会显示有用的错误消息。

There probably are some dependencies that eclipse doesn't know about or which aren't configured correctly within it, and are therefore missing from the JAR.

可能有一些 eclipse 不知道的依赖项或其中未正确配置的依赖项,因此 JAR 中缺少这些依赖项。

In general, it's not a good idea to rely on IDE functionality to produce deliverables - it depends on too many hidden factors and can't be automated for a build server. Any project that's beyond the experimental/toy stage should have an automated build via Ant or Maven.

通常,依靠 IDE 功能来生成可交付成果并不是一个好主意——它依赖于太多隐藏因素,并且无法为构建服务器实现自动化。任何超出实验/玩具阶段的项目都应该通过 Ant 或 Maven 进行自动构建。

回答by Thorbj?rn Ravn Andersen

The message

消息

Could not find the main class: violet-0.21.2-SVN.jar.  Program will exit.

indicates that you have gotten the Main-Class: MyPackage.MyClassline wrong in the Manifest when packaging together the jar file. You need to give the full name of the class with the main(String[] args) method.

表示您Main-Class: MyPackage.MyClass在将 jar 文件打包在一起时在 Manifest 中弄错了行。您需要使用 main(String[] args) 方法给出类的全名。

See http://download.oracle.com/javase/tutorial/deployment/jar/appman.htmlfor details.

有关详细信息,请参阅http://download.oracle.com/javase/tutorial/deployment/jar/appman.html