如何在 netbeans (java) 中使用导入库中的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20987577/
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 do I use classes from an imported library in netbeans (java)
提问by Craig Lafferty
This must be the simplest thing. I have been trying to do this for hours. I know how to import libraries into netbeans but how do I use classes in these libraries using imports? I have been trying unsuccessfully for hours to use the javacv library on 64bit linux and when I try to import the necessary files from the library netbeans tells me the package does not exist. I want to access a java file example.java
inside com.googlecode.javacv.cpp.linux-x86_64
which is inside opencv-2.4.8-linux-x86_64.jar
. How do I actually import this so I can do the following: example obj = new example
?
这应该是最简单的事情了。我一直试图这样做几个小时。我知道如何将库导入到 netbeans 中,但是如何使用导入来使用这些库中的类?我一直在尝试在 64 位 linux 上使用 javacv 库几个小时都没有成功,当我尝试从库中导入必要的文件时,netbeans 告诉我该包不存在。我想访问一个java文件example.java
里面com.googlecode.javacv.cpp.linux-x86_64
这里面opencv-2.4.8-linux-x86_64.jar
。实际上,我怎么导入这个,所以我可以做到以下几点:example obj = new example
?
Also, in almost every reference to javacv I see this import com.googlecode.javacv.CanvasFrame;
but nowhere have I actually seen this in any of the files readily available by google. What's the deal?
此外,在几乎所有对 javacv 的引用中,我都看到了这一点,import com.googlecode.javacv.CanvasFrame;
但实际上我在谷歌随时可用的任何文件中都没有看到过这一点。这是怎么回事?
回答by Rugal
In netbeans, it is rather easy to iomport a external jar
and utilize it.
First you need to add jar
file in project tab by right clicking on the library
node.
Then, in this project, just import the.name
in your java file.
This should work, I have used Netbeans for three years
在 netbeans 中,很容易导入jar
并使用它。
首先,您需要jar
通过右键单击library
节点在项目选项卡中添加文件。
然后,在这个项目中,就import the.name
在你的 java 文件中。
这应该有效,我已经使用 Netbeans 三年了
回答by Paul Samsotha
You many need to do one of the following option.
您需要执行以下选项之一。
- Add an new Library to NetBeans, then add it to you project
- If you
Tools -> Library
a popup will come up. - There you can add a new library. Click on
New Library
and name the new Library. - Import a library from the
ClassPath
tabAdd Jar/Folder
- If you want to can also add the javadoc from the
JavaDoc
tab. You would add the file from the library file you downloaded, calledjavadoc
. This will allow for the javadocs to be seen while editing your code. - Also you can add the sources file form the
Sources
tab. Import the file namedsources
- Then click on the
Libraries
folder icon in your project andAdd Library
- If you
- Another option is just add the
.jar
.- Right-click on the
Libraries
folder icon in your project and clickAdd Jar
- Locate the
.jar
file and add it.
- Right-click on the
- 将新库添加到 NetBeans,然后将其添加到您的项目中
- 如果你
Tools -> Library
会弹出一个弹出窗口。 - 在那里您可以添加一个新库。单击
New Library
并命名新库。 - 从
ClassPath
选项卡导入库Add Jar/Folder
- 如果您愿意,还可以从
JavaDoc
选项卡中添加 javadoc 。您将从下载的库文件中添加文件,称为javadoc
. 这将允许在编辑代码时看到 javadoc。 - 您也可以从
Sources
选项卡中添加源文件。导入名为的文件sources
- 然后单击
Libraries
项目中的文件夹图标并Add Library
- 如果你
- 另一种选择是添加
.jar
.- 右键单击
Libraries
项目中的文件夹图标,然后单击Add Jar
- 找到该
.jar
文件并添加它。
- 右键单击
回答by Cybernetic Cranium
As long as you have: package MyProject;
只要你有:打包MyProject;
At the top of your project Java wont recognize it. * It seems to be like some sort of sandboxing affect. * To use your classes you have to open the directory you * Saved the project to and Copy/Paste the library to the source file. * I also found that simply deleting the named package works as well. * But now you're no longer linked to that package. -shrugs-
在您的项目顶部 Java 不会识别它。* 这似乎是某种沙盒效应。* 要使用您的类,您必须打开您保存项目的目录并将库复制/粘贴到源文件。* 我还发现简单地删除命名包也可以。* 但现在您不再链接到该包。-耸肩-
回答by almost a beginner
Please give this a try, I will be as specific as possible:
请试一试,我会尽可能具体:
- Copy the library folder you want to use
- Paste it in the "src" folder of your project folder
- 复制要使用的库文件夹
- 将其粘贴到项目文件夹的“src”文件夹中
Once you completed the above two tasks, you can import different classes from the library using the following code:
完成上述两项任务后,您可以使用以下代码从库中导入不同的类:
import nameOfLibraryFolder.*;
The above code will import all the classes from within the library. Replace "nameOfLibraryFolder" with the name of the library folder, to be more specific, the folder that contains all the classes of the library, to be even more specific, the folder that you placed in your project's subfolder called "src".
上面的代码将从库中导入所有类。将“nameOfLibraryFolder”替换为库文件夹的名称,更具体地说,包含库所有类的文件夹,更具体地说,是您放置在项目子文件夹中的文件夹,称为“src”。
In order to import a specific class from the library, you need to replace the * symbol with the class name.
为了从库中导入特定的类,您需要用类名替换 * 符号。
For example:
例如:
import nameOfLibraryFolder.className;
but if the class is in a subfolder, then:
但如果该类位于子文件夹中,则:
import nameOfLibraryFolder.subFolderName.className;
I hope this is specific enough, and I hope it helps.
我希望这足够具体,我希望它有所帮助。
回答by Guneet Singh
- Download the .jar file from net.
- Right click on the libraries under the source package.
- Click on add .jar/zip and select the file.
- Now the .jar file will show with the libraries.
- import [name of class ] in the program.
- 从网上下载 .jar 文件。
- 右键单击源包下的库。
- 单击添加 .jar/zip 并选择文件。
- 现在 .jar 文件将与库一起显示。
- 在程序中导入[类名]。
example :
例子 :
- download
servlet.jar
- Add it to the library
import javax.servlet
- 下载
servlet.jar
- 将其添加到库中
import javax.servlet
this name like javax.servlet
u can see the name of classes from the .jar file and import accordingly.
这个名字就像javax.servlet
你可以从 .jar 文件中看到类的名称并相应地导入。