I tried to read the file MANIFEST.MF
in BOOT-INF/ lib
, but an exception occurred
Caused by: java.lang.IllegalStateException: Unable to determine code source archive from file:/Users/bruce/disk/IdeaProjects/grpc_integration/grpc_provider/target/grpc_provider-0.0.1.jar!/BOOT-INF/lib/netty-common-4.1.73.Final.jar!
at com.bruce.provider.util.ManifestUtil.createArchive(ManifestUtil.java:27)
at com.bruce.provider.ProviderApplication.main(ProviderApplication.java:56)
... 8 more
Here is my code:
public class ManifestUtil {
public static Archive createArchive(Class<?> clazz) throws Exception {
ProtectionDomain protectionDomain = clazz.getProtectionDomain();
CodeSource codeSource = protectionDomain.getCodeSource();
URI location = (codeSource != null) ? codeSource.getLocation().toURI() : null;
String path = (location != null) ? location.getSchemeSpecificPart() : null;
if (path == null) {
throw new IllegalStateException("Unable to determine code source archive");
}
File root = new File(path);
if (!root.exists()) {
throw new IllegalStateException("Unable to determine code source archive from " + root);
}
return (root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root));
}
}
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ProviderApplication.class, args);
Archive archive2 = ManifestUtil.createArchive(ByteProcessor.class);
System.out.println(archive2.getManifest().getMainAttributes());
}
}
Comment From: wilkinsona
You appear to be trying to treat a nested jar as if it's directly available on the file system. That's the main reason why your code is failing. Rather than using the Archive
API, I'd recommend getting a URLConnection
by calling openConnection()
on the location
of the CodeSource
. You can then cast that to JarURLConnection
and call getManifest()
.
If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.