• Post Reply Bookmark Topic Watch Topic
  • New Topic

classloader program  RSS feed

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[code]
public class Test{
public static void main(String args[])
{
System.out.println(Class.getClassLoader().getClass().getName());
}
}[\code]

here i got an error msg saying thatnon-static method getClassLoader() cannot be referenced from a static context
System.out.println(Class.getClassLoader().getClass().getName());
how can i resolve this error
pleas give some suggestions
thanking u
cinux
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need an actual object for this, because it's not a static method. Instead of

Class.getClassLoader()

use

(new Test()).getClass().getClassLoader()

For your purposes (where you are not using any classloaders of your own) it's probably equivalent to use

ClassLoader.getSystemClassLoader()

if you want to avoid the creation of a Test object.
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey i have used ur piece of code.. but unfortunately i got sonme strange output..
it has given the hashcode of the class...:-(
what can do with ath hashcode ...
is there any advantages,applications by knowing the classloader??
please tell me soon
thanx
cinux
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it has given the hashcode of the class

What do you mean by that? What has given you the hashcode? Are you passing the classloader to println? Then you most likely got its in-memory reference, with which you can do nothing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!