简体   繁体   中英

Runnable JAR not working with referenced libraries

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.

Enable the java console by writing

javaws -viewer

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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