简体   繁体   中英

How GraalVM integrate resources file in native image

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?

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM