GeeCON Prague 2014*
The moose likes Java in General and the fly likes how to get class in the jar as inputstream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "how to get class in the jar as inputstream" Watch "how to get class in the jar as inputstream" New topic
Author

how to get class in the jar as inputstream

prasad Venkat
Ranch Hand

Joined: Oct 17, 2006
Posts: 32
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

Joined: Mar 02, 2006
Posts: 196
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

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
prasad Venkat
Ranch Hand

Joined: Oct 17, 2006
Posts: 32
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

Joined: Mar 02, 2006
Posts: 196
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to get class in the jar as inputstream