11

I have a project A (for example an Ant based Java Application project) and a second project B (a Java Class Library project or another Java Application project, both Ant build based.)

Project B has some additional Java library dependencies (.jar files in the Libraries folder).

A depends on B. A can use classes which are written by myself in B but it can not see classes in the additional libraries of B unless I add those libraries/jar files to A.

Is there a way to get NetBeans to automatically manage the library dependencies in A by including the dependencies of B in addition to B's own jar file? In eclipse when you add .jar files to project B, it is not necessary to add them to A again.

Thanks.

jla
  • 6,904
  • 2
  • 36
  • 34
  • I would like to know this myself. So far I've just been repeatedly adding common dependency JARs the way you described it. I'm not sure if there's any other way TBH. – predi Apr 25 '12 at 13:50
  • I believe what you are asking is "How do I get NetBeans to automatically include the libraries used in project B in project A without manually adding them to and updating them in both projects. – jla May 10 '19 at 01:06

4 Answers4

5

In your project folder you should see a folder called Dependencies, right click on that folder and choose Add Dependency.... Open the Open Projects tab; from there you can select your project from the list. Click Add.

This works for NetBeans IDE 7.2.1.

Steve Pitchers
  • 7,088
  • 5
  • 41
  • 41
  • In simple Java SE projects there is no Dependency folder. – JHead May 12 '17 at 12:44
  • Maven project types have the Dependencies folder and, if setup correctly, can auto-add child dependencies, see https://stackoverflow.com/questions/9447654/how-do-i-create-use-a-class-library-with-netbeans-on-top-of-maven – jla May 10 '19 at 01:00
2

The simpler NetBeans Ant project Libraries folder is used for compile time and run time dependencies of each individual project. If your "Java Class Library" type Ant based project B has additional library dependencies, they are not automatically forwarded or updated in Project A when you include B as a library of A.

When project A is built, it's .jar file will only contain Project A code and the lib/ folder will only include .jar files from it's Libraries folder. This can cause confusion when you upgrade the dependent library versions and possibly forget to upgrade them everywhere you depend on project B.

You will need to manually manage the dependencies unless you switch to a project management and build system that handles this, like Maven.

A NetBeans Maven project will have a Dependencies folder instead of a Libraries folder and if project B is properly setup and registered as a Maven project and project A is also created as a Maven project, you could Add Dependency on B to A and B would forward it's dependency information to Maven project A.

jla
  • 6,904
  • 2
  • 36
  • 34
0

If you want to add another project as a library in netbeans right click on the libraries directory and select "Add Project", select the project you want to add. When you do this the jar file(in the dist dir) that has been built for the project you are adding will be selected and then click the "Add Project jar files" and that should add the jar file to your project. Alternatively you could select "Add Jar" instead and add the jar file yourself the way that Ravi described.

ChadNC
  • 2,528
  • 4
  • 25
  • 39
  • i need to know is there a way to avoid adding jar files of independant project to dependant project (like eclipse). – Mohammad Reza Kargar Apr 25 '12 at 16:26
  • Once you add the jar files to the libraries directory in the project you will not have to keep adding them to that project and when you build the project netbeans will create a jar for your project in the dist directory for the project and the jar files that added to the libraries directory will be included in that jar. – ChadNC Apr 25 '12 at 16:40
  • dear ChadNC before building the dependant project, we have compile time error becuase it does not see jar files in indepenant project. so we can not build it. – Mohammad Reza Kargar Apr 25 '12 at 20:50
  • ChadNC, This is not an answer to the question. The original question is: how to avoid adding jar files of the project added as library. – JHead May 12 '17 at 12:41
-1

In your project folder you see a folder called Libraries , right click on that folder from there you can select your .jar file. You need not to add .jar files repeatedly after adding once to your project. You just need to Clean and Build your project whenever new files are added to .jar file.

Ravi Jain
  • 1,452
  • 2
  • 17
  • 41
  • 2
    The jar files of the dependent projects are not added automatically by NetBeans. That is the problem. – JHead May 12 '17 at 12:45