2

I know that there are plenty of ways to do this, however I'm experiencing problems in this task which I can't solve with the solutions that I have already found.

First of, I don't want to read a specific file inside a jar: I want to read all the files contained in a directory inside the jar given the path of the directory. That said, with a bit of researches I found out how to do this and I wrote a test code.

public class StringLocalizer { private final List<URL> files; public StringLocalizer(final String stringsDirectory) throws URISyntaxException, IOException { ClassLoader loader = Thread.currentThread().getContextClassLoader(); final BufferedReader br = new BufferedReader(new InputStreamReader(loader.getResourceAsStream(stringsDirectory), StandardCharsets.UTF_8)); files = br.lines() .map(l -> stringsDirectory + "/" + l) .map(loader::getResource) .collect(Collectors.toList()); // This line has the debug purpose of showing all the url contained in the list System.out.println(Arrays.toString(files.toArray(new URL[files.size()]))); } public static void main(String[] args) { try { new StringLocalizer("test/testDirectory/"); } catch (URISyntaxException | IOException e) { e.printStackTrace(); } } } 

First I tried the code in my IDE (Eclipse) and I got this output:

[file:/C:/Users/*my_user_name*/neon/workspace/JavaStringLocalizer/bin/test/testDirectory//test.xml] 

The code works well, this was my first thought, however when I tried to package the program inside a runnable JAR file I got an unexpected output:

[] 

Why is the list empty even if the file is packaged in the JAR file?

(The correct output should be a representation of an array containing all the files in the given directory, like the first output)

EDIT: To understand better my situation I have this files:

>MyJar.jar >classFiles >test>testDirectory>test.xml // I want to get this file 

NOTE: this directory will contain more files, so I don't want to access them statically but I want to dynamically read all the files

EDIT: Code to extract the files using the ZipFile:

public class StringLocalizer { private final List<ZipEntry> files = new ArrayList<ZipEntry>(); public StringLocalizer(final String stringsDirectory) throws URISyntaxException, IOException { URL jarUrl = getClass().getProtectionDomain().getCodeSource().getLocation(); File jarFile = new File(jarUrl.toURI().getPath()); ZipFile unzipper = new ZipFile(jarFile, ZipFile.OPEN_READ); ZipEntry dirEntry = unzipper.getEntry(stringsDirectory); Enumeration<? extends ZipEntry> entries = unzipper.entries(); for(ZipEntry entry = entries.nextElement(); entries.hasMoreElements(); entry = entries.nextElement()) { if(entry.getName().startsWith(dirEntry.getName()) && !entry.getName().equals(dirEntry.getName())) { files.add(entry); } System.out.println(entry.getName()); } System.out.println(Arrays.toString(files.toArray(new ZipEntry[files.size()]))); unzipper.close(); } public static void main(String[] args) { try { new StringLocalizer("test/testDirectory/"); } catch (URISyntaxException | IOException e) { e.printStackTrace(); } } } 

Why is the last entry ignored?

4
  • I know this doesn't solve your problem, but I've heard that jar resources behave differently depending on how the jars are run. I would highly recommend having your resources packaged separate from the jar, it saves a lot of headaches. Commented Sep 9, 2016 at 20:23
  • Did you try printing out the value of the stringsDirectory parameter? Also, I'm assuming you confirmed that the xml file is actually in the jar file? Commented Sep 9, 2016 at 20:23
  • @AmirAfghani I have extracted manually the jar file using WinRar and I have found the xml file exactly where it needs to be. Commented Sep 9, 2016 at 20:25
  • 2
    I would like to suggest that you use the Java built-in ZipFile file classes, then. docs.oracle.com/javase/7/docs/api/java/util/zip/ZipFile.html They will allow you to open up a jar, which is really a zip. Commented Sep 9, 2016 at 21:15

1 Answer 1

1

JAR files are really ZIP files, so just treat the file like it is a ZIP, like what @Jamie suggested.

Sign up to request clarification or add additional context in comments.

7 Comments

I have a question: once I get the ZipEntry object of the directory, how can I list all the files inside the ZipEntry?
I found out how to get the right ZipEntry however I think there is a problem in my code because the last ZipEntry of the jar file is ignored: I'm adding the new code in the question.
How about simply looping an extra time after? Possibly your loop condition stops one rotation before it needs to.
I already tried to manually call the entries.nextElement() method after the loop but all I got is an exception saying that there are no more elements left...
I solved this problem too using the Java 8 Stream API instead of the loop!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.