How can my JAR open itself as a stream?

I am trying to open a JAR as a stream, but I cannot figure out where to get this stream ...

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

Where to get this stream? I am trying to open a JAR that is currently responding.

+3
source share
2 answers

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... */
}
+5
source

Check out the class that is in the JAR. For instance:

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

I know you need a JarInputStream, but I would argue that the JarFile does what you want.

+2
source

Source: https://habr.com/ru/post/1769138/


All Articles