The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

JarInputStream

I'm confused with the JarInputStream class.

JarInputStream takes in an InputStream, but an InputStream of what? Is it the Jar file as an InputStream? It cant be the files inside as an InputStream because isnt that what getNextEntry() and getNextJarEntry() methods are for?

If it is supposed to be an InputStream of the Jar, how the heck to I get that? Specifically, I am trying to get the InputStream of the Jar that is being executed to run the program so I can get particular JarEntries inside of it.

I could use some quick help. Any information is appreciated.

NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.

Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Re: JarInputStream

I am trying to get the InputStream of the Jar that is being executed to run the program

Not sure what you are asking. You don't execute a jar, you execute a class file that is in a jar.
There isn't an InputStream for the jar (well maybe in the classloader that is reading the class files from the jar).

To get the file path to the jar file that your class file is in, look at the the getResource method.
Here's a program I wrote long ago that does some of that:

Re: JarInputStream

Not sure what you are asking. You don't execute a jar, you execute a class file that is in a jar

Ok, I meant the I want to access files within the Jar that contains the class that is being executed. I am attempting to access some .class files that will be used to create 3 unique Jars. In order to do that, I believe I need to be able to access those class files as a Stream, as I think that is the only way. From there, I am wanting to grab predetermined class files from the jar is has the class that is being executed and create executable jars with them.

I'll review the code you posted when I get to work in an hour and I have time to really analyze it.

NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.

Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Re: JarInputStream

Ok, I'm completely confused about the whole getResource(String) method. I attempted what you did with the getClass().getResource(args[0]); but I got an index out of bounds exception.

NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.

Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

My next issue is: How can I get a list or something of the .class files in the jar that I am getting my class files from? The reason I need that is because I need to be able to find the unknown amount of .class files that contain the name of: Class$X$X.class where X a random number. For the usual directory, I just used the file.list() method and went through the String[] it returned. But I don't know how to do the same for a .jar or .zip directory. It wouldn't be an issue if I knew every time where the .jar file would be (I could use the JarFile(String) constructor and methods), but it could be located anywhere and that can change.

Last edited by aussiemcgr; October 11th, 2010 at 03:08 PM.

NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.

Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Re: JarInputStream

I attempted what you did with the getClass().getResource(args[0]);

Look at the commented out main method. It shows how to use the class.

I wrote a project for searching thru the contents of a jar file for entries containing a String. I had a Search program for files in folders and I wanted to use the same code for searching in a jar file without having to change any logic. So I put this together so the search code would think the jar was a folder with subfolders.