Runnable JAR not working with referenced libraries

12,891

Solution 1

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.

Solution 2

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

Share:
12,891
Chamane
Author by

Chamane

Updated on June 04, 2022

Comments

  • Chamane
    Chamane almost 2 years

    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.

  • Chamane
    Chamane about 12 years
    Didn't know the command line to open the Java Preferences, but I activated it already. Once that radio button is chosen, it should open the console next time I try to run my program, but it doesn't. Just the alert window saying it couldn't be opened.
  • Chamane
    Chamane about 12 years
    It wasn't checked. Tried it, still doesn't work. I've tried with the first two options for exporting a runnable.
  • Chamane
    Chamane about 12 years
    Dont' know the exact translation from french, but it's something like: "Impossible to launch the file Test.jar". Search in the console potential error messages.
  • Chamane
    Chamane about 12 years
    Yup, will select the right answer as soon as I get it to work. It does indeed make sense to have a resource folder. I created one and moved everything there. I update the path I use to open the files and all my JUnit are successful. 'rsc' has been added to Build Path, but the jar is still not running, saying that the file could not be open (no such file or directory). I clearly am close to making it work. So as soon as it does, I'll close this problem.
  • Chamane
    Chamane about 12 years
    Thank you very much, it all works now. I had to use this even though I have no idea at the moment why. To make it work I also had to move my *.txt files directly in the package where they are used, which is absolutely horrifying. There is surely another solution to this and I'll keep looking. As for including referenced library, all is well. Thank you again.
  • vaughandroid
    vaughandroid about 12 years
    Glad to help. If you can't work out for yourself what's going on with the paths in your compiled Jar, I'd suggest searching on here. If you can't find anything that sounds like your problem, then you'll want to open another question. Good luck!
  • Chamane
    Chamane about 12 years
    Should anyone have a similar problem, I posted this question. There are still problems to be solved on that question, but there are a few answers.