• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Detect if class exists

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm having following problem:
I try to detect if a class in a package exists. This is no problem. I do it using:
java.net.URL classurl1 = new Object().getClass().getResource("/org/apache/xerces/framework/Version.class");
and
java.net.URL classurl2 = new Object().getClass().getResource("/org/apache/xerces/impl/Version.class");
Depending on the package used one of the two classes will be absent.

If classurl is null/not null, I know the class does (not) exist/
So far so good.
Now I want to retrieve a static variable out of the class (that exists in the package). I use following code:
if (classurl1 != null)
System.err.println("= Xerces build: "+(org.apache.xerces.framework.Version().fVersion);
if (classurl2 != null)
System.err.println("= Xerces build: "+org.apache.xerces.impl.Version().fVersion);
Now my compiler refuses to compile this as I have only the package installed that contains the org.apache.xerces.impl.Version class (the org.apache.xerces.framework.Version was used in a previous version/package of Xerces).
How can I solve this problem ?
Regards,
Stefan
 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may use Class.forName(String name). This does not presuppose the absence of one or the other class.
I don't know the class hierarchy of these classes. Have they a common ancestor where you can apply fversion to?
W.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Wilfried,
i have a question. if we use Class.forName() then also would not the problem be same? because, "we want to access a static member of the class". we can't write,
Class.forName("blah..blah..blah...").staticMemberName;
right?
because Class.forName() returns "Class" object and compiler won't resolve staticMemberName variable in "Class". we have to write the class name to access the static member but we can't use full name of the class as compiler would complain that it didn't find the class, if the class is not there in the CLASSPATH.
so, even if we use Class.forName() , the problem seems not solved.
do you see what i mean?
am i right in my argument? if not, please correct me.
sorry, i might not be that clear in what i really mean. i'm little poor at certain things.
regards,
maulin
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


we can't write,
Class.forName("blah..blah..blah...").staticMemberName;
right?


Have you tried to use the getDeclaredField() method from the "Class" class? I think it also returns static fields, but I haven't tried yet.
Something like:
Class.forName("blabla").getDeclaredField("fVersion").getInt(null);
A Field provides information about, and dynamic access to, a single field of a class or an interface. The reflected field may be a class (static) field or an instance field.
Pierre
[ November 29, 2002: Message edited by: Pierre Post ]
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also investigate the java.lang.reflect package.
Once you have a Class object, you can retrieve its methods and get Method object. Once you found the found the Method object corresponding to "fversion", you can apply invoke on it, whatever the class.
W.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic