views:

404

answers:

2

I'm a .Net developer, but for my current project I have to create some Java code. I did some Java coding in the past (I even learned OOP using Java), but am new to Eclipse, which I have to use.

I have to create a few Java console applications that share common functionality. I want to bundle that common functionality into one library (JAR file) and use that file from the command line programs. When I change the library, all dependent console applications also have to be rebuilt.

In Visual Studio (.Net) I would create a new solution and add one "Class Library" project and multiple "Console Application" projects and make them dependent on the Class Library. I'm having trouble recreating the same setup using Eclipse/Java. I guess the Java equivalent of a .Net Class Library is a JAR file. However, I haven't yet found out how to create a JAR file project in Eclipse (I can export a project to a JAR, but that's a manual step; I like this to go automatically) and reference this JAR project from multiple "normal" Java projects.

I hope I've explained myself clearly. Can anybody shed some light?

+1  A: 

In eclipse you can proceed the same way you do in VS:

  1. Create an independent project for each class library (JAR) you want to share functionality with
  2. Create a project for each console application. On this project properties window, make it dependent on the other class library projects by selecting the projects you need

For automatic deployment, you can have an ANT task to make all the automated processing you need to generate all the files.

Pablo Santa Cruz
And how would I create such an ANT task. Can that be done from within Eclipse? Or do I have manually have to create ANT config files?
LeonZandman
You have the two options: create an ANT task with some wizards Eclipse gives you, and manually create and run the ANT task within Eclipse.
Pablo Santa Cruz
Your solution seems to work, although the class library isn't created as a JAR file. I guess I have to use an ANT task for that.
LeonZandman
A: 

Well, i don't know if there is an "automatic" function which can creates the JAR file from your project. Today I'm using Rational Application Developer (very similar to eclipse), but I've used Eclipse and Netbeans too, and i can't remember that feature.

Maybe you can create a keyboard shorcut (read manual..) ;)

By the way, you can't "add a project" to a library. I think it's the other way round. I mean, you reference library from projects, not projects from libraries. What you have to do is:

  • Create your library and export it to a jar.
  • Then create a second project an add that jar to this project.
  • Create more projects and do the same..
  • If you change your library, you just have to replace the jar.

Hope this helps

PD: Seems that Pablo knows what he talks about. I'm just a noob programmer. Read his solution..

eLZahR
Your solution requires too much work and too much manual labour... And I never said I wanted to add a project to a library!?
LeonZandman
"reference this JAR project from multiple "normal" Java projects" <- there. I don't think its a very much complex solution. Anyway, as I said, Pablo's seems more interesting.
eLZahR
Yes, so I said I want to reference the JAR from other projects, which is more or less the same as saying I want to add a library to a project. So why do you persist in saying I mean it the other way around? I really don't get you.Anyway, in the Eclipse Build Path window you can select other projects which should be referenced. So in my console app projects I selected the library project and now everything works.
LeonZandman
I wasn't persisting, just clarifying why i did understand that. I'm sorry if I've offend you.However, that you have done in your eclipse is what I (wanted to) told you to do. May be I should've explained a bit better.. >.<
eLZahR