简体   繁体   中英

How to read a file inside a jar's resources from ouside the jar

I have a file X.json inside abc.jar in its classpath. There is a method readFile in abc.jar which reads the file as

URL url = Abc.class.getClassLoader().getResource("X.json");  
File file = new File(url.toURI());*

which reads it fine if i run it from within the jar's context ie main method in some file inside the abc.jar

But when readFile is called from from outside of abc.jar from some other code it fails with java.lang.IllegalArgumentException: URI is not hierarchical

How do I get the File object by calling the method readFile from outside the jar's context ?

By the time of deployment, those resources will become an . That being the case, the resource must be accessed by URL instead of File . See the info page for the tag, for a way to form an URL .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM