• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class loading  RSS feed

 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i would like to have some help to understand how dynamic class loading works. I have a custom class loading that extends URLClassLoader. I m using this class to load a class contained in a jar. The class loaded is a jdbc driver for a database. How can i use this loaded class ? If i do
It is not working, and it throws a ClassNotFoundException.
Thank you for explaining why ?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This version of Class.forName uses the class loader of the class you're calling the method from.
What you want, is either the other version of Class.forName that takes a class loader as an argument, or the class loader's loadClass method.
 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello and thanks,

Originally posted by Rob Prime:
This version of Class.forName uses the class loader of the class you're calling the method from.
What you want, is either the other version of Class.forName that takes a class loader as an argument, or the class loader's loadClass method.


So most of DataSource implementation that uses Class.forName("my.loaded.Class") method will not work and the only solution is to implement a custom DataSource instead of using one open source is it ?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it depends.

If your DataSource has been loaded by the system class loader and my.loaded.Class can only be found using the URLClassLoader, or if the DataSource and my.loaded.Class need different class loaders for another reason, then you have a problem.

However, if the my.loaded.Class can be loaded from the same class loader as the DataSource, then it's ok. Because, as I said before, This version of Class.forName uses the class loader of the class you're calling the method from.
This means that if the DataSource calls Class.forName, it will use the DataSource's class loader.
 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
Well it depends.
However, if the my.loaded.Class can be loaded from the same class loader as the DataSource, then it's ok. Because, as I said before, This version of Class.forName uses the class loader of the class you're calling the method from.
This means that if the DataSource calls Class.forName, it will use the DataSource's class loader.


Hello,

The DataSource is loaded by the system class loader as i put the jar in the classpath. My jdbc driver can only be loaded by my URLClassLoader cause i can load different ones, depends on the database i want to use, unless i put all the jdbc driver existing in the classpath which is not really possible. So if i understand well the class loading mechanism if i load the DataSource jar with my URLClassLoader then my jdbc driver jar with this same classloader then it should work right ?
If do i have to take the DataSource jar out of the classpath or can i leave it there, then reload it with my URLClassLoader when i will need it ?
Thank you.
[ January 16, 2008: Message edited by: dav mrazek ]
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once a class has been loaded, it is very hard to unload it; only if its entire class loader will be garbage collected, the class may be garbage collected. Don't count on that though.

As for the issue of doing it your way, I tested it but it doesn't seem to work; because the JAR file is on the class path, the URLClassLoader will in fact use the system class loader first, and this is the one that finds it.

So it seems a little redesigning seems to be the best (only?) option.
 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
Once a class has been loaded, it is very hard to unload it; only if its entire class loader will be garbage collected, the class may be garbage collected. Don't count on that though.

As for the issue of doing it your way, I tested it but it doesn't seem to work; because the JAR file is on the class path, the URLClassLoader will in fact use the system class loader first, and this is the one that finds it.

So it seems a little redesigning seems to be the best (only?) option.


Hello,

A little redesign... yes except that i dont have a clue on how to do it. Using a custom class loader was the only one that came to me. If i dont know in advance what database the user is going to choose, the only way is to load the class at runtime through a class loader, i dont see other way of doing it.
 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As for the issue of doing it your way, I tested it but it doesn't seem to work; because the JAR file is on the class path, the URLClassLoader will in fact use the system class loader first, and this is the one that finds it.


Hello, if i load both jar with my custom class loader and then use introspection to get instance of class and use class's methods, i think it should work. Is it a good way/design of doing it using introspection ?
Thank you for advices.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a custom classloader in the first place? Can't you put the drivers of all supported database in the classpath?
 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, because i d like it to be sort of generic and work with all kind and relational database that exist, if possible. And i dont think it is good to put all drivers in the classpath. I only know few relational databases like oracle, mysql, postgre and few other but i guess there are many others that i dont know. Once i have my custom class loader working, it is more developpement now of course but after it will be alright and i will not have to worry about drivers anymore. It is more convenient for users to have just one tool instead of one tool for one database and another for another database. Simply convenience like i think java is very good a pure java software can work on different system without having to change anything, but that is another discution.
[ January 25, 2008: Message edited by: dav mrazek ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!