eclipse 可运行的 JAR 不能与引用的库一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10231544/
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 18:03:45  来源:igfitidea点击:

Runnable JAR not working with referenced libraries

javaeclipsejarexecutableexecutable-jar

提问by Chamane

I want to create a runnable JAR that include referenced libraries, namely Hymanson, in eclipse via the export. There used to be fat jarthat seemed to be the goto solution before Eclipse offered to export to runnable JAR.

我想通过导出在 Eclipse 中创建一个可运行的 JAR,其中包含引用的库,即Hymanson。在 Eclipse 提供导出到可运行 JAR 之前,曾经有一个胖 jar似乎是 goto 解决方案。

From this question, the first option would be what I'm looking for. Since it is for the moment a small project, it wouldn't be a problem to always have everything together (plus it's easier for me since I'm just starting with JARs, runnables and jnlp)

从这个问题来看,第一个选项是我正在寻找的。由于目前它是一个小项目,因此始终将所有内容放在一起不会有问题(而且对我来说更容易,因为我刚开始使用 JAR、runnables 和 jnlp)

When I try to launch it, there's an alert window saying there was a problem and to try to find it in the console (which I can't get to open).

当我尝试启动它时,会出现一个警告窗口,提示出现问题并尝试在控制台中找到它(我无法打开它)。

If I remove any use of this referenced library everything runs perfectly (except the part that it's not doing what I want at all). The window looks the way I designed it, but no functionality without the library.

如果我删除对这个引用库的任何使用,一切都会完美运行(除了它根本没有做我想要的部分)。该窗口看起来像我设计的那样,但没有库就没有任何功能。

I don't even have a stack trace to help with the problem. I've created other projects without any referenced libraries and everything is fine. So I've pretty much narrowed it down.

我什至没有堆栈跟踪来帮助解决这个问题。我创建了没有任何引用库的其他项目,一切都很好。所以我几乎缩小了范围。

Since I let Eclipse handle the exporting and everything, I don't suppose you need the manifest to see if it is alright. Any questions you might have, I'll provide the informations you need. It goes without saying that the project runs directly from Eclipse.

由于我让 Eclipse 处理导出和所有事情,我认为您不需要清单来查看它是否正常。您可能有任何问题,我会提供您需要的信息。不用说,该项目直接从 Eclipse 运行。

Eclipse Version: Indigo Service Release 2 Build id: 20120216-1857

Eclipse 版本:Indigo Service Release 2 版本号:20120216-1857



Edit following Baqueta's answer

按照Baqueta的回答进行编辑

What happens when I do that is that I get a custom RuntimeException. I'm a bit baffled here. Here's a simple explanation of my code. I have a utility class (UtilityJSON) that uses the referenced library (Hymanson). The constructor of that utility class instantiate an object defined in the referenced library, let's call it mapper. A method of UtilityJSON uses a method of 'mapper'. At this point the exception is raised. Why I'm baffled is that 'mapper' is succesfully instantiated but using its method raises an exception. If the jar couldn't find the referenced library, it probably couldn't create the object 'mapper'. But running the project from Eclipse is all fine. Is there any way to debug when running an executable jar? Maybe echo something in the console.

当我这样做时会发生什么我得到了一个自定义的 RuntimeException。我在这里有点困惑。这是我的代码的简单解释。我有一个使用引用库 (Hymanson) 的实用程序类 (UtilityJSON)。该实用程序类的构造函数实例化了一个在引用库中定义的对象,我们称之为映射器。UtilityJSON 的方法使用“mapper”的方法。此时引发异常。为什么我感到困惑的是“映射器”已成功实例化,但使用其方法会引发异常。如果 jar 找不到引用的库,它可能无法创建对象“映射器”。但是从 Eclipse 运行项目一切正常。运行可执行jar时有什么方法可以调试吗?也许在控制台中回显一些东西。



Edit 2

编辑 2

I may have found the error. My program reads a *.txt file in a folder inside the project. When exploring the content of the jar I see that this folder was not included, thus the exception. So the question now becomes: how do I include this folder? It's in the root folder of the project.

我可能发现了错误。我的程序读取项目内文件夹中的 *.txt 文件。在探索 jar 的内容时,我发现该文件夹未包含在内,因此是例外。所以现在的问题变成了:我如何包含这个文件夹?它位于项目的根文件夹中。

采纳答案by vaughandroid

You could try the following:

您可以尝试以下操作:

  1. In Eclipse, go to Project->Properties.
  2. Select 'Java Build Path' from the column on the left.
  3. Select the 'Order and Export' tab.
  4. Find 'Hymanson' in the list and make sure it's checked.
  1. 在 Eclipse 中,转到 Project->Properties。
  2. 从左侧的列中选择“Java Build Path”。
  3. 选择“订购和导出”选项卡。
  4. 在列表中找到“Hymanson”并确保它被选中。

EDITTo answer your updated question:

编辑回答您更新的问题:

In Eclipse, find the *.txt file in the Package Explorer. Right-click it and select Build Path->Add to Build Path. Then follow the instructions above to make sure the file gets included in the Jar. Finally, do a clean and rebuild.

在 Eclipse 中,在 Package Explorer 中找到 *.txt 文件。右键单击它并选择构建路径->添加到构建路径。然后按照上面的说明确保文件包含在 Jar 中。最后,进行清理和重建。

If you're ever going to add more resources, it is common practice (and makes sense!) to have a resources folder (often called 'res'). You can then add the entire folder to the build path, so that all the resources in there get included in the Jar.

如果您打算添加更多资源,通常的做法是(而且很有意义!)拥有一个资源文件夹(通常称为“res”)。然后,您可以将整个文件夹添加到构建路径,以便其中的所有资源都包含在 Jar 中。

回答by Stefan

Enable the java console by writing

通过编写启用java控制台

javaws -viewer

in a terminal. Close the Cache Viewer and click the advanced tab. There open Java-Console and click "enable console".

在一个终端。关闭缓存查看器并单击高级选项卡。在那里打开 Java-Console 并单击“启用控制台”。