3

Based on this:

By default, the native-image builder will not integrate any of the resources which are on the classpath during image building into the image it creates. To make calls such as Class.getResource(), Class.getResourceAsStream() (or the corresponding ClassLoader methods) return specific resources (instead of null), the resources that should be accessible at image runtime need to be explicitly specified.

That explain how to access the resource file via getResource(). I have an use case which I need to load the resources file in WebView for iOS, so basically I try this:

public void start(Stage stage) {

    WebView webView = new WebView();

    URL url = Thread.currentThread().getContextClassLoader().getResource("demo/index.html");
    
    System.out.println("url = " + url);
    System.out.println("url.toExternalForm = " + url.toExternalForm());
    System.out.println("url.getPath()= " + url.getPath());
    
    webView.getEngine().load("file://" + url.getPath());
    
    VBox root = new VBox(webView);
    root.setAlignment(Pos.CENTER);
    Scene scene = new Scene(root, 640, 480);
    stage.setScene(scene);
    stage.show();
}

None of these url.toString(), url.toExternalForm, url.getPath() return valid loadable path in native images, their value are either resource:demo/index.html or file://demo/index.html.

Does anyone know how the resource files are managed in the native image? Are they kind of merged in the single binary? By any chance, are we able to locate the resource files via file protocol?

Sam YC
  • 10,725
  • 19
  • 102
  • 158

1 Answers1

1

you can do something like this:

native-image -H:IncludeResources= <Java regexp that matches resources to be included in the image> 

to get the full picture, please check this link out https://docs.oracle.com/en/graalvm/enterprise/20/docs/reference-manual/native-image/Resources/

Munish Chouhan
  • 318
  • 4
  • 10
  • It doesn't answer the question. The documentation is horrible, explain only half of the things. For instance, if you have a resource in the root resources folder, where does it go in the native image? My code works when running in the JVM, but doesn't work in the NI because the file is not found. – Jordan Silva Nov 06 '22 at 19:31
  • @JordanSilva you can check out the examples don't in the documentation All resources can be loaded with .*/Resource.*txt$, specified as {"pattern":".*/Resource.*txt$"} in a configuration file, or -H:IncludeResources='.*/Resource.*txt$' on the command line. – Munish Chouhan Nov 10 '22 at 07:18
  • 1
    I found how to get things working. You need to use the JVM agent, so all the configuration files are generated for you. This should be the first thing they say in the doc, not that you run on so many issues and spend days until you find it. – Jordan Silva Nov 11 '22 at 09:14