Java Jar file: use resource errors: URI is not hierarchical
You cannot do this
File src = new File(resourceUrl.toURI()); //ERROR HERE
it is not a file!
When you run from the ide you don't have any error, because you don't run a jar file. In the IDE classes and resources are extracted on the file system.
But you can open an InputStream
in this way:
InputStream in = Model.class.getClassLoader().getResourceAsStream("/data.sav");
Remove "/resource"
. Generally the IDEs separates on file system classes and resources. But when the jar is created they are put all together. So the folder level "/resource"
is used only for classes and resources separation.
When you get a resource from classloader you have to specify the path that the resource has inside the jar, that is the real package hierarchy.
Why is my URI not hierarchical?
You should be using
getResourceAsStream(...);
when the resource is bundled as a jar/war or any other single file package for that matter.
See the thing is, a jar is a single file (kind of like a zip file) holding lots of files together. From Os's pov, its a single file and if you want to access a part of the file
(your image file) you must use it as a stream.
Documentation
URI is not hierarchical exception when running application from JAR
The call getClass().getResource(GAME_FILE);
will return a URL relative to this
class. If you are executing your program from a JAR file, it will return a URL pointing to a JAR file.
Files in java can only represent direct filesystem files, not the ones in zip/jar archives.
To fix it:
- Try to use
getClass().getResourceAsStream()
and use that instead ofFile
s or - extract the files into some directory and use
File
in the same way as you are trying now.
Java I/O help. Jar Cannot Open File: URI is Not Hierarchal
If you have to use File object do not put xls-file into resources directory.
Maven puts all files from resources directory into jar.
Java can not create File object based on file in jar-file.
Put your xls-file somewhere in file system and create File object based on its URL.
Since your xls-file is not a resource do not use getResource
.
Its URL is its full filename (with path).
URI not hierarchical need to use File class for a method
You can copy the file from the jar to a temporary file and open that.
Here's a method to create a temporary file for a given jar resource:
public static File createTempFile(String path) {
String[] parts = path.split("/");
File f = File.createTempFile(parts[parts.length - 1], ".tmp");
f.deleteOnExit();
try (Inputstream in = getClass().getResourceAsStream(path)) {
Files.copy(in, f.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
return f;
}
And here's an example of how you'd use it:
Desktop.getDesktop().open(createTempFile("/videos/tutorialVid1.mp4"));
Related Topics
How to Set Up Jax-Rs Application Using Annotations Only (No Web.Xml)
Valid Characters in a Java Class Name
How to Go About Formatting 1200 to 1.2K in Java
Does Polymorphism Apply on Class Attributes in Java
Spring Boot Adding Http Request Interceptors
What Is the Use of Marker Interfaces in Java
Best Java/Swing Browser Component
Differencebetween Jvm, Jdk, Jre & Openjdk
Does a Finally Block Always Run
Difference Between Validate(), Revalidate() and Invalidate() in Swing Gui
Java:Cannot Format Given Object as a Date
How to Serialize an Object into a String
Getting Java Version at Runtime
Why Were Applets Deprecated in Jdk 9
Using Bigdecimal to Work with Currencies
Surefire Is Not Picking Up Junit 5 Tests
Can Java Call Parent Overridden Method in Other Objects But Not Subtype