0

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)
  • Can you share the code of how you're accessing the resource? Some implementations have changed, might be the reason for you ending up getting a `null` as result. – Naman Sep 19 '18 at 03:08
  • One such example by @Jano from a quick search - https://stackoverflow.com/a/46874004/1746118... Another using gradle https://stackoverflow.com/questions/48673769/java-9-module-system-resources-files-location – Naman Sep 19 '18 at 03:22
  • If you are migrating your code to modules then you should have no issue find the resources in your own module with Class.getResourceXXX. If the issue is that you are using the ClassLoader.getResourceXXX methods to find your own resources then you may have to change that code as part of the migration. More detail in the question would be useful. Also would be useful to first check that the application runs on JDK 11 first, before you migrate it to modules. That might eliminate other issues from the discussion. – Alan Bateman Sep 19 '18 at 06:40
  • I figured out. I have a set 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. After 1 day of trying different combinations solution was: "--module-path ...pathTotestClasses;$buildDir/resources/main;". So you need to add ".../resources/main" as module path – Konstantin Pupkov Sep 19 '18 at 23:35

1 Answers1

0

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