help.! Java URLClassloader

S

Sundar

I have a requirement, I have to use the existing jar of websphere,
when my program is shipped. This program uses AdminClient of websphere
JMX. This has to run as a standalone. It installs an application using
JMX. What I require is to use URLCLassloader and load the adminclient,
objectname classes and use them. If I set classpath i am working fine,
but i do not have the luxury of that. Can I know how to use the
URLClassLoader.

A segment of my code looks like this.

AdminClient client;
Class createClass = loader.loadClass
("com.ibm.websphere.management.AdminClientFactory");

Class[] args=new Class[]{java.util.Properties.class};
Method create = createClass.getMethod("createAdminClient", args);
Object params[] = new Object[]{props};
Object obj= create.invoke((Object)"createAdminClient", params);
//works fine till this
client = (com.ibm.websphere.management.AdminClient)(obj); // casting
no success error.

String version = (client.getServerMBean()).getKeyProperty("version");
// this is my purpose.

Can anyone help me ? I am urgent schedule.

Thanks in advance,
sundar
 
A

Anton Spaans

Your 'loader' variable needs to be a URLClassLoader instance:

URL[] urlArray = ....;
loader = new URLClassLoader(urlArray);

Now the question is how to figure out the URLs in urlArray. I assume that
your AdminClientFactory class is available locally (i.e. you could use the
File object to get access to them), either in a directory on in a Java
Archive (.jar, etc.. file).
First, figure out the classpath you would have added if you had the luxury
of that ( as you put it :) ). Each item in that classpath (be sure you have
enough items to load your AdminClientFactory class and all the clasess it
needs, etc..) can be represented by a File object. Take these as examples:
"D:/MyProjects/ibm" for accessing
"com.ibm.websphere.management.AdminClientFactory" and
"D:/MyProjects/ibm/lib/ibm.jar" for the classes that are needed by it. Then:

URL[] urlArray = new URL[] {
new File("D:/MyProjects/ibm").toURL(),
new File().toURL("D:/MyProjects/ibm/lib/ibm.jar")
};


That should do it.
-- Anton.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top