如何让自动模块在Java 9中找到自己的资源? [英] How to let an automatic module find its own resources in Java 9?

查看:76
本文介绍了如何让自动模块在Java 9中找到自己的资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的应用程序运行Java 9,但不幸的是,当它尝试使用 classLoader.getResource(name),得到一个null。

I am trying to get my application to run with Java 9, but unfortunately one of the plain jar dependencies, when it tries to load a resource using classLoader.getResource(name), gets a null instead.

这当然适用于Java 8。

This, of course, works in Java 8.

我宣布使用模块文件依赖于相关模块,通过其jar名称引用模块的名称(糟糕),并使用添加jar-as(不使用Java 9进行自定义) - -module-path 选项。

I declared a dependency on the module in question using the module file, referring to the name of the module by its jar name (awful), and added the jar as-is (no customization for Java 9) using the --module-path option.

这是我的近似模块文件声明:

Here is my approximate module file declaration:

module my.mod {
    requires ivy; // the file is called ivy-2.4.0.jar
}

我跑使用此命令:

java --module-path my-mod.jar:ivy-2.4.0.jar -m my.mod

当我运行应用程序时,如果库没有尝试加载它,它可以正常工作资源...但如果确实如此,它会在尝试使用该资源的行中获得 NullPointerException

When I run the application, it works fine if the library doesn't try to load that resource... but if it does, it gets a NullPointerException at the line it tries to use the resource.

我可以看到资源存在于jar文件中的正确路径中。

I can see the resource is present in the correct path in the jar file.

我尝试将我的应用程序作为jar(如上所示)运行类文件:

I've tried running my application both as a jar (shown above) and just with the class files:

java --module-path modules-dir:ivy-2.4.0.jar -m my.module/my.Main

在后一种情况下,错误是不同的,但相关:常春藤不能甚至找到它试图从自己的jar加载的属性文件!

In the latter case, the error is different, but related: Ivy can't even find a properties file it tries to load from its own jar!

是否有任何解决方法,这是一个已知错误,还是我做错了什么?

Is there any workaround, is this a known bug, or am I doing something wrong?

解决方案

尝试向自动模块常春藤添加打开声明。这允许访问它的资源:

Try to add an 'opens' declaration to the automatic module 'ivy'. That allows access to it's resources:

java --add-opens ivy/<dot.separated.path.to.resources>=ALL-UNNAMED --module-path my-mod.jar:ivy-2.4.0.jar -m my.mod

这篇关于如何让自动模块在Java 9中找到自己的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆