【问题标题】:How GraalVM integrate resources file in native imageGraalVM 如何在原生镜像中整合资源文件
【发布时间】:2020-08-28 17:02:36
【问题描述】:

基于this

默认情况下,原生镜像构建器不会集成任何 在将图像构建到类路径中的资源 它创建的图像。要进行诸如 Class.getResource() 之类的调用, Class.getResourceAsStream() (或相应的 ClassLoader 方法) 返回特定资源(而不是 null),应 需要明确指定在图像运行时可访问。

这解释了如何通过getResource() 访问资源文件。我有一个用例,我需要在 iOS 的 WebView 中加载资源文件,所以基本上我试试这个:

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();
}

这些url.toString()url.toExternalFormurl.getPath()都不会在原生图像中返回有效的可加载路径,它们的值要么是resource:demo/index.html要么是file://demo/index.html

有谁知道资源文件在原生镜像中是如何管理的?它们是否在单个二进制文件中合并?有没有机会通过文件协议定位资源文件?

【问题讨论】:

    标签: java graalvm graalvm-native-image


    【解决方案1】:

    你可以这样做:

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

    要获得完整的图片,请查看此链接 https://docs.oracle.com/en/graalvm/enterprise/20/docs/reference-manual/native-image/Resources/

    【讨论】:

      猜你喜欢
      • 2020-09-09
      • 2021-02-03
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      • 2021-05-10
      • 2021-09-09
      • 1970-01-01
      相关资源
      最近更新 更多