Runnable JAR不能使用引用的库 [英] Runnable JAR not working with referenced libraries

查看:171
本文介绍了Runnable JAR不能使用引用的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可运行的JAR,包括引用的库,即杰克逊,在日食中通过导出。以前,在Eclipse提供导出到可运行的JAR之前,曾经是的fat jar 似乎是goto解决方案。



从这个问题,第一个选项将是我正在寻找的。既然这是一个小项目,总是把所有东西都放在一起是不成问题的(加上对我来说更容易,因为我刚刚开始使用JAR,runnable和jnlp)



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



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



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



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



Eclipse
版本:Indigo Service Release 2
Build id:20120216-1857






编辑以下 Baqueta 的答案



当我这样做会发生什么,我得到一个自定义的RuntimeException。我在这里有些困惑这是我的代码的简单说明。
我有一个使用引用库(jackson)的实用程序类(UtilityJSON)。该实用程序类的构造函数实例化了引用库中定义的对象,我们称之为映射器。
UtilityJSON的一种方法使用mapper的方法。此时异常被提出。为什么我很困惑的是,映射器已成功实例化,但使用其方法引发异常。如果jar找不到引用的库,那么它可能无法创建对象mapper。但是从Eclipse运行这个项目是很好的。
运行可执行jar时是否有任何调试方式?可能会在控制台中显示一些东西。






编辑2



我可能发现错误。我的程序在项目中的文件夹中读取* .txt文件。当浏览jar的内容时,我看到这个文件夹没有被包含,这样就是异常。所以问题现在变成:我该如何包括这个文件夹?它位于项目的根文件夹中。

解决方案

您可以尝试以下操作:


  1. 在Eclipse中,转到Project->属性。

  2. 从左侧的列中选择Java构建路径。

  3. 选择订单和导出标签。

  4. 在列表中查找杰克逊,并确保已检查。

  5. <

    编辑
    要回答您更新的问题:



    在Eclipse中,找到Package Explorer中的* .txt文件。右键单击它并选择Build Path-> Add to Build Path。然后按照上面的说明确保文件被包含在Jar中。最后,做一个干净和重建。



    如果你要添加更多的资源,通常的做法(有道理!)有一个资源文件夹通常称为'res')。然后,您可以将整个文件夹添加到构建路径,以便其中的所有资源都包含在Jar中。


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

    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)

    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 Version: Indigo Service Release 2 Build id: 20120216-1857


    Edit following Baqueta's answer

    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 (jackson). 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.


    Edit 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.

    解决方案

    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 'jackson' in the list and make sure it's checked.

    EDIT To 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.

    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.

    这篇关于Runnable JAR不能使用引用的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆