2

Many similar questions have been asked, but there's only so much pain I can take before I have to ask the question myself.

Anyway, I have files in jar file/input, and I need to read the list of the names of the files in that folder (and later, read the files themselves.)

Attempt #1:

String path Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
// "/Applications/company/extensions/components/Box/Box/Box.jar"
String decoded = URLDecoder.decode(path, "UTF-8");
// "/Applications/company/extensions/components/Box/Box/Box.jar"
File[] files = (new File(decoded + "/input")).listFiles();
// files = null (bad)

Attempt #2:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource("input");
// url.file: "file:/Applications/company/extensions/components/Box/Box/Box.jar!/input"
// url.path: (ditto)
// url.protocol: "jar"
// url.host: ""
// url.query: null
// url.port: -1
java.net.URI uri = new java.net.URI(url.getProtocol(), url.getAuthority(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
// URISyntaxException --> "Relative path in absolute URI" (bad)

Other miscellaneous attempts in-between which had the same result. I simply cannot read the folder in that jar...

Anybody know how to fix this?

Ryan
  • 729
  • 1
  • 10
  • 25
  • http://stackoverflow.com/questions/1429172/how-do-i-list-the-files-inside-a-jar-file – Oleg Mikheev Feb 27 '12 at 05:55
  • *"so much pain I can take before I have to ask the question myself."* How much pain will it take for you to outline the extraordinary need for this functionality? Fill in the extraordinary need and there will likely be better strategies put forward to achieve it. (And I suspect that all those other threads failed to get good answers because the OP was too focused on what they were trying to ***do,*** rather than what they were trying to ***achieve.)*** BTW - who builds the Jar? – Andrew Thompson Feb 27 '12 at 06:08
  • The program cannot recompile once deployed, thus it has to dynamically change based on events that trigger reading the files in this folder. And pain = 2 hours, so far. I am trying what has been posted but haven't been able to get anything to work yet. And I build the jar ** – Ryan Feb 27 '12 at 06:16
  • I just read you cannot modify the jar during runtime, thus making my problem moot. I think I am just going to store the files remotely and read them with an HTTP call. Unless somebody has a better idea... ? http://stackoverflow.com/questions/6805616/read-a-file-from-inside-the-jar-folder – Ryan Feb 27 '12 at 06:30

2 Answers2

0

Maybe I've misunderstood your question, but... considered to use java.util.jar package?

Here is an example: Showing jar file content

Hope this helps

Community
  • 1
  • 1
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
0

As per How do I list the files inside a JAR file?

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. */
  ...
} 
Community
  • 1
  • 1
Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95