7

Possible Duplicate:
How to determine which classes are used by a Java program?

I have a Java project on Eclipse that contains a lot of Java classes. I want to export one of them to a .jar file but I ran into some problems.

I already read through some existing threads, but I didn't get the right answer for my requirement, because:

If I choose "Export to JAR file" I get the right class exported, but the .jar file does not contain the required dependencies to run the jar file.

If I choose "Export to runnable JAR file" the resulting jar file contains all dependencies but also puts in all the other classes from the whole project (also those that I don't need).

How can I export the single Java class to a .jar file that includes also required libraries for running the jar file? The resulting jar file has to be as small as possible.

Thanks a lot.

Community
  • 1
  • 1
fachexot
  • 491
  • 1
  • 8
  • 24
  • Are you using maven? Checkout the [assembly plugin](http://maven.apache.org/plugins/maven-assembly-plugin/) – Pablo Lascano Jun 18 '12 at 17:10
  • I have the same question - I have written a simple program and with all the commons stuff, it weighs over 3Meg! I am sure if I could build the jar with just the classes it actually needs it would be tiny. – Tom Carnell Aug 23 '12 at 16:04

3 Answers3

1

You could create a new project with only the class that you need in it and export that project as a runnable jar.

Poindexter
  • 2,396
  • 16
  • 19
  • That's the solution for just one file, but then I have to create a new Project each time I have to export a single class. In a big project that could mean a lot of work. Or even when I need only one class but that class needs other classes out of the project, I have to look for every class needed and move it to the new project. Why is there no automatic possibility to keep exported applications small? – fachexot Jun 18 '12 at 16:59
0

There might be a workaround, depending on the use-case you need this for. Generally, a full dependency analysis will not be possible since your code might load classes at runtime etc. If you need the .jars to be as small as possible (e.g. if you want to be able to "klickstart" parts of the application and not waste storage redundantly) you might use

Export... -> Runnable JAR File -> Copy required libraries into a sub-folder...

ant then keep only one copy of the external libraries for all jars. Please provide more info on what your use-case is, so that we can post more specific answers :)

jvataman
  • 1,357
  • 1
  • 12
  • 13
  • The use-case is simple. I have one project that consists of many frameworks (external libraries, source files, etc) and many class files (some of them with a main method). Now I want to export each of the classes with a main method in a runnable jar file. Then, I want to quantify the size of the different jars. If I use Export as runnable Jar file, Eclipse puts in ALL the classes of the whole project into that jar file. If I use Export Jar only, the exported jar will not include required libraries etc. I know the project is kind of chaos, but I have not implemented it, I just wanna analyze it. – fachexot Jun 19 '12 at 16:46
0

Try this, this is the way to create a jar or runnable jar in eclipse

File -> Export-> Java ->Runnbale JAR file

Launch configuration : your Class containing the public static void main(String[] args)

Export destination : Target place

Library Handling:

Package required libraries into generated JAR

FINISH
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • This is the way Im doing it already. Unfortunately this does not solve my problem since the runnable jar contains all files of the whole Eclipse project. – fachexot Jun 24 '12 at 19:07
  • @fachexot Same problem here... still no solution? – mjs Oct 17 '14 at 18:24