5

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.

Community
  • 1
  • 1
Chamane
  • 77
  • 1
  • 1
  • 8
  • What error message do you get when you try to run the Jar? Go to a command prompt and type `java -jar .jar` and it should tell you what's going wrong... – vaughandroid Apr 20 '12 at 08:16
  • Hey there, sorry I missed your answer. See my edit please. – Chamane Apr 23 '12 at 13:04
  • From what I gather you're trying to launch it from explorer, and it sounds like it's dying even before it starts the console. You'll likely get more information by launching it from the command line. In cmd (or equivalent) to the folder containing the jar and type `java -jar .jar`. It will hopefully give you some useful info about why it couldn't launch... – vaughandroid Apr 23 '12 at 13:14
  • No Explorer, just the terminal (on a Mac). Ran java -jar Test.jar and I get an exception in the terminal. See my second edit. Seems the problem might not be where I thought it was. – Chamane Apr 23 '12 at 13:42

2 Answers2

4

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.

vaughandroid
  • 4,315
  • 1
  • 27
  • 33
  • 1
    It wasn't checked. Tried it, still doesn't work. I've tried with the first two options for exporting a runnable. – Chamane Apr 19 '12 at 16:28
  • 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 Apr 23 '12 at 16:25
  • Thank you very much, it all works now. I had to use [this](http://stackoverflow.com/questions/2671796/how-to-include-text-files-with-executable-jar) 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. – Chamane Apr 23 '12 at 17:32
  • 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! – vaughandroid Apr 24 '12 at 08:19
  • Should anyone have a similar problem, I posted this [question](http://stackoverflow.com/questions/10286116/read-text-file-that-is-not-in-the-main-package-in-a-runnable-jar). There are still problems to be solved on that question, but there are a few answers. – Chamane Apr 24 '12 at 13:37
1

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

Stefan
  • 12,108
  • 5
  • 47
  • 66
  • 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 Apr 19 '12 at 15:55
  • 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 Apr 19 '12 at 16:28