1

I have 4 different Java Projects created in Eclipse.

One of them contains all my Utilities and Helper methods, that the other 3 projects use.

I don't want to copy that same package 3 times into each project, since I want to be able to just edit it in one place if I should have to. So, I have referenced the myUtilities project from all other 3 projects.

I'll be exporting the 3 main projects and they will be going to different servers to run independently as "java -jar myproject.jar"

Everytime I do an export, the myUtilities.jar is not getting compiled in the JAR. If I open the JAR, I can see it is not there. And this is going to be a problem when I move them to the other servers.

Is there a way to add the package into the source for the JAR? Or am I going to have to copy the myUtilities.jar into the JAVA_HOME of each server?

This is my current Manifest file:

Manifest-Version: 1.0
Main-Class: com.elcool.process.alpha.RetrieverMain
elcool
  • 6,041
  • 7
  • 29
  • 44

5 Answers5

1

You can place a reference to the external utility jars (myUtility.jar in your question) in the manifest in your project (myproject.jar). The manifest is a text file within the jar of your project, and many build tools support adding information to it.

This way, you can compile your utility jar once, make a reference to it in your projects, and deploy it alongside the three main projects, perhaps in the same directory or a nearby subdirectory.

nrobey
  • 705
  • 4
  • 12
  • I think this is the path I want to take. How do I add another JAR to the Manifest? All I know how to do is place the Main Class. – elcool Jan 23 '11 at 16:29
  • Here is an example within another post: http://stackoverflow.com/questions/682852/apache-ant-manifest-class-path – nrobey Jan 23 '11 at 20:08
1

You should use "Export" -> "Runnable Jar" to have dependent projects included correctly.

There are three different kinds of runnable jars, depending on you want a single jar-file with all classes, a single jar file with dependent jars and a correct Class-Path in the manifest, and a single jar file with the dependent jars enclosed and a special classloader understanding this.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • This would be the easiest, but it will compile ALL references and I don't need ALL of them. I just need the myUtilities.jar to be added to the Jar. – elcool Jan 23 '11 at 16:24
  • Not if you set up projects A, B, C so they only reference your utility project and do the export in e.g. project A only. I suppose that B and C do not know about A? – Thorbjørn Ravn Andersen Jan 23 '11 at 20:11
  • Yeap, A, B and C don't know about each other. But they use other common Java JARS and API Jars from another app that I don't need in the compiled jar. – elcool Jan 23 '11 at 22:40
  • Then I would suggest the "single jar with dependent jars in a folder" and have a later deployment step removing those jars you do not want. This is easy to script with ant. – Thorbjørn Ravn Andersen Jan 24 '11 at 10:08
0

Copy into JAVA_HOME? That's not the way to do anything. You should never do that.

If it's a web project, you'll have to copy the JAR into the WEB-INF/lib for each project.

If it's not a web project, you'll have to package that JAR up with the main JAR and add it to the CLASSPATH. I'll assume that the main is either an executable JAR with a manifest or that you've got a script to execute the main so you can encapsulate the CLASSPATH into it.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • The JAR will run independently, all other JARs (java utils, java io... and so on) are in the java\jre\bin folder. But I don't want to place the myUtilities.jar in that folder, since I'll probably be modifying it again and again. I just want to replace one JAR (the main project jar) – elcool Jan 23 '11 at 16:26
0

You may also add Classpath references in the executable jar file MANIFEST.MF or for more experienced users use maven assembly plugin which will create artefact with all its dependencies packed within a single jar.

Marcin Michalski
  • 1,266
  • 13
  • 17
-1

Everytime I do an export, the myUtilities.jar is not getting compiled in the JAR. If I open the JAR, I can see it is not there.

How do you create it? A jar file is just renamed zip with manifest file, so consider building it yourself in case your tool doesn't suit your need.

maaartinus
  • 44,714
  • 32
  • 161
  • 320