Question

I have an issue that is really annoying right now.

For a school project (that is due on monday :( ), I have to submit a .JAR file that is a stand alone app and that includes sources.

However, in Eclipse, I didn't find how to export the sources and at the same time, include the required libraries.

My BuildPath is set up this way :

http://image.noelshack.com/fichiers/2014/19/1399746312-owp08.png

When I do :

  • Export as a runnable JAR file, everything works in my program but I don't have the sources inside the JAR

  • Export as a JAR File, I can add my sources, but when i try to run the JAR file, I have this exception :

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Here are my settings :

http://image.noelshack.com/fichiers/2014/19/1399746469-sans-titre.png

I really don't know what to do, I've been searching for hours now, can someone help me ?

Thank you in advance.

Regards,

Azsde.

Was it helpful?

Solution

Well, the short answer is that eclipse doesn't support doing that directly. This is why most people use other tools such as Ant or Maven in their projects, rather than relying on the IDE.

You can work around eclipse's lack of an export source option in the runnable jar export wizard in one of two ways:

  • You can create a runnable jar and then manually add your source files to it
  • You can create a normal jar which includes your source files and add a MANIFEST.MF file to it.


Adding source files manually

This is what I would normally consider a terrible option, but since this is a homework assignment, there are two mitigating factors:

  • this is a one-off
  • you are short on time

If you have neither the time nor inclination to learn the details about manifests, I would recommend this option.


Adding your own MANIFEST.MF

As you may have noticed, even when you select the option to have eclipse generate your manifest in the export wizard, your jar file won't actually run. If you open up the 'normal' jar file and look at the generated manifest file it will look something like this (for a simple HelloWorld program using one third party library):

Manifest-Version: 1.0
Main-Class: HelloWorld

Whereas if you open up the manifest file for an exported runnable jar it will look something more like this (for the same program):

Manifest-Version: 1.0
Rsrc-Class-Path: ./ commons-lang3-3.3.1.jar
Class-Path: .
Rsrc-Main-Class: HelloWorld
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

As you can see, the generated manifest file is failing to include support for third party libraries.

What you would need to do in this case is write your own manifest file and keep it as a resource in your project. Then do a normal jar export, and on the last page of the wizard select the Use existing manifest from workspace option, and point it at your own manifest file.

This will have the advantage that you can export a new runnable jar whenever you like, including your source files, and without requiring manual tinkering each time.

If you're interested in working with manifest files, there's a good tutorial on oracle's website here that can help you get started:

http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top