Why Is My Uri Not Hierarchical

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

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.

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:

  1. Try to use getClass().getResourceAsStream() and use that instead of Files or
  2. extract the files into some directory and use File in the same way as you are trying now.

IllegalArgumentException: URI is not hierarchical when calling existing folder

it should be file:// not file:

Maven - URI is non hierarchial

I managed to solve this issue using maven-remote-resources-plugin . Now when I run mvn clean install on the master POM the framework runs from e2e.

In the module containing the resources I wanted to share, I added the following to the POM file

<build>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.7.0</version>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*.xml</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>

In the module where I want to use the resource. I added the following

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.7.0</version>
<configuration>
<resourceBundles>
<resourceBundle>{groupId}:{resource artifactId}:1.0-SNAPSHOT</resourceBundle>
</resourceBundles>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>

URI is not hierarchical

When you run the code from within Eclipse it uses the compiled classes (by default in folder 'target'). However if you run the code from external normally you use a JAR file created by Eclipse.

And this problem arises when referencing something inside the JAR which is explained by the linked questions.

In short: URIs in the file system are syntactically correct. An URI referencing something into a JAR is no more a valid URI.



Related Topics



Leave a reply



Submit