简体   繁体   中英

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

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 ?

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?

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

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" .

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.

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

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 -> Library a popup will come up.
    2. There you can add a new library. Click on New Library and name the new Library.
    3. Import a library from the ClassPath tab Add Jar/Folder
    4. If you want to can also add the javadoc from the JavaDoc tab. 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 Sources tab. Import the file named sources
    6. Then click on the Libraries folder icon in your project and Add Library
  • Another option is just add the .jar .
    1. Right-click on the Libraries folder icon in your project and click Add Jar
    2. Locate the .jar file and add it.

As long as you have: package 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-

  • 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.

example :

  1. download servlet.jar
  2. Add it to the library
  3. import javax.servlet

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM