Dynamically add jar file to the class path

 File jarDir = new File(jarPath);
		        ArrayList<URL> jarFiles = new ArrayList<URL>();

		        for(File file: jarDir.listFiles())
		            if(file.isFile() && file.getName().endsWith(".jar"))

		        for(URL url:jarFiles)

		        URL[] urlArray = new URL[jarFiles.size()];
		        for(int i=0; i<jarFiles.size(); i++)
		            urlArray[i] = jarFiles.get(i);

		        ClassLoader  loader = new URLClassLoader(urlArray);

Open in new window

I'm using the above code to load the jar from external source. But, I'm not able to include the jar into my class path.

For example,

I can load mysqlconnector.jar from any external source. But, when I use the statement,
Class.forName ("com.mysql.jdbc.Driver");

Open in new window

It gives an exception

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Is there a way to include the external jar into my application class path?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Firstly, can we ask, why are you not able to add the path of 'jarDir' prior to vm startup?

fyi, there's nothing wrong with your code
spectrumsofttechAuthor Commented:
I've an application, which will load an external class. That class may be dependent on the jar files say for example "mysqlconnector.jar" that may not be present in the application.  So when I load that class, I'll also load the jar on which the external class dependent.
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I agree with @CEHJ's reservations about why you can't do this the "normal" way, and your comment hasn't really convinced me either.

However, in order to resolve the exception that you are getting, have you tried this version of the Class.forName() method...
Class.forName("com.mysql.jdbc.Driver", true, loader);
//where that 3rd arg, loader, is the loader from line 19 of your first code snippet

Open in new window

The above may work for you but you are getting into deep water when you have different classes loaded by different classloaders!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fyi, there's nothing wrong with your code
Sorry spectrumsofttech. I managed to fail to connect the outcome with your code. There IS nothing wrong with your code, but you should try, after running it, to do what mccarl suggested (who has not had the same problem as i connecting the code with the outcome ;))
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.