I'm trying to use the Reflections library to give me a list of all the classes in a specific package in an external jar file. This works in Netbeans, but not when running the jar file from the command line. It looks like Netbeans finds and loads the classes I need, whereas the command line run doesn't. How should I set this up so it works everywhere?
I've tried both the usage example on the Reflections readme, as well as the response to this issue. Both methods have the same result.
Here's the test code I've been working with to reproduce the issue:
package javaapplication1;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import org.reflections.Reflections;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.ClasspathHelper;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.ResourcesScanner;
import java.util.Set;
import org.externaljar.package.*;
public class JavaApplication1
{
private static Reflections reflections;
public static void main(String[] args)
{
final String myPkg = "org.externaljar.package";
URL[] urlPath = new URL[1];
try{
urlPath[0] = new URL("jar:file:/path/to/external.jar!/");
}catch(MalformedURLException ex){
ex.printStackTrace();
}
URLClassLoader urlLoader = URLClassLoader.newInstance(urlPath);
final ConfigurationBuilder config = new ConfigurationBuilder()
.addClassLoader(urlLoader)
.setScanners(new ResourcesScanner(), new SubTypesScanner(false))
.setUrls(ClasspathHelper.forPackage(myPkg));
reflections = new Reflections(config, new SubTypesScanner(false));
Set<Class<? extends ObjectBase>> objects = reflections.getSubTypesOf(ObjectBase.class);
System.out.println("\n\nFound " + objects.size() + " Objects\n\n");
}
}
Running the project in Netbeans gives a non-zero value for objects.size()
, but running java -jar JavaApplication1.jar
prints "Found 0 Objects". Adding -verbose:class
to each shows that Netbeans loads all the classes I'm looking for, but those aren't loaded when run from the command line.
Aucun commentaire:
Enregistrer un commentaire