Using library defined in global library - java

I have created a new class library and I've defined it in the global libraries with the name Encryption. I've also added the jar file in this newly created class library. My question is how do I use the this jar file in my other projects now? Do I need to import Encryption in the class I am referencing it from?
This is the print screen of how I'm trying to add jar file in dependency folder and the print screen of the project properties folder:-

Once you define it as a global library using netbeans option, you need to do following
Right click on project -> Libraries
Go to Project properties
Compiler Tab
Add Library Button --> In global Library list you should be able to
see that library.
Create a library from your Encryption jar file
Add this Library to project where you want to use
Then add that global library to your local project, in which you can import the Encryption class and use it.

Even if you make a .jar file for your library classes, you'll have to import it. For netbeans import of libraries below is a screenshot -
Edit
Why don't you try Add Dependency?

Instead of adding the jar via "add local resources" after creating the dependency, I tried using the option "manually install artifact". This allowed jar file to become available in my dependencies and I could use the class files by using a simple import statement referencing the path to the class files in that jar file.

Related

IntelliJ ctrl-click references a .class file not a .java file

I have the following line of code in a .groovy file for testing:
GenerateShipConfirmsForBatch gscb = new GenerateShipConfirmsForBatch();
Ctrl-clicking on the GenerateShipConfirmsForBatch takes me to GenerateShipConfirmsForBatch.class in a .jar, and not the .java file, even though I have the class correctly imported at the top. I want it to reference the .java file so it will pick up changes I make to the .java file.
Any help would be greatly appreciated!
First guess - Wrong import
What you wrote seems to me like you have class with the same name in 2 different packages or in imported project instead of in open-able java class. When you import class be careful that you import the one you want to use.
Eg. annotation class Service is wildly used in different packages.
import org.springframework.stereotype.Service;
import com.google.web.bindery.requestfactory.shared.Service;
Just check that you are using the correct import.
Second guess - Incorrectly set modules
If you are having the multi-module application you have to set correctly the parent project to properly address this issue as well as child projects where the links should be as well.
In Maven it is done using pom.xml. It is very nicely addressed in Maven - Guide to Working with Multiple Modules.
In Gradle it is done using build.gradle. You can read more about it Gradle Multi-Module Project Setup.
Basics about classes
Local class
Idea is linking local .java files in preference instead of .class therefore if this is happening I'd recommend reinstalling Idea as I cannot find the correct approach.
Linked class (from external library)
If you have imported external library it WILL link to .class as it is decompiled from .jar file.
What you can do is either download .jar with source codes, if you are using Maven Projects click on Download Sources and/or Documentation.
Just because you have the class imported at the top does not mean that you can view the source code (e.g., .java file). If this class is coming from a dependency defined in your pom.xml or build.gradle file then you likely won't have access to view the source code. However, if this is a separate module you have at the top level of your project, then you'll be able to view the .java file. If this library is open source then I'd suggest cloning it in your project and adding it as a module. That will solve your problem.
You can install Java Decompiler IntelliJ Plugin from here: https://plugins.jetbrains.com/plugin/7100-java-decompiler-intellij-plugin
It allows you to display all the Java sources during your debugging process, even if you do not have them all

Referencing .jar files in NetBeans

I am trying to use FEST automation software. I am currently dealing with NetBeans 6.8 because it is what I have to use at work and am unable to upgrade the software at this time because of our system requirements
[TLDR: don't tell me to upgrade NetBeans because I can't]
I have downloaded the jar files from the google code repository and am trying to get a very simple application to work. I have tried adding the jar files to Libraries through the property panel for my project. NetBeans still cannot find these methods. Where else/how do I add these jar files to the class paths?
In order to use .jar files in NetBeans, you can either add the jar file to your global libraries or your local libraries. It sounds like you tried doing it locally without much success. I recommend adding a library such that you can just pick from your list of libraries without needing to reload it. I use NetBeans 8.1 so it may be just a bit different for 6.8.
What you need to do is go to Tools->Library
Then you need to click on New Library at the bottom left of the dialogue box.
AddLibrary
Name it and hit 'Ok'. Then, you'll add a new jar file by specifying your jar path. Hit 'Ok' again to save everything.
In order to use your new library, you'll need to then click on your Libraries folder in your project and add a new library. Select from the list of libraries the library you just created.
SelectLibrary
Finally, you import your individual files in your library using the #import keyword as per usual. If you want to import everything, you'd type something akin to
import MyNewLibrary.*;
To load a library the way you tried to do it, you'd right click the libraries folder and click "Add JAR/folder" instead of "Add Library". Then you'd import everything.
If you still for some reason cannot get your library to work, then you probably aren't importing using the right name. Or, you might be trying to load a static library without using the static keyword.

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:
Copy the library folder you want to use
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
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 tab Add 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, called javadoc. 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 named sources
Then click on the Libraries folder icon in your project and Add Library
Another option is just add the .jar.
Right-click on the Libraries folder icon in your project and click Add Jar
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-

PrintCommandListener is missing

I'm trying to upload a file via FTP client using Java, but when trying the:
import org.apache.commons.net.PrintCommandListener;
the PrintCommandListener is show in red. I verified my library and it does include commons.net. What should I do?
PrintCommandListener is in fact in Apache Commons Net. For example, if you download the current version, the jar you need to include in your build path is commons-net-3.3.jar. If this single jar is seen by your compiler, then the class should be recognized. Make sure there are no other classes called PrintCommandListener (for example custom ones that you might have written) in your build path.
If using an IDE (such as eclipse) make sure the jar is on your build path. For example, with Eclipse right click project, go to Java Build Path and click Add JARs. Navigate to the location of your jar file and add it.

how to import a java class by a jar file

I had a problem when importing org.slf4j.Logger and org.slf4j.LoggerFactory, how to download these two classes and if it is by the mean of a jar file, how to import from it.
To be able import any classes into your project, such classes should be added to the classpath of your application.
Usually you won't need to extract classes from library, so in your case you need to add slf4j-api-1.7.5.jar from slf4j-1.7.5.zip which accessible here: http://www.slf4j.org/download.html
Here you can find example how to add library for eclipse project: http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29
If you do have the corresponding Jar file, you just have to add it to your project. If you are using Eclipse IDE, you can click on "Project" menu, "Properties", "Java Build Path", "Add External Jars..." and then a file chooser will appear. Choose the location of your Jar file and open it. Here you go. If you are using another IDE, just specify it, so we can further help you with respect to your IDE.

Categories

Resources