3

I'm trying to open a JAR as a stream, but can't understand where to get this stream...

JarInputStream s = new JarInputStream(/* what is here? */);

Where to get this stream? I'm trying to open the JAR which is currently is in charge.

yegor256
  • 102,010
  • 123
  • 446
  • 597
  • possible duplicate of [List files inside a jar ](http://stackoverflow.com/questions/1429172/list-files-inside-a-jar) – erickson Oct 12 '10 at 13:43

2 Answers2

4

From my answer to a similar question:

CodeSource src = MyClass.class.getProtectionDomain().getCodeSource();
if (src != null) {
  URL jar = src.getLocation();
  ZipInputStream zip = new ZipInputStream(jar.openStream());
  /* Now examine the ZIP file entries to find those you care about. */
  ...
} 
else {
  /* Fail... */
}
Community
  • 1
  • 1
erickson
  • 265,237
  • 58
  • 395
  • 493
2

Look up a class that is in the JAR. Eg:

Class clazz = SomeClass.class;
URL resource = clazz.getResource(clazz.getSimpleName()+".class");
JarFile jarFile = ((JarURLConnection)resource.openConnection()).getJarFile();

I know you wanted a JarInputStream but I would bet JarFile does what you want.

Plaudit Design
  • 1,156
  • 8
  • 16