GraalVM如何在本地映像中集成资源文件

huangapple go评论71阅读模式
英文:

How GraalVM integrate resources file in native image

问题

基于这个链接

> 默认情况下,native-image 构建器不会将在图像构建期间位于类路径上的任何资源集成到创建的图像中。为了使诸如 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

有人知道本机映像中如何管理资源文件吗?它们是否被合并到单个二进制文件中?是否有可能通过文件协议定位资源文件?

英文:

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?

答案1

得分: 1

你可以像这样执行:

native-image -H:IncludeResources= <Java 正则表达式,用于匹配要包含在图像中的资源>

要获取完整的信息,请查看以下链接:
https://docs.oracle.com/en/graalvm/enterprise/20/docs/reference-manual/native-image/Resources/

英文:

you can do something like this:

native-image -H:IncludeResources= &lt;Java regexp that matches resources to be included in the image&gt; 

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

huangapple
  • 本文由 发表于 2020年8月29日 01:02:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63638125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定