Dynamically add jar file to the class path

Posted on 2013-10-11
Medium Priority
Last Modified: 2013-10-27
 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?
Question by:spectrumsofttech
  • 2
LVL 86

Expert Comment

ID: 39565079
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

Author Comment

ID: 39565087
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.
LVL 36

Accepted Solution

mccarl earned 1500 total points
ID: 39567559
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!
LVL 86

Expert Comment

ID: 39567906
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 ;))

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
A solution for Fortify Path Manipulation.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question