Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 944
Chamane Avatar asked Apr 19 '12 15:04

Chamane


People also ask

How do I export a JAR file including reference library in Eclipse?

To export your project, right-click it and select Export. Select Java > Runnable JAR file as the export destination and click Next. On the next page, specify the name and path of the JAR file to create and select the Launch configuration that includes the project name and the name of the test class.

How do I create a JAR file with an external library?

If it is a standalone (Main method) java project then Not any specific path put all the jars inside the project not any specific path then right click on the project - > export - > Runnable jar --> Select the lunch configuration and Library handeling then choose the radio button option "Package required libraries into ...

What is the difference between runnable jar and executable jar?

FYI: In simple terms, the difference between a JAR file and a Runnable JAR is that while a JAR file is a Java application which requires a command line to run, a runnable JAR file can be directly executed by double clicking it.


1 Answers

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.

like image 99
vaughandroid Avatar answered Sep 22 '22 05:09

vaughandroid