• 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

how to get class in the jar as inputstream

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to get class which is located in jar (eg i will give input as java.util.Date ), as inputstream.i have tried using getResourceASStream but this is returning null
so can any one help in resolving this issue.


Thanks in Advance
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really don't know why you would want to do this, but if you would post the snippet of code you use in attempting to load the class, we may be able to help you a little better.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Presumably, this relates to the Original Poster's earlier questions, where he wanted to find out all classes used by a particular class. Doing that involves reading the class file.

One pit-fall in trying to read classes from Jars is that the name needs to use slashes, not dots, as package delimiters. So "java.lang.String" is "java/lang/String" in the Jar.
 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here i am checking for class file to be exsited in particular path
if file dont exists at that location it raises exception which sets flag
if flag is set than i am trying to read that file from java packages of jars

try {

cs = new DataInputStream(new FileInputStream(fileName));
}catch(IOException e){
flag=true;
}try{
if(flag){
cs = new DataInputStream(getClass().getResourceAsStream(clsname));
}
 
Martin Simons
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show us where you define "clsname". Also, as the previous poster mentioned, you need to convert the "." characters to "/" characters, and add a leading "/" character.

Edit:

And another to remember that, that complicates all of this, are internal classes, anonymous and otherwise, that will appear with a "$" character in the name of the class file, so the class file name will not exactly match the name of the class, if it even has one. So if you run into a class that contains internal classes and you wish to "cycle through" these classes as well, you may wind up having some real fun attempting to paste together the name of the class file.

2nd Edit:

Also, I don't believe the problem you are having here is really one for the Advanced forum. But, if the moderators agree with me, this thread will probably be moved to the Intermediate forum anyway.
[ January 12, 2007: Message edited by: Martin Simons ]
[ January 12, 2007: Message edited by: Martin Simons ]
 
Always look on the bright side of life. At least this ad is really tiny:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic