Ankur
Ankur

Reputation: 437

Jars with same name of class file and with same package structure

I have two jars A.jar and B.jar and both have a class say test with package com.source,

I know i can get hold of test from A.jar if A.jar comes first in the class path.

I am looking for the way, to get access of test from B.jar if I don't have any control in modifying class path. So basically, what all can be done to access test from B.jar without removing A.jar?

Upvotes: 1

Views: 726

Answers (1)

Sainik Kumar Singhal
Sainik Kumar Singhal

Reputation: 811

You can use URLClassLoader and give the exact path to your jar file.

    URL myURL = new URL("jar:file:" +jarfilepath+"!/");
    URL[] urls =  new URL[]{myURL};
    URLClassLoader cl = URLClassLoader.newInstance(urls);
    Class c = cl.loadClass(classname);

Upvotes: 1

Related Questions