33

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

maksimov
  • 5,792
  • 1
  • 30
  • 38
bobetko
  • 5,019
  • 14
  • 58
  • 85

2 Answers2

49

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.

kozyr
  • 1,334
  • 1
  • 20
  • 30
  • 1
    Try refreshing both projects (right click -> Refresh) after you generate the stub files, and see if that helps. Also, if the stubs are in some separate src directory (from the main src dir) in Project2, make sure that dir is also exported in "Order and Export" tab of "Java Build Path" for Project2. – kozyr Apr 10 '12 at 15:33
  • Thanks. Refresh did the trick. Now, the question is... when I am going to deploy Project1 to client, am I going to have to include Project2's JAR file then? Why would original programer opt for copying JAR files insted of for making project reference? – bobetko Apr 10 '12 at 16:07
  • Depends on how you will build Project1 for deployment. If you're just generating a jar file out of Project1 thru Eclipse, you can include the classes from Project2 as well, I believe, at jar export time (just select them in the UI). If there's some ant script that builds up a deployment package, it's a lot simpler to JAR Project2 and include it as part of the deploy. – kozyr Apr 10 '12 at 16:19
  • Wow, my code has only gone and bloody worked after doing this. Thank you.# – thonnor Feb 15 '17 at 16:56
  • can you please clarify how is this different from "Project References" choosing dependent project. – vikramvi May 17 '17 at 15:19
  • Second part of your answer saved my life! – tarares Jan 10 '18 at 21:26
  • To make it explicit, if you have method `a() {` in `project1` & you want to run method `main(String[] args) {` of class `GNuplotDemo.java` of a project named `javagnuplothybrid`, then you don't have to put the project `javagnuplothybrid` in the `src` folder of `project1`, you can put it in same folder as the `project1` folder is located. After adding to **Java Build Path** like @kozyr explained, you can just* run the main with: `String[] empty =new String[0];` and `JGnuplotDemo.main(empty);` in method `a() {`.*Assuming the javagnuplothybrit is visible/imported in your Eclipse Package Explorer. – a.t. Apr 06 '19 at 07:18
7

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.

jalopaba
  • 8,039
  • 2
  • 44
  • 57