如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:45:09  来源:igfitidea点击:

How do I use classes from an imported library in netbeans (java)

javanetbeans

提问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.javainside com.googlecode.javacv.cpp.linux-x86_64which 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 jarand utilize it.
First you need to add jarfile in project tab by right clicking on the librarynode.
Then, in this project, just import the.namein 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
    1. If you Tools -> Librarya popup will come up.
    2. There you can add a new library. Click on New Libraryand name the new Library.
    3. Import a library from the ClassPathtab Add Jar/Folder
    4. If you want to can also add the javadoc from the JavaDoctab. You would add the file from the library file you downloaded, called javadoc. This will allow for the javadocs to be seen while editing your code.
    5. Also you can add the sources file form the Sourcestab. Import the file named sources
    6. Then click on the Librariesfolder icon in your project and Add Library
  • Another option is just add the .jar.
    1. Right-click on the Librariesfolder icon in your project and click Add Jar
    2. Locate the .jarfile and add it.
  • 将新库添加到 NetBeans,然后将其添加到您的项目中
    1. 如果你Tools -> Library会弹出一个弹出窗口。
    2. 在那里您可以添加一个新库。单击New Library并命名新库。
    3. ClassPath选项卡导入库Add Jar/Folder
    4. 如果您愿意,还可以从JavaDoc选项卡中添加 javadoc 。您将从下载的库文件中添加文件,称为javadoc. 这将允许在编辑代码时看到 javadoc。
    5. 您也可以从Sources选项卡中添加源文件。导入名为的文件sources
    6. 然后单击Libraries项目中的文件夹图标并Add Library
  • 另一种选择是添加.jar.
    1. 右键单击Libraries项目中的文件夹图标,然后单击Add Jar
    2. 找到该.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:

请试一试,我会尽可能具体:

  1. Copy the library folder you want to use
  2. Paste it in the "src" folder of your project folder
  1. 复制要使用的库文件夹
  2. 将其粘贴到项目文件夹的“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 :

例子 :

  1. download servlet.jar
  2. Add it to the library
  3. import javax.servlet
  1. 下载 servlet.jar
  2. 将其添加到库中
  3. import javax.servlet

this name like javax.servletu can see the name of classes from the .jar file and import accordingly.

这个名字就像javax.servlet你可以从 .jar 文件中看到类的名称并相应地导入。