Copy directory from a jar file

Thanks for the solution! For others, the following doesn’t make use of the auxiliary classes (except for StringUtils) /I added extra information for this solution, check the end of the code, Zegor V/ public class FileUtils { public static boolean copyFile(final File toCopy, final File destFile) { try { return FileUtils.copyStream(new FileInputStream(toCopy), new FileOutputStream(destFile)); } … Read more

Class.getResource() returns null

Assuming getClass() returns com.foo.bar.MyActionListener, getClass().getResource(“img/foo.jpg”) looks for a file named foo.jpg in the package com.foo.bar.img. If the image is not in this package, or if it is in this package but its root directory is not in the classpath, the method will return null. If the img folder is at the root of the classpath, … Read more

How to 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()); while(true) { ZipEntry e = zip.getNextEntry(); if (e == null) break; String name = e.getName(); if (name.startsWith(“path/to/your/dir/”)) { /* Do something with this entry. */ … } } } else { /* Fail… */ } Note that … Read more

What is the difference between Class.getResource() and ClassLoader.getResource()?

Class.getResource can take a “relative” resource name, which is treated relative to the class’s package. Alternatively you can specify an “absolute” resource name by using a leading slash. Classloader resource paths are always deemed to be absolute. So the following are basically equivalent: foo.bar.Baz.class.getResource(“xyz.txt”); foo.bar.Baz.class.getClassLoader().getResource(“foo/bar/xyz.txt”); And so are these (but they’re different from the above): … Read more