NetBeans - How to import a class from an external library - java

I have a Java project in Netbeans and I want to use some classes from Weka within my project.
I added the file C:\Program Files\Weka-3-7\weka-src.jar into my Libraries following the instructions here (project, properties, libraries ..)
So how do I now import the classes I want?
I tried importing like this:
import weka.core.converters.ConverterUtils.DataSource;
And for kicks, I also tried this which didn't work either:
import src.main.java.weka.core.converters.ConverterUtils.DataSource;
NetBeans says "package does not exist" for both.
Have I linked the libraries incorrectly? Do I need to phrase the import differently?
Much thanks for any insight into this you can provide.
Update:
In my Libraries folder of my projects tab I see:
weka-src.jar and under that I see: , META-INF, lib, src.main.java.weka.associations, and lots of other things from weka.

I have downloaded Weka from here. I have added the JAR file you mentioned and I have also added weka-src.jar.
The problem is that although I have no problems with your first import:
import weka.core.converters.ConverterUtils.DataSource;
I can't see this package anywhere:
import src.main.java.weka.core.converters.ConverterUtils.DataSource;
To see if you have successfully imported a .jar file or a library, click on the "Projects" button on the left margin, find the Project you have added the resource to and expand the view (by pressing the + sign). You should have an item named "Libraries". If you expand that, you should be able to see the files you have added.

I was looking for how to install Weka Jar file too, and I read this tutorial and I realize that the jar file is actually on you Weka installer, and then you just need to import the jar file as same as any other library. Take a chance to look this tutorial, it is pretty cool.
http://www.cs.umb.edu/~ding/history/480_697_spring_2013/homework/WekaJavaAPITutorial.pdf
"Logic will take you from point A to point B, imagination will take you everywhere"

Related

The import org.omg cannot be resolved

I'm following this tutorial on how to set up corba, but I can't use the packages they are talking about, and no where on the internet can give an adequate explanation of how to get them.
I want the following imports to not give me an error.
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
Downloading JacORB just gives me a github file the also has imports of the package 'org.omg'
Somebody said use this findjar site, I have no idea what the things are on that site
Be sure to download the CORBA API from a reliable source:
Glassfish
JOnAS
Add a folder to your project called lib and drag the jar file into lib
Right Click the jar file and select Build Path -> Add to Build Path
Then go to project -> properties -> libraries and move the jar to your CLASSPATH

Import .jar file, poorly documented

I know there are many related questions, but after reading at least 5 of them, I still feel many questions remain unanswered.
This jar file is a generic argument parser for java programs, such that you will use less time writing arg parsers for each program, and instead use this. Although the program is well documented, a programmer like me who still has not used external .jar files, finds it odd that there is no documentation whatsoever about how I should import this(what classes etc).
So I know that I need to put the .jar file somehow in my classpath. And then probably run some additional commands when compiling my program. But how do I find out what to import from the .jar file?
Thanks in advance, and bear with me. I am an import-noob.
Importing classes
importing the classes is symply done by import package.subpackage.Classname;
Using libraries in Eclipse:
To add the .jar file to your libraries, you better use a proper IDE (interpreting your question your are using a texteditor). I recommend Eclipse.
Create a folder in your projects folder in the workspace called "lib"
(or anything that suggests it contains your libraries)
Move the .jar file inside the "lib" folder
Right click on your Java project and
select "Preferences"
Select "Java Build Path" in the appearing window (menu on the left)
Go to "Libraries" (menu on the top)
Select "Add JARS..." and select your .jar from the "lib" folder
You should now see the library listed under "Jars and class folders on the build path" Hit "Apply" and "Ok".
You need to import the classes you want to use just like any other program. If you want to know which classes you need to read the documentation or use you IDE to add the imports for you.
If you were to cut and paste the program into an IDE it would be able to work out the imports for you however, if you don't have an IDE you need to read the javadoc http://www.martiansoftware.com/jsap/doc/javadoc/index.html which lists the packages you need for each class.
Adding a JAR is the same for every JAR so I guess they assumed this knowledge is covered elsewhere. When writing documentation, some knowledge has to be assumed or you would have to start with step 1) turn on your computer 2) wait for it to boot up ....

finding referenced library from import statement in eclipse

I have a Java project in Eclipse with quite a lot of third-party libraries. Now I am looking for a way to get from the import statement to the corresponding .jar in my "Referenced Libraries" section.
My import statements look like this:
import org.xml.sax.*;
Currently, to find out which .jar this came from, I need to open them all (in the Eclipse Package Explorer) and check which of them contains this path of org/xml etc. This is quite annyoing, so I am wondering if there is a better way? Can I
improve my imports to make them more readable?
somehow "jump" from the import statement to the referenced .jar with some nice Eclipse method?
I tried to find some information on this in the www, but either there is none or my search terms were wrong.
If you open a class file (one way is to press F3 on the class) you can then use 'Show In > Package Explorer' (or 'Project Explorer') in the right-click menu to show the class in the explorer view - the view will then show you which jar contains the class.

Adding Reflections library to a Eclipse project

I am trying to add Reflections library to an Eclipse Java project to use it for my needs. The problem is that although I have added reflections-0.9.9-uberjar.jar to my project's lib folder, and also to the Build Path (in fact it appears also under "Referenced Libraries"), the builder seems not to recognize it, so this line for example gives me an error:
Reflections reflections = new Reflections("net.iberdroid.gameserver.cmds");
"Reflections cannot be resolved to a type"
If I try to import org.reflections it says that it cannot be resolved neither.
Any ideas?
Thanks a lot in advance,
The quick and dirty way: open the reflections-0.9.9-uberjar.jar and extract all the jars within it to your lib folder. Then add those jars to the build path.
The more correct way would be to setup your project as a maven project and setup all the dependencies there. Take a look inside META-INF folder of the uberjar.
You may import a class:
import org.reflections.Reflections;
or all the classes in a package:
import org.reflections.*;
But you can't import a package:
import org.reflections; // looks for a class named "reflections" in the package "org"
Note that, with an IDE like Eclipse, you almost never import anything, because the IDE does it for you. Type "Refl", then Ctrl-space, and Eclipse will suggest to use Reflections and add the import for you. Or use "Reflections" without importing it, then hit Ctrl-Shift-O, and Eclipse will organize the imports by adding all the necessary imports and removing unnecessary ones.
EDIT:
The reflections-0.9.9-uberjar.jar file is not a Java library (a jar containing classes). It's a jar containing other jars (and which should thus be a zip file to avoid the confusion). You must unzip the jar and put all the libraries it contains into the build path.

How to use Java packages with Groovy

I'm at school and we are doing this java stuff. We use a special library named sum.kern: http://www.mg-werl.de/sum/bjw.zip
I want to use Groovy instead of Java. But I can't get groovy to use this library. If I add the jar in grape and try to import sum.kern and create a sum.kern.Bildschirm it says that it is not able to find it. How to do it?
I don't think it should be a problem, its pretty straight forward. The only requirement is to have the library in your classpath.
Are you able to import the package successfully?
It doesn't matter though, What IDE are you using, BTW?
You should have a "groovy-starter.conf" file that specifies the directories in which groovy looks for jar files. It should be in the "conf" directory within groovy.home (for example, on my machine it is "C:\groovy\Groovy-1.5.6\conf\groovy-starter.conf").
If the jar file containing the classes you want to import are within one of these directories, you should be able to import it no problem. Alternatively, you could add the directory that the jar is currently in to your groovy-starter.conf file.

Categories