Java 如何在 NetBeans 中使用 .jar 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1975973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use .jar files in NetBeans?
提问by MalcomTucker
I am completely new to Java development so bear with me...
我对 Java 开发完全陌生,所以请耐心等待...
I have downloaded the Joda time library which consists of three files:
我已经下载了由三个文件组成的 Joda 时间库:
- joda-time-1.6.jar
- joda-time-1.6-javadoc.jar
- joda-time-1.6-sources.jar
- 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?
首先,我如何在我自己的 NetBeans 项目之一中使用这个库?
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?
其次,一般来说,上面三个文件的区别是什么?标签“javadoc”和“来源”表示什么?这些 jar 与 'joda-time-1.6.jar' 库有何不同?
Many thanks for any help.
非常感谢您的帮助。
回答by Kaleb Brasee
You would add the JAR file to the Libraries in your project:
您可以将 JAR 文件添加到项目中的库中:
(source: netbeans.org)
(来源:netbeans.org)
As for the different JARs,
至于不同的 JAR,
- joda-time-1.6.jarjust contains the compiled classes (this is the JAR you need in order to use Joda in your project)
- joda-time-1.6-javadoc.jarcontains the documentation for all the classes
- joda-time-1.6-sources.jarcontains the source code for all the classes
- joda-time-1.6.jar只包含已编译的类(这是在项目中使用 Joda 所需的 JAR)
- joda-time-1.6-javadoc.jar包含所有类的文档
- joda-time-1.6-sources.jar包含所有类的源代码
回答by ssteidl
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.
创建新项目时,项目查看器中有一个库文件夹。右键单击它并选择添加 jar 文件选项。添加 joda-time-jar 后,您可以将源代码或 javadoc jar 与该 joda-time.jar 相关联。关联源或 java 文档允许 netbeans 提供智能感知。
- 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.
- Joda-time.jar 包含 Joda 库 .class 文件。
- joda-time-javadoc 包含描述库 api 的 javadoc html 文件。
- joda-time-sources 包含库的实际 java 源文件。
回答by trashgod
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 Toolsmenu choose Librariesto 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:
除了将 JAR 或文件夹直接添加到您的项目之外,您还可以将它们添加到库中并在多个项目之间共享。从工具菜单中选择库以获取用于添加库的对话框。添加后,可以从Add Library...上下文菜单中选择它们,如 Kaleb Brasee 的回答所示。注意,例如 H2 Database、JFreeChart 和 JScience 的添加:
回答by vkraemer
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.
在 NetBeans 中有多种使用 .jar 文件的方法。Kaleb 和垃圾神已经覆盖了其中的两个。重要的事情是弄清楚哪种适合您。
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.
如果您有一个随机的 jar 文件,它具有要在项目中使用的功能,则“添加 Jar/文件夹...”选项可能是一个不错的选择。您的项目将依赖于该 jar 文件。如果您开始与其他人共享该项目,他们将需要使用该 jar 来编译代码。
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).
当您向项目添加 jar 文件时,NB Java 编辑器将能够利用它并提供代码完成(在您可以选择的弹出窗口中显示方法签名)。
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.
在这种特殊情况下,花额外的时间为 Joda 时间创建一个库可能是更好的选择。通过创建库,您可以将 javadoc 与功能相关联。这使得 NB 编辑器中的代码完成更加有用,因为 NB 将向您显示与您可以选择的方法签名相关联的 javadoc。好的 javadoc 将帮助您确定哪种方法真正适合您。您还可以将库的源与 jar 相关联。这在您开始调试代码时变得非常有用,因为您将能够“步入”库代码。这些是使用“添加 Jar/文件夹...”选项不支持的很棒的附加功能。
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.
想要编译你的代码的人需要创建一个类似的库,这将是更多的工作。
回答by Alireza Fattahi
If it is a maven base project the above will not work and you should use : build maven project with propriatery libraries included
如果它是 maven 基础项目,则上述内容将不起作用,您应该使用: 构建带有专有库的 maven 项目