gpt4 book ai didi

java - GraalVM如何在原生镜像中集成资源文件

转载 作者:行者123 更新时间:2023-12-05 06:12:26 25 4
gpt4 key购买 nike

基于 this :

By default, the native-image builder will not integrate any of theresources which are on the classpath during image building into theimage 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 shouldbe accessible at image runtime need to be explicitly specified.

这解释了如何通过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.htmlfile://demo/index.html

有谁知道原生镜像中资源文件是怎么管理的?它们是否有点合并到单个二进制文件中?有没有机会,我们是否能够通过文件协议(protocol)定位资源文件?

最佳答案

你可以这样做:

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/

关于java - GraalVM如何在原生镜像中集成资源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63638125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com