question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In native mode a static instance cannot load resources as the resource URL references a jar in the path

See original GitHub issue

Describe the bug

When an object is instantiated as static then the object or any objects instantiated by it are not able to load resources.

If I instantiate the object as non static, then when the object tries to load a resource, the resource URL looks like this:

resource:/diagram-interchange-dish-example.dmn

If I instantiate the same object as static, then the resource URL looks like this:

jar:file:/project/quarkus-bug-1.0-SNAPSHOT-runner.jar!/diagram-interchange-dish-example.dmn

This is the exception I get when I try to open a resource loaded by a static object:

Caused by: java.nio.file.NoSuchFileException: /project/quarkus-bug-1.0-SNAPSHOT-runner.jar
    at sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(UnixFileAttributeViews.java:55)
    at sun.nio.fs.UnixFileSystemProvider.readAttributes(UnixFileSystemProvider.java:149)
    at sun.nio.fs.LinuxFileSystemProvider.readAttributes(LinuxFileSystemProvider.java:99)
    at java.nio.file.Files.readAttributes(Files.java:1764)
    at java.util.zip.ZipFile$Source.get(ZipFile.java:1259)
    at java.util.zip.ZipFile$CleanableResource.<init>(ZipFile.java:733)
    at java.util.zip.ZipFile$CleanableResource.get(ZipFile.java:850)
    at java.util.zip.ZipFile.<init>(ZipFile.java:248)
    at java.util.zip.ZipFile.<init>(ZipFile.java:177)
    at java.util.jar.JarFile.<init>(JarFile.java:350)
    at sun.net.www.protocol.jar.URLJarFile.<init>(URLJarFile.java:103)
    at sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:72)
    at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:84)
    at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:125)
    at sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:155)
    at java.net.URL.openStream(URL.java:1165)
    at my.quarkus.test.QuarkusBug$Loader.read(QuarkusBug.java:30)

Expected behavior

the resource URL is the same for both scenarios (static and non static resource loader objects)

resource:/diagram-interchange-dish-example.dmn

Actual behavior

when the object which loads the resource is instantiated as static then the resource URL references a non-existent jar file:

jar:file:/project/quarkus-bug-1.0-SNAPSHOT-runner.jar!/diagram-interchange-dish-example.dmn

How to Reproduce?

Link to a minimal project to reproduce the issue: https://drive.google.com/file/d/1ZeIZypM1rCpA0SVsUW33uwi2hV1GlntF/view?usp=sharing

build it with

mvn clean install -Pnative

sample code:

‘’’ public class QuarkusBug implements RequestHandler<Map<String, Object>, Map<String, Object>> {

@Slf4j public static class Loader { private URL resource;

public Loader() {
  resource = Loader.class.getClassLoader().getResource("diagram-interchange-dish-example.dmn");
  log.info("-------------- LOADER CONSTRUCTOR: {}", resource);
}

public void loader() {
  resource = Loader.class.getClassLoader().getResource("diagram-interchange-dish-example.dmn");
  log.info("-------------- LOADER METHOD: {}", resource);
}

public void read() {
  log.info("-------------- LOADER READ RESOURCE: {}", resource);
  try (InputStream is = resource.openStream()) {
    log.info("-------------- LOADER READ OPERATION: {}", new String(is.readNBytes(38)));
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

}

// THE FAILING ONE private static Loader loader = new Loader();

// THE PASSING ONE // private Loader loader = new Loader();

public QuarkusBug() { loader.read(); }

@Override public Map<String, Object> handleRequest(Map<String, Object> request, Context context) { return request; } } ‘’’

Output of uname -a or ver

Linux 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Output of java -version

openjdk version “11.0.16” 2022-07-19

GraalVM version (if different from Java)

GraalVM 22.2.0 Java 11 CE (quay.io/quarkus/ubi-quarkus-native-image:22.2-java11)

Quarkus version or git rev

2.12.0.Final

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.6.3

Additional information

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tarsijcommented, Sep 13, 2022

Thanks a million @zakkak

1reaction
geoandcommented, Sep 7, 2022

@zakkak can give you all the info about to the latest changes

Read more comments on GitHub >

github_iconTop Results From Across the Web

Classpath resource not found when running as jar
6 - I'm loading a classpath resource using an @Value annotation, which works just fine when I run the application from within STS...
Read more >
Class Loading Reference - Quarkus
When running a production application using the legacy-jar package type everything is loaded in the system ClassLoader, so it is a completely flat...
Read more >
How to Avoid the Java FileNotFoundException When Loading ...
Java programs cannot easily load files once they have been compiled into the resources of a JAR or WAR file. Here's how to...
Read more >
3 ways to solve java.lang.NoClassDefFoundError in Java J2EE
One of my libraries was giving NoClassDefFoundError because of its dependency on native library. This was resolved by placing a native library(dll) file ......
Read more >
Loading Resources at Runtime - Unity - Manual
Furthermore, you may even want to load assets from a separate file or URL to reduce initial download time or allow for interchangeable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found