Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
public class FileUtils {
public static String getResourceContents(String resource) throws URISyntaxException, IOException {
Path resourcePath = getResourceAsPath(resource);
return getPathContents(resourcePath);
private static Path getResourceAsPath(String resource) throws URISyntaxException, IOException {
URI uri = FileUtils.class.getResource(resource).toURI();
if (isResourceInJar(uri)) {
return getResourceFromJar(uri);
} else {
return Paths.get(uri);
private static boolean isResourceInJar(URI uri) {
return uri.getScheme().equals("jar");
private static Path getResourceFromJar(URI fullURI) throws IOException {
// see
String[] uriParts = fullURI.toString().split("!");
URI jarURI = URI.create(uriParts[0]);
FileSystem fs;
try {
fs = FileSystems.newFileSystem(jarURI, Collections.<String, String>emptyMap());
} catch (FileSystemAlreadyExistsException ex) {
fs = FileSystems.getFileSystem(jarURI);
String resourceURI = uriParts[1];
return fs.getPath(resourceURI);
private static String getPathContents(Path path) throws IOException {
return new String(Files.readAllBytes(path));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.