How to use .jar files in NetBeans?

2019-01-02 21:19发布

问题:

I am completely new to Java development so bear with me...

I have downloaded the Joda time library which consists of three files:

  • joda-time-1.6.jar
  • joda-time-1.6-javadoc.jar
  • joda-time-1.6-sources.jar

Firstly, how do I use this library in one of my own NetBeans projects?

Secondly, generally speaking, what is the distinction between the three files above? What do the labels 'javadoc' and 'sources' signify? How are these jars different from the 'joda-time-1.6.jar' library?

Many thanks for any help.

回答1:

You would add the JAR file to the Libraries in your project:

alt text http://wiki.netbeans.org/wiki/images/b/b7/Adding_jar_AntlrInNetBeansJavaApplication.jpg

As for the different JARs,

  • joda-time-1.6.jar just contains the compiled classes (this is the JAR you need in order to use Joda in your project)
  • joda-time-1.6-javadoc.jar contains the documentation for all the classes
  • joda-time-1.6-sources.jar contains the source code for all the classes


回答2:

In addition to adding JARs or Folders directly to your project, you can add them to the Library and share them among multiple projects. From the Tools menu choose Libraries to get a dialog for adding libraries. Once added, they can be selected from the Add Library... context menu, as shown in Kaleb Brasee's answer. Note, for example, the additions of H2 Database, JFreeChart and JScience:



回答3:

There are many ways to use a .jar file in NetBeans. Kaleb and trashgod have covered two of them. The important thing to do is figure out which is right for you.

If you have a random jar file that has functionality that you want to use in your project, the 'Add Jar/Folder...' option can be a good choice. Your project will become dependent on that jar file. If you start to share the project with other folks, they will need to have that jar to compile the code.

When you add a jar file to the project, the NB Java editor will be able to take advantage of it and provide code completion (display method signatures in a popup that you can select).

Taking the extra time to create a Library for the Joda Time may be a better choice in this particular case. By creating a library, you can associate the javadoc with the functionality. This makes code completion in the NB editor even more useful, since NB will show you the javadoc associated with the method signatures that you can select. Good javadoc will help you decide which method is really the right one for you. You can also associate the source of the library with the jar. This becomes very useful when you start to debug code, since you will be able to 'step into' the library code. These are great additional features that are not supported by using the 'Add Jar/Folder...' option.

Creating a library does have some disadvantages, though...

Folks that want to compile your code would need to create a similar Library, which will be a bit more work.



回答4:

When you create a new project there is a library folder in the project viewer. Right click on that and pick the add jar file option. Once you add the joda-time-jar you can associate either the sources or the javadoc jar with that joda-time.jar. Associating the source or the java doc allows netbeans to provide intellisense.

  • Joda-time.jar contains the Joda library .class files.
  • joda-time-javadoc contains the javadoc html files that describe the library api.
  • joda-time-sources contains the actual java source files for the library.


回答5:

If it is a maven base project the above will not work and you should use : build maven project with propriatery libraries included



标签: