I am developing an eclipse plugin. When the plugin is launched (for example, in an eclipse debugging instance), it loads certain classes from all java projects in the debugging workspace. For this purpose reflection is used. To better understand what I'm doing here, this is the code:
for (IPath outDir : _outputDirs)
{
IPath fullTestPackageDir = outDir.append(testPackageDir);
File dir = fullTestPackageDir.toFile();
if (dir.exists() && dir.isDirectory())
{
for (File classFile : dir.listFiles())
{
String binaryClassName = packageName + "." + FileUtils.getNameWithoutExtension(classFile);
Class<ITest> testClass = tryLoadClass(binaryClassName, ITest.class);
if (testClass != null)
{
testClasses.add(testClass);
}
}
}
}
Below is my implementation tryLoadClassused above as mentioned in the comments (not much magic). _urlClassLoader is created from an array URL(output folders of java projects in the debugging workspace) and has the current ClassLoader context stream as the parent.
@SuppressWarnings("unchecked")
private <T> Class<T> tryLoadClass(String binaryClassName, Class<T> baseType)
{
try
{
Class<?> testClass = _urlClassLoader.loadClass(binaryClassName);
if (!baseType.isAssignableFrom(testClass))
return null;
return (Class<T>)testClass;
}
catch (ClassNotFoundException e)
{
System.err.println("class not found");
return null;
}
}
Later, these classes are created and the function ( run) is called :
Class<ITest> testClass = testClasses.get(i);
try
{
ITest test = testClass.getConstructor(IRuntime.class).newInstance(_runtime);
test.run();
}
catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e)
{
e.printStackTrace();
}
, . MyUtil, , , . , MyUtil run.
. test.run() , ClassNotFoundException MyUtil. ( , , 6- ). ITest Runnable:
Class<ITest> testClass = testClasses.get(i);
try
{
ITest test = testClass.getConstructor(IRuntime.class).newInstance(_runtime);
Thread thread = new Thread(test, "TestThread");
thread.start();
}
catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e)
{
e.printStackTrace();
}
, , ContextClassLoader , . , ContextClassLoader MyUtil, , . ClassLoader , .
, , , ClassLoaders...
:
Caused by: java.lang.ClassNotFoundException: de.my.company.MyPlugin
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 2 more
, "de.my.company.MyPlugin": /, Java- , , "base" classpath ( ...)
/:
, - : test.run() ( ) Eclipse Job. , : , Job -context ( Eclipse). , ITest (, eclipse, main()).
, , _urlClassLoader ITest. Java -, (afaik), _urlClassLoader . , _urlClassLoader ( java- "classpath" ). . , , ...