Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a different Java project in Eclipse

I have Project1 and Project2. Project1 is dependent of Project2. I am sort of tired that every time I make some code changes in Project2, I have to Export Project2 JAR file, and copy it into lib folder of Project1.

Is there a way to achieve this automatically? Or, is there any other way to let know Project1 that Project2 had some changes?

In Build Path of the project, there is a way to specify references to other projects in Workspace, but this doesn't seem to do anything? What does it actually do?

I was reading somewhere that I can use Deployment Properties to automatize this process, but I can't find it.

UPDATE: Uh, I am C# developer and have some experience in Java development (mostly Android development), I might ask stupid questions, so please bear with me...

Project1 is standard Java Project (Run on client's machine, uses swing, etc..). Project2 is Dynamic Web Project (for to me unknown reason). The only thing it does is calling webservice (third project I don't need to worry about) and passing result back to Project1. Project2 contains those Axis2 webserviceStub.java files... Every know and then, I have to recreate these stub files then I have to export JAR file and move it to Project1. Project2, even though it is Web Project, it is actually code that runs on client.

Just simple project referencing in Build Path doesn't work for me.

Thanks

like image 556
bobetko Avatar asked Apr 10 '12 14:04

bobetko


People also ask

How do I reference a project in Eclipse?

Right-click the file, and then: Click Link artifact to project. A window that lists the projects in the current Eclipse workspace opens. Select the project or folder where you want to place the linked file.

How do I add a project as a dependency of another project in Eclipse?

Instead open the project properties of the second project and select "Java Build Path". On the right side on the "Projects" tab you can add your first project as "required project on the build path". The class files of project 1 are now added to the class path of project 2.


2 Answers

Right click on Project1, then click on Properties. In the dialog that comes up, select Java Build Path, and then click on the Projects tab. There, add Project2 to the build path.

If Project1 is a web app, you need to make sure your Deployment Assembly (same Properties UI) has Project2 there as well.

like image 67
kozyr Avatar answered Oct 09 '22 22:10

kozyr


In Eclipse, when a Java project is built, every .java file from source folders is compiled and the .class file is saved into an output folder. The non-java files from the source folder are copied unchanged to the corresponding folder hierarchy in the output folder.

When adding "referenced projects" within a workspace in Eclipse via Java Build Path, i.e. adding Project2 to Project1's build path, what you are actually doing is telling the project builder to use the output folder of Project2 when building Project1 (NOTE: not only the output folder, but also exported libraries).

So if the answer from kozyr does not work for you, check the "output folder" configuration of Project2.

like image 31
jalopaba Avatar answered Oct 09 '22 22:10

jalopaba