मुझे हाथों में एक ही समस्या थी, जबकि मैं जार में पैक किए गए संसाधनों से कुछ हडूप कॉन्फ़िगरेशन लोड करने का प्रयास कर रहा था ... आईडीई और जार (रिलीज संस्करण) दोनों पर।
मुझे स्थानीय फाइल सिस्टम और जार दोनों पर निर्देशिका सामग्री पर पुनरावृत्त करने के लिए सर्वोत्तम काम करने के लिए java.nio.file.DirectoryStream
मिला।
String fooFolder = "/foo/folder";
....
ClassLoader classLoader = foofClass.class.getClassLoader();
try {
uri = classLoader.getResource(fooFolder).toURI();
} catch (URISyntaxException e) {
throw new FooException(e.getMessage());
} catch (NullPointerException e){
throw new FooException(e.getMessage());
}
if(uri == null){
throw new FooException("something is wrong directory or files missing");
}
/** i want to know if i am inside the jar or working on the IDE*/
if(uri.getScheme().contains("jar")){
/** jar case */
try{
URL jar = FooClass.class.getProtectionDomain().getCodeSource().getLocation();
//jar.toString() begins with file:
//i want to trim it out...
Path jarFile = Paths.get(jar.toString().substring("file:".length()));
FileSystem fs = FileSystems.newFileSystem(jarFile, null);
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(fs.getPath(fooFolder));
for(Path p: directoryStream){
InputStream is = FooClass.class.getResourceAsStream(p.toString()) ;
performFooOverInputStream(is);
/** your logic here **/
}
}catch(IOException e) {
throw new FooException(e.getMessage());
}
}
else{
/** IDE case */
Path path = Paths.get(uri);
try {
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path);
for(Path p : directoryStream){
InputStream is = new FileInputStream(p.toFile());
performFooOverInputStream(is);
}
} catch (IOException _e) {
throw new FooException(_e.getMessage());
}
}
स्रोत
2017-03-06 18:15:21
इससे मदद मिलेगी: http://stackoverflow.com/questions/1529611/how-to-write-a-java-program-which-can-extract-a-jar-file-and-store-its-data -इन-एस –
यह भी देखें: http://stackoverflow.com/questions/4764347/extract-and-load-dll-from-jar –
पहला वाला एक जार फ़ाइल निकालने के बारे में है, जो कि आखिरी चीज है कोशिश करना चाहते हैं, एक बदतर-बदतर स्थिति की तरह !! अगर मैं निकाली गई फाइलों को चाहता हूं, तो मैं उन्हें इंस्टॉलेशन समय पर अपने इंस्टॉलेशन फ़ोल्डर में डाल दूंगा! मैं उन्हें जार के अंदर से एक्सेस करना चाहता हूं, और मेरा कारण यह है कि, अगर रिसोर्सएस्ट्रीम एक फ़ाइल तक पहुंच सकता है, जावा भी उस जार में फ़ोल्डरों को एक्सप्लोर करने की आवश्यकता के बिना एक्सप्लोर करने में सक्षम होना चाहिए !! लेकिन धन्यवाद ... –