• 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

class.forName()

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is a piece of smaple code:

if i change the line "Class.forName("java.io.File")" to "Class.forName("File")" then the result of running should be "not exist" while remain the original code, it doesn't throw the exception.So i want know why and i hava set the enviroment variable classpath to the position of the installation of jdk in my computer.
In the doc of jdk1.3 I found that the method forName of "Class" class in java.util has a another overloading version with a argument ClassLoader.But i can't understand that how can i create a ClassLoader and make it works.Of course I know that ClassLoader is a abstract class,but if I want make my ClassLoader extends from it works, which methods should i implement.
Thanks in advance!
[ edited to preserve formatting using the [code] and [/code] UBB tags and to remove tabs -ds ]
[ July 22, 2002: Message edited by: Dirk Schreckmann ]
 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you be more explicit?
What do you expect? What to you want to know?
Do you want to create your own ClassLoader so that Class.forName("File") does not throw an exception?
W.
 
Steven Zeng
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, just like you said.
 
Steven Zeng
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I didn't explain my question clearly.In fact I want know the following:
1 how to use the method of class.forName() that it works in findng a existing class without throwing the exception.
2 how to use the classloader
3 Moreover, how can i know the sequence that the classloader load in the classes.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the API:
Given the fully qualified name for a class or interface
ie it needs to be the complete package and class name. Therefore 'File' won't work, but 'java.io.File' will.
The basic version of Class.for name is equivelent to Class.forName("Foo", true, this.getClass().getClassLoader()), so that usually the default ClassLoader is used, and this is usually enough.
The ordering is locate, load, and link the class or interface. The extra info is that a Class will only be loaded once by a specific ClassLoader and it will only be initialised once. Also, it is possible to create a ClassLoader heirarchy which muddles things, but usually it isn't a problem.
Dave
 
Steven Zeng
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you ,David.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic