• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get class in the jar as inputstream

 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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
 
Martin Simons
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • 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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic