简体   繁体   English

从Java 9+中的src / main / resources作为模块加载属性文件的任何源代码示例

[英]Any source code example to load property file from src/main/resources in Java 9+ as module

I am migrating Java 8 application to JDK11 (early access). 我正在将Java 8应用程序迁移到JDK11(早期访问)。 New code suppose to be a module. 新代码应该是一个模块。 It read an error message from property file in Java8, but return 'null' in Java 11. Any help how to load/read resourceInputStream from /src/resources/messges/myerrors.properties 它从Java8中的属性文件中读取一条错误消息,但在Java 11中返回“ null”。有关如何从/src/resources/messges/myerrors.properties加载/读取resourceInputStream的任何帮助。

Any help appreciated. 任何帮助表示赞赏。
tree :


    ├── build.gradle
    ├── out
    │   └── production
    │       ├── classes
    │       │   ├── com
    │       │   │   └── acme
    │       │   │       └── Main.class
    │       │   └── module-info.class
    │       └── resources
    │           └── messages
    │               └── mymessages.properties
    ├── settings.gradle
    └── src
        ├── main
        │   ├── java
        │   │   ├── com
        │   │   │   └── acme
        │   │   │       └── Main.java
        │   │   └── module-info.java
        │   └── resources
        │       └── messages
        │           └── mymessages.properties
        └── test
            ├── java
            └── resources

Java : Java的



    package com.acme;

    import java.util.Locale;
    import java.util.PropertyResourceBundle;
    import java.util.ResourceBundle;

    public class Main {
        public static void main(String args[]){
             Main m=new Main();
             m.process();
        }
    void process(){
        PropertyResourceBundle prb= (PropertyResourceBundle)ResourceBundle.getBundle("messages.mymessages",Locale.getDefault());
        String key="key1";
        String value= prb.getString(key);
        System.out.println(key+":"+value);
        }
    }
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name messages.mymessages, locale en_US
at java.base/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2045)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1679)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1572)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1546)
at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:914)
at resource.main/com.acme.Main.process(Main.java:13)
at resource.main/com.acme.Main.main(Main.java:10)

For the code above solution is: "--class-path ./out/production/resources". 对于上面的代码,解决方案是:“-class-path ./out/production/resources”。 I figured out. 我想通了。 Also, another variation is this. 另外,这是另一种变化。 I have a set of JUnits. 我有一组JUnit。 During test code need to generate error message from /src/main/resources/messages/mymessages.properties. 在测试期间,代码需要从/src/main/resources/messages/mymessages.properties生成错误消息。 However, the test wasn't able to "see" resources from "main". 但是,该测试无法从“主”中“查看”资源。 After 1 day of trying different combinations solution was: "--module-path ...pathTotestClassesOrOtherProductionClasses;$buildDir/resources/main;$buildDir/resources/test;". 经过1天的尝试不同组合的解决方案是:“-module-path ... pathTotestClassesOrOtherProductionClasses; $ buildDir / resources / main; $ buildDir / resources / test;”。 So you need to add ".../resources/main" as module path 因此,您需要添加“ ... / resources / main”作为模块路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM