13

I have just started playing with Java, and I really like the language. I am using the NetBeans IDE, and I find one odd oversight. I can create a new class in a project, but NetBeans doesn't appear to support any way to import an existing class into a project.

I manage to get it done by going to some other project where the desired class already occurs, and then I copy/paste it into the source code folder of the new project, then change the package name at the top of the pasted file to match the package name of the new project.

Is there a nice direct way to do this from the NetBeans interface?

Thanks for any help on this.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
John R Doner
  • 2,242
  • 8
  • 30
  • 36
  • I don't do Netbeans, but in Eclipse you could just do *Rightclick Project > Build Path > Add Projects* to achieve that. To the point, the other project just needs to be in the classpath (called "Build Path" in most IDE's) of the current project. – BalusC Jan 16 '10 at 04:50

5 Answers5

21

In the Projects window you can expand the project that you are working on. Right Click on Libraries and select Add Project.... You will get a dialog allowing you to select another Netbeans Project with the Java classes that you wish to use.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
  • This, so far as I can tell, only works if the provider project creates a jar file. But this doesn't seem to happen by default. Can someone explain how to make that happen? – Toby Eggitt Jul 08 '13 at 16:10
  • You may need to edit the properties of the provider project and ensure that under the *package* tab that you have a configuration for the jar. This seems to be the default though. – Vincent Ramdhanie Jul 08 '13 at 17:47
  • For a web project, this option describes the war file. Actually, my project is a web project, but I don't want the whole thing, i just want the entity stuff that I created in it. However, for a "plain java" project, this seems to be more useful. _After_ you actually go to that page in the config (but seemingly not before) it creates the jar with the build, and it does update it regularly on subsequent build operations. We get some options to choose the files to exclude (but one cannot exclude .class files, so it is moderately useful). but you can't choose exactly which files "matter" – Toby Eggitt Jul 09 '13 at 00:09
  • If the source code of the imported jar is modified, will the imported jar be updated? – MAZDAK Jun 28 '16 at 16:50
2

You have to just make the .jar file of that/those java files which you want to use in another project. right click on file->export->java->.jar then finish.

Now you can use these jar into your another project.

Chintan Panchal
  • 721
  • 6
  • 11
  • Well, in version 7.3, there's no such menu entry. File->Export Project exists, but that only allows archiving the entire project, source, config files, everything. It's by no means obvious where this might have moved to. – Toby Eggitt Jul 09 '13 at 00:03
1

When you build the java project in netbeans it creates a jar file on the dist folder in the current project directory. You can use this jar file for the other project as a package by adding this as ADD jar in the next project.I have tried this ,i am using my old project jar file for new project development.

Prakash Panjwani
  • 2,540
  • 5
  • 23
  • 34
  • Where do I find the dist folder if I go in my new project in the project explorer to libraries folder--> "add project"? I have build, nbproject, src,.. but no dist. – Timo Dec 03 '13 at 17:11
0

Use the 'Clean and build' your project option from the netbeans application, that creates the 'dist' folder which you are looking for (and obviously can't find because you havn't cleaned&buld)

0

The netbeans itself creates .jar files and you can add the jar file to the current project you are working on.