Java 9模块系统资源文件位置 [英] Java 9 Module system Resources files location

查看:428
本文介绍了Java 9模块系统资源文件位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有写下有问题的一切,而是想放置一张图片。问题是我应该在哪里放置包含 .properties 文件的资源目录?



查看图片了解更多信息和确切的问题。

解决方案

首先,如果您使用的是Intellij IDEA和Gradle,尝试将此添加到 build.gradle

  apply plugin:'idea'
idea {
module {
inheritOutputDirs = true
}
}

更可能的解决方案



根据我的经验,Java 9已锁定访问资源基于关于资源是否被封装的一些规则(


Instead of writing everything in question I would like to place an image. The problem is where should I place the resource directory which contain the .properties files?

See the image for more information and exact problem.

解决方案

First of all, if you are using Intellij IDEA and Gradle, try adding this to your build.gradle:

apply plugin: 'idea'
idea {
    module {
        inheritOutputDirs = true
    }
}

The more likely solution

From my experience Java 9 has locked down accessing resources based on some rules about whether the resource is encapsulated or not (see javadocs).

Things that have worked for me:

  1. Putting a resource in the root of my resources folder and calling

    getClass().getClassLoader().getResource("myresource.txt")

  2. Making the folder structure leading to your resource match a suitable package in your project. For example, if you had a com.your.package then your folder structure would be resources/com/your/package/myresource.txt. Once your had this, you can add opens com.your.package in module-info.java and get your resource by calling

    YourClass.class.getResource("/com/your/package/myresource.txt")

Other things to try

Marking your resources folder as resources in Intellij IDEA.

这篇关于Java 9模块系统资源文件位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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