views:

365

answers:

5

I have few class files that are required for my project. I have added them in the source folder itself in appropriate folder structure. My limitation is that I have to deliver a single jar file with all dependent classes. It cannot be an executable jar file.

Now the problem I am facing is that when I selected export in eclipse to export the jar file, I am not getting the class files (.class) I have added in source folder, getting exported. They are not even getting listed in the package explorer. How can I acheive this? any export setting need to be modified?

To be pricise, my project is like this

project
     src
       com
         test
            file1.java
            file2.java
          external
            class1.class
            class2.class

Now I want the generated classes for my source files and the class files that I have added myself all to be exported into the jar.

A: 

First, I am not sure your classes could work if they are not in their respective directory representing their packages.

I.e.

project
     src
       com
         test
            file1.java
            file2.java
          external
            org
              extproject
                subpackage
                  class1.class
                  class2.class

Then, I would rather try to add a linked folder referencing the root directory of those classes, and select that folder when exporting the project to a Jar.

project
     src
       com
         test
            file1.java
            file2.java
     org (linked to c:/another/path/to/ext/project/classes)
       extproject
         subpackage
            class1.class
            class2.class
VonC
A: 

Whenever I implement an application that needs to be delivered to a customer, I include a build script of some kind for building the distribution from the command line. For example, an Ant "build.xml" file, a Maven "pom.xml" file or even a Makefile. To deal with your particular situation, the script would include a step that assembled all the relevant classes into a temporary directory tree and created the distro JAR file from it.

I don't like to be dependent on the vagaries of some IDE for doing production builds.

Stephen C
A: 

I would use Maven and the Maven Assembly Plugin - this makes it really easy to make a jar with dependencies - or even an executable jar.

The downside is that it takes a while to learn how to make the most of Maven - but you will find life much simpler once you have altered your project to use it. As you are using Eclipse I can reccomend this plugin: M2 Eclipse Plugin

Grouchal
A: 

The src folder normally is not used to keep the .class files. Eclipse looks to already built .class files and create the jar file. Therefore you can add your external .class files into a "lib or something else" folder under your project. Then build your project and export the jar file. you will see that the jar file now contains a folder named lib or something else and your external .class files in it.

miette
+2  A: 

Maybe you need to export Runnable JAR file?

Edit: If you have a lot of dependencies, then a jar shrinker/obfuscator is your best friend. See Proguard or yGuard. For Proguard, there is an Eclipse plugin. Or you can use Ant to integrate either of them manually.

thSoft
Yes. This is an option that I will consider if I don't have so many libraries referenced, apart from those class files.
sarav
Edited answer to include obfuscator info which might be helpful.
thSoft
Thanks. That is helpful.
sarav