Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Reflections library to a Eclipse project

Tags:

java

eclipse

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,

like image 568
Fran Marzoa Avatar asked Feb 19 '23 19:02

Fran Marzoa


2 Answers

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.

like image 172
Francisco Paulo Avatar answered Mar 03 '23 15:03

Francisco Paulo


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.

like image 29
JB Nizet Avatar answered Mar 03 '23 14:03

JB Nizet