I am trying to access a resource file from within the jar file that is currently running. The resource I need right now is a jar file, but I need a way that will work for any file. I've tried looking at the different solutions, but none of them have worked so far. I was attempting to use a URL to point to the absolute path of the file within the jar, but that doesn't seem to work. I've checked the path, and it should be getting that file. No matter what I do, the URL always returns null. I am using Apache Commons-IO.
Here's my code so far.
private static void copyFiles() {
URL source = ClassName.class.getClass().getResource("/resources/file.jar");
File target = new File(System.getProperty("user.home") +
System.getProperty("file.separator") + "Desktop" +
System.getProperty("file.separator") + "filename");
try {
FileUtils.copyURLToFile(source, target);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
This throws a NullPointerException. Any help is appreciated. Thanks.