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.
Related
I am creating an external library to share code between my client/server programs. IntelliJ can't seem to import this jar file and I can't understand why, I followed all the advice I've found online.
I added the jar to my lib folder and added it as a library in my main module (it's called 'common':
This is the error (repeated on each instance of me using User or Packet). On import statements it says "package common does not exist":
I've tried invalidating caches and that didn't seem to work either. I tried this same import on a new project just to see if I was doing anything wrong and it worked perfectly on that new project so I'm not sure what's going wrong here.
EDIT:
I've noticed that this error only happens in files that are in packages within the src folder. So basically, Main can find the jar because it is only in the src folder, but RegistrationController can't because it's in src/communication.
The problem was that the shared code I was making was not in a package, just a src folder. Because of that, the code that was in my default package (src) could access the classes, but not the code in packages. To fix this, I put the shared classes in a package (com), exported them as a jar, and imported that into my project. Now, my packages can do com.Packet or com.User and get the files successfully.
I need to import a few class files into a java project in eclipse. I put the files in a folder and then add the folder to my project by right clicking the project, then select Properties > Java build path, then "Add External Class Folder".
Then I imported the class into the java file using an import statement:
import cla11.classname;
("cla11" is the folder name, "classname.class" is the name of the class file.)
However, the compiler doesn't allow the import (import cla11 cannot be resolved) and the classes contained in the class files are hence unusable in the project (... (class name) cannot be resolved to a type).
Note: I am aware that my question is almost the same as the one in this link: https://stackoverflow.com/questions/2477947/how-to-import-class-file-in-a-java-file#=
I used the method described in the answers, but the compiler does not allow it (as described).
(Since I am not yet allowed to comment, the only way I could think of is to ask the same question again.(Any other suggestions on what I could do to solve such problems would be welcome.))
Class' package name and your folder name must match. For example, if your class' full name is com.example.MyClass you need a directory structure like com/example/MyClass.class and import the root folder to the Eclipse.
Update
I do not know your actual needs. Like why do you even need to import an external .class file. So you may prefer to create a Jar file and add it as dependency. IMHO, This way it will be a lot easier to distribute your application.
You can create a jar file like this jar cf my-library.jar com/ after that, you can add Jar file as a dependency. For example if you are developing a web application you can simply drop your my-library.jar file to ${project-root}/WebContent/lib directory. Or if you developing a console application, you can simply add your my-library.jar file to your class-path.
I'm trying to learn Java ( I know php ) but I don't know what path the included classes have. For example, I create a new directory in Eclipse (in the package) and drag there a class from other project. When I try to import it, it cannot find the class. Even if I don't have any dirs and the class is directly in the package, using import package.classname doesn't work...
I must be missing something but googling doesn't show me any replies.
How do I get the class path? Is it somewhere in the properties?
Java has the concept of a classpath: a path where all classes should be found.
You can get the existing classpath with this code:
System.getProperty("java.class.path")
If you run java from the command line then you have to set your own classpath with your classes.
From the classpath, use the package to find the class. For example, if the classpath is ".", which is the current folder, and you have a class called A, which is in the package com.yourcompany, then you will find the class under ./com/yourcompany/A.class
On the example you gave, go to the terminal and look for the "bin" folder and you will see all classes. However, if you want to add a new class from another project to your project, then there are simpler ways. You can simply open your build path in Eclipse and add the class from the other project onto your project.
Another way is to create a jar from the other project and add the jar to your project.
In Eclipse, go to Project->Properties-Java Build Path where you can config the classpath which allow you to import.
I have two jar files - jar1 and jar2. Both of them are located in C:\Eclipse projects\ and I have added the paths to both of them to the Environment Variable CLASSPATH as follows
.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Eclipse projects\stdlib.jar;C:\Eclipse projects\algs4.jar
the ".;" at the beginning were there so I left them. Then I added the jars to the project from their location C:\Eclipse projects\ and they showed up as Referenced Libraries. However, when I try to instantiate a class from the jars it does not recognize it. I am also not able to import the jar (import jar1).
After I tried adding a lib folder in the project and I added the jars there. After, I added them as references once again (so not they appear twice in the Referenced Libraries), however, I am still not able to use the inner classes. Any help will be much appreciated.
UPDATE:
Something must be wrong on my end. None of the suggestions worked for me. Here is a video with all the steps: screencast.com/t/gC81YzCsLY0e
RESOLUTION In my project I had a package called TestProject and it seems that those jars needed a default package. After deleting the TestProject package and using a defaultPackage everything worked correctly after adding external JARs as explained below.
I've got the same problem as you today, And no answer from the web can solve it. However, I fixed it at last.
In fact, there is nothing wrong with the setup, it is right to import those jars through "Add External JARs". The real problem is the location/package of you java code. I found that you have to put your .java file in the default package. For example, you will get errors if you put your java code in a package like com.xxx.yyy.ccc, below is an image which shows the right location/package you should use(see WTF.java). After doing that, you program will be able to run.
However, that is how i fixed my problem, i'm not sure that could work for everyone..
In eclipse, right click on a project->Propeties->Java Build Path->Add External JARs (Add JARs if the jar is inside the project's folder) and then choose your jar file.
From now you can use the inner classes of the jars you added. Eclipse will import them when you'll start using them.
Why don't you use these two JARs—— stdlib-package.jar and algs4-package.jar.
And below the code page(http://algs4.cs.princeton.edu/code/)
Q. If I use a named package to structure my code, the compiler can no longer access the libraries in stdlib.jar or algs4.jar. Why not?
A. The libraries in stdlib.jar and algs4.jar are in the "default" package. In Java, you can't access classes in the default package from a named package. If you need to use our libraries with a named package, you can use these package versions: stdlib-package.jar and algs4-package.jar.
Warning: if you are taking Princeton COS 226 or Coursera, Algorithms, Part I or II, you must use the default package verison of our libraries to facilitate grading.
Showing my test success:
If you have a folder with your JAR files into the project:
Right click on the project>Build Path>Configure Build Path;
At the tab "Libraries" click on Add JARs, search and select the JARs files you want to use.
If you have yours JAR files any other place outside the project:
Right click on the project>Build Path>Configure Build Path;
At the tab "Libraries" click on Add External JARs, search and select the JARs files you want to use.
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"