Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invoke a Class from a Particular JAR file

 
Neelakantan Kuthalanathan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Class with under a package com.pkg.test which I have a Packaged as JAR file
say JAR1

----------------------JAR1-------------------------------------


--------------------------------------------------------------------

I have another JAR file with the same class name but differnt implemetaion and value of static variable
under same Package structure...Say

-------------------------JAR2---------------------------------------



---------------------------------------------------------------------------

Simalarly I have another JAR file with the same class under same Package structure...Say

-------------------------JAR3---------------------------------------



---------------------------------------------------------------------------




Now I create Another MainClass and import all these three JAR files into my class-path
(I included the JAR files in this order : JAR1,JAR2,JAR3)

--------------------------------------------------------------------------------------------



>>>>>>>>>>>>>>>>>>I get the following output>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Executing the project JARTest
Getting the Static variable T from ClassA
Value of T=1000
Calling printID method from ClassA
In Jar 1
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

If I include the JAR file in the following order :JAR3,JAR1,JAR2 ; then i get following output

Executing the project JARTest
Getting the Static variable T from ClassA
Value of T=222
Calling printID method from ClassA
In Jar 3


-------------------------------------------------------------------------------------------------

My question is how can we control this behaviour ,I mean how can we say to JAVA to use the ClassA
from the particular JAR file at runtime.My requiremnt is I need to access the methods of ClassA from
JAR1,JAR2 and JAR3.

Any Idea regarding this.

Thanks
Neel
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of putting the jars onto the classpath, you'll need to use your own classloader(s).

Can you tell us more about where that requirement is coming from?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic