简体   繁体   中英

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). 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

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 :



    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". I figured out. Also, another variation is this. I have a set of JUnits. During test code need to generate error message from /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;". So you need to add ".../resources/main" as module path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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