my dog learned polymorphism*
The moose likes Beginning Java and the fly likes How Classes Are Loaded Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How Classes Are Loaded" Watch "How Classes Are Loaded" New topic
Author

How Classes Are Loaded

greg philpott
Ranch Hand

Joined: Nov 10, 2000
Posts: 73
Let's say that you have two jar's in your classpath, say a.jar and b.jar.
Now say there is a class, lets call it common.class in both of these jar files.
If you compile a .java file that calls that common class, the compiler will use the common.class file in the first jar it gets to in the classpath.
example. classpath=c:\jars\a.jar;c:\jars\b.jar Then the common.class in a.jar is used.
Now for my question.
If you have two jars in your installed extensions directory, how is the class found?

(ie. the jars are in c:\(java2 jdk)\jre\lib\ext; )
Thanks for your help.
Chris O`Hara
Greenhorn

Joined: Nov 17, 2000
Posts: 10
Greg,
I don`t know the definitive answer to this, but you could determine which class is used first by a simple experiment.
Create two versions of a class which simply output a message say "using class a(or b)" and run the class which uses the jar and see what message is output first - hence you could deduce what class within the directory is used first.
greg philpott
Ranch Hand

Joined: Nov 10, 2000
Posts: 73
thanks for the suggestion.
The order jar files are looked through by the class loader is alphabetical, so a.jar is looked in before b.jar.
Lance Zant
Greenhorn

Joined: Sep 25, 2001
Posts: 1
I tried to resolve this using the -verbose:class option, but found some "Loaded class..." messages omit the "from foo.jar" part. Any idea when this option does & does not report where it found a class?
Originally posted by greg philpott:
thanks for the suggestion.
The order jar files are looked through by the class loader is alphabetical, so a.jar is looked in before b.jar.

Marilyn Monickam
Ranch Hand

Joined: Jun 05, 2001
Posts: 66
Hi Greg,

I guess you are wrong.
I have experimented on something similar to this.
As far as class files are concerned ,they are taken in the order in which they appear in the classpath.
It could possibly be a.jar's class file because you have given it first in your classpath.Try putting b.jar first in the order.It could solve your doubt as well as mine.
Reply after trying,
With regards,
Marilyn
greg philpott
Ranch Hand

Joined: Nov 10, 2000
Posts: 73
Wow, It has been ages since this post was first made . . . must be book givaway week in this forum!!!
Marilyn,
You do not understand the original post. Please re-read my first post carefully.

 
wood burning stoves
 
subject: How Classes Are Loaded
 
Similar Threads
Classpath for jar files (Tiger)
Setting classpath for Jar file inside jar file
jar classpath help
setting classpath for jar file inside jar file
jar issues