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.
Related
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 ....
I have a lot of classes within a package and I have to move them to another package. I'm using Eclipse IDE and after moving them, I have this problem of editing each class file's first line (the "package" line)
For example, I have to change the line:
package abc.def.xyz;
to
package abc.def.hij.xyz;
is there a better way than to open each file and manually do it? I'm not a familiar with the IDE and I've looked around for a better way in Google but no solution yet. I have a lot fo things to move.
The error will come in case where you are manually moving classes directly to another package in your workspace.
Instead of doing this go to package explorer of your Eclipse IDE, I guess there will be multiple packages in your projectname->src folder(If it is simple java project type).
Now just select the java files from one package in your package explorer and drag them to target package.
Doing this will show a "Move" popup with auto selected checkbox "update references to classname.java", just press Ok.
This will just change the reference of your classes and you do not have to change the package name manually to all of your classes.
I hope this helped.:)
I have been writing a project in Java that I've called NHL2 and at this point in the development I've decided to try to incorporate wheels which can be found here. I've downloaded and unzipped the folder, and tried to import the files in several different ways. In Eclipse I've tried right-clicking the NHL2 project and importing it. The files get inserted into certain places in the project but when I type import wheels.etc.* for instance, it gives me an error and says it cannot resolve the import. So I tried deleting the imported files, right-clicking the package containing my files, which is also NHL2, and running the import command through there. Again, same basic problem.
I've also tried just creating a whole separate project titled wheels and importing there. This seems to be fine and the wheels project itself has no errors, but still I cannot import those files in my NHL2 project. I've tried looking up YouTube videos and other stackoverflow questions because this seems like it's just so basic that it should have been answered by now, but I can't find it.
Create a new Folder in your Project, e.g. called "lib"
Copy the wheels.zip into that Folder
Open the properties of your Project (Alt-Enter on the Project)
Select "Java Build Path"
Select the Register Card "Libraries"
Click "Add JARs"
Select "Yourproject/lib/wheels.zip"
Click "OK" then again "OK"
Though it is no jar file (but a zip) it works. I tested it.
This is because the zip contains the class files and the source files as well.
I've got 45 java packages I've imported into a src folder in Eclipse. Each of course has a particular name, and contains numerous java files. Problem is that the import statements in the packages' classes all refer to the various packages (including themselves in the package line) with an initial prefix name.
So for example, if the package name is
snowman.image
the java files within start with abc.snowman.image
and refer to another package named snowball.hit as abc.snowball.hit. Needless to say, Eclipse complains that these packages referred to in the code do not exist.
I'm thinking I must've done something wrong. Did I import the packages incorrectly? Can I change some property so that the prefix is understood before the packages' names. I'd rather not go through hundreds of java files and manually change the package and import names.
First you need to make sure that the project configuration is correct. Right click on the project, then go to "properties", and in the tree on the left select "java build path". Switch to the "source" tab and and make sure then you have the "your project/src" as a source folder. Starting from the "src" folder you will have the package names just as Tom commented on your question.
Also eclipse has some nice feature, such as "organize imports" - default shortcut ctrl+shift+o - which will look through your source files and update your imports. This works both in the text editor for the current class, as also from the "navigator/package explorer" view for the entire. selected project.
Please make a backup of your project before trying this, as it will automatically change your files by removing unused and adding the imports it finds in your current structure so you might end up with snowball.hit instead of your desired abc.snowball.hit.
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"