Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Dynamic loading of jar file!!!
Dynamic loading of jar file!!! [message #212114] Fri, 25 May 2007 15:12
Jugal Shah is currently offline Jugal ShahFriend
Messages: 19
Registered: July 2009
Junior Member
I have been trying to dynaimcally load a class from a jar file i.e trying
to modify plug-in classpath at runtime... I spent almost an entire day on
finding out how to do this... but the efforts were in vain..:-(.

Following is the description of what I am trying to acheive:

The location of jar file is not known at compile time as its location can
only be obtained during runtime by the value set by the user in
Preferences. After the user set this file and perform an action using a
context menu click, I have to load a class from this jar file. This class
in turn internally refers to xercesimpl.jar and xml-apis.jar.
It is not possible to create a bundle out of this jar as the location for
the file is known to at compile time and also its not possbile to change
the Manfiest information in this file, so the buddy class loading option
is ruled out and the only option left out for me is to load this class
dynamically using Context classloader technique.
Following snippet shows on how I am trying to acheive this:

Thread current = Thread.currentThread();
ClassLoader oldLoader = current.getContextClassLoader();
try{
BundleClassLoader pluginClassLoader = (BundleClassLoader)
getClass().getClassLoader();
current.setContextClassLoader((ClassLoader)pluginClassLoader );
pluginClassLoader.getResource(generatorArgs.getGeneratorJarL ocation());
Class c;
String typeName = "com.mastek.jspx.util.Generator";
try {
c = pluginClassLoader.loadClass("com.mastek.jspx.util.Generator ");
}
catch (ClassNotFoundException e) {
LogUtil.log(e);
return "Failed to load: " + "com.mastek.jspx.util.Generator";
}

// Instantiate the target object
Object target;
try {
target = c.newInstance();
}
catch (Exception e) {
LogUtil.log(e);
return "Failed to instantiate: " + typeName;
}
// Use reflection to obtain and execute the method
Method m;
try {
m = c.getMethod("main", new Class[] {String[].class});
}
catch (Exception e) {
LogUtil.log(e);
return "Failed to find method: " + "generate";
}
Object result;
try {
result = m.invoke(target, new Object[] {new String[]{"-form",
generatorArgs.getFDLocation(), generatorArgs.getJSPXUserLocation(),
generatorArgs.getJSPOutputLocation(),
generatorArgs.getJavaOutputLocation(),
generatorArgs.getConfigOutputLocation()}});
}
catch (Exception e) {
LogUtil.log(e);
return "Failed to invoke method: " + "generate";
}
// Return a message containing the execution result
return "Return value = " + result;
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
current.setContextClassLoader(oldLoader);
return "Failed to invoke generator utility";
}
}

The method generatorArgs.getGeneratorJarLocation() is responsible for
obtaining the location of the jar file at runtime. When I try to load
class from this jar file using pluginClassLoader.loadClass() it give me
ClassNotFoundException... in fact even before that the class loader
actually is not able to find resource (pluginClassLoader.findResource())
and thus is not able to load class.
I really dont know where I am going wrong... why is it not working... I
have exactly followed the way it is instructed in FAQ, if you have to load
classes dynamically..

Please help me out in this...

Waiting for your reply,

Jugal.
Previous Topic:Eclipse perspective
Next Topic:Possible to run JBoss3.0.1 on Eclipse3.2.1?
Goto Forum:
  


Current Time: Fri Apr 26 17:26:22 GMT 2024

Powered by FUDForum. Page generated in 0.08523 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top