aspose file tools*
The moose likes Java in General and the fly likes Creating an executable jar for a complex Java project Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating an executable jar for a complex Java project" Watch "Creating an executable jar for a complex Java project" New topic
Author

Creating an executable jar for a complex Java project

Robin Sharma
Ranch Hand

Joined: Aug 24, 2005
Posts: 76
Hi!

I am trying to create an executable jar out of a Java project which uses the jExcel API (jxl.jar). The project has been created using Eclipse 3.1 M7. I used the "Export as Jar file" tool in Eclipse to create the said jar file.
But, when i try to run the jar, it says:
"Fatal exception occured. Program will exit."
It is not able to find the "BiffException" class, says the exception trace.

What could be the reason? The program runs perfectly fine from within Eclipse.

In fact, i am facing this problem only when i refer any external jars in my project. If the project just uses the standard Java api, the executable jar runs fine.

Please help.

Thanks.


DW
There is always a bug :-)
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Most likely the problem is that you have the necessary libraries/APIs (i.e. the external jars) defined in Eclipse, but you are not referencing them when running the application externally. Thus when you run from Eclipse that library (and its classes) is included in your classpath, allowing the JVM to find all the required classes. However, when you are running externally, these additional external jars are not in your classpath (since Eclipse is not doing it for you). This is a potential danger when you test using an IDE; you have to figure out classpath issues when getting ready to deploy.

So you either need to:
  • Define all those external jars in your classpath when you run your application (via the -classpath option)
  • As an alternative to the above, you can define the additional jars in your jar's manifest and simply have the additional jars in the same directory. See http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html for more info
  • Create a Super Jar file by un-jar-ing all the external libraries, and then jar everything (including your code) into a single jar. (Be sure to read any license agreements for the external libraries to verify this is ok). You can use the Ant jar task to do this for you. You'll want to leave the keepcompression option to its default value of false. There are some dangers to this including the loss of code signatures that are located in /META-INF/MANIFEST.MF files, the danger of overwriting files common to multiple libraries (such as README, license.txt, and log4j.properties to name a few), loss of digital signature if the third-party jar is signed, and if the third-party JAR contains JARs itself.
  • Package the external JARs in with your JAR (again you can use the Ant jar task, but set keepcompression to true) and then to load classes in JAR files within your JAR file into the class path, you must write custom code to load those classes.
  • Vote for this (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4648386) request for enhancement to Java so that the above option will happen automatically without the need for custom classloader code. Granted this would be a rather long term solution and there is debate as to whether this is a good idea since some developers argue that "a jar is suppose to be 'an atomic' entity which can be sealed and distributed." I haven't thought about it enough myself to form an opinion on the issue.
  • Check out the SourceForge One-JAR Project for an additional solution: http://one-jar.sourceforge.net/


  • [ March 17, 2006: Message edited by: Mark Vedder ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Creating an executable jar for a complex Java project