• 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

Getting the InputStream for a Class

 
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am writing a distributed application where I need to load classes from remote nodes. For this purpose, I wrote a custom class loader, which attempts to get the byte[] for a remote class, and then use the ClassLoader.defineClass() method to define the class.

However, I am unable to get the byte[] of the class in the remote machine. Currently, I am trying to do it as follows: (this code is executed in the remote machine where the class resides)

Note : name = fully qualified name of the class


Then, I hope to use the InputStream to get the byte[] for the class.

However, getResourceAsStream() returns null. Class.forName() loads the class correctly, but getResourceAsStream() is unable to return InputStream.

Any help / suggestion is deeply appreciated.

Thanks.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the class file your trying to get an InputStream for relative to the classpath for the code that is running getResourceAsStream()?
The class file must be on the classpath for the classloader.

public InputStream getResourceAsStream(String name)

Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader.




Is resName the name of the file?
[ July 03, 2008: Message edited by: Norm Radder ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carefully read the Javadoc for the method you're calling. It's going to append the package-path of the class you're loading the resource through to the beginning of the path you pass, unless the path starts with a "/". I believe if you prepend a "/" to the name, and use this code, it should work.
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ernest
Thanks Ernest. It worked after inserting "/" before the resName.

@Norm
Yes, resName is the name of the file, with the path. If 'name' is "test.pack.MyClass", 'resName' will be "test/pack/MyClass.class".

However, since I was using the class loader from the class itself, it was not the relative path to the actual class file. That caused the problem, as Ernest pointed.

Thanks a lot guys.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic