0

I am building a web app based on Maven multi modules. So My project structure is like the following:

/module1
    /src/main/java
      package/Module1.java
    /src/main/resources
      conf/i18n_en.properties
/webapp
    /src/main/java
      package/Web.java
    /src/main/resources
      conf/i18n_en.properties
    /src/main/webapp

I have different properties("i18n_en.properties") for I18N in every module, I load the properties file in code and get the information. It seems good running in single module test.

private ResourceBundle rb;
String filename = BASE_NAME + "_" + LANG + ".properties";
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
        if(in == null) {
            in = c.getResourceAsStream(filename);
        }
        if (in == null) {
            in = c.getClassLoader().getResourceAsStream(filename);
        }

        if (in != null) {
                rb = new PropertyResourceBundle(in);
         }

However, when I started tomcat with IDEA, the program only load the properties in webapp! Just cannot find the "Module1" s properties!

I think it's the problem of loading properties from innner jar. For the deployed directory is as following:

/WEB-INF
  /classes
    /conf/i18n_en.properties  (from webapp)
  /lib
    /Module1.jar  (has it's own properties in it)

I was really confused. I searched every thing related for a long time and i found some thing similar, but I just can't solve the problem. I think I have done what they said.Java WebApp: Loading resource from .jar located in WEB-INF

I don't know where is the problem, There must be some ways to load the properties inner jar. Anyone knows anything , please help me, thank you very much~

Community
  • 1
  • 1
Alvin
  • 3
  • 1
  • What is value of your BASE_NAME variable ? – Jekin Kalariya Sep 08 '16 at 08:12
  • you need to add '/' before BASE_NAME – Jekin Kalariya Sep 08 '16 at 08:16
  • yes, I have tried what you said. like " /conf/i18n_properties", "conf/i18n_properties". It just can't find the file if it's packaged in the jar.@JekinKalariya – Alvin Sep 08 '16 at 09:41
  • in which if condition it goes? and what is ypur value of c – Jekin Kalariya Sep 08 '16 at 09:48
  • I use "/conf/i18n_properties", and I run Unit Test in "Module1", It could find the "properties" successfully. `c` is a varible of Class which call the code. – Alvin Sep 08 '16 at 09:54
  • I debuged the program, and found that `someclass.getResource[AsStream]("...")` always go to find something under `/WEB-INF/classes` if under the web server. And also the `ClassLoader` is the `webappClassLoader`. I think this is really abnormal, for the code which I run is under the `Module1.jar`. – Alvin Sep 08 '16 at 09:59

1 Answers1

0

In jar file, you can't use ClassLoader() method to find an element.
You can use:

try {
      URL url2 = new URL(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().toString()+your path to file in structre);
      InputStream in = url2.openStream();
} catch (URISyntaxException ex) {
      Logger.getLogger(PrepareGUI.class.getName()).log(Level.SEVERE, null, ex);
}
Numb
  • 155
  • 3
  • Thank you for your help. What you want to do is to construct a URL which could find the file in jar. I try it and it's really work! But, there is a little difference that I use the following code: `new URL("jar:" + c.getProtectionDomain().getCodeSource().getLocation().toString() +"!"+ filename);` I have noticed the specific URL format for jar by the `Spring`. – Alvin Sep 08 '16 at 11:06
  • I really want to know why does the `ClassLoader.getResourceAsStream(...)` can't find file in jar. I saw so many answer on the Internet gave the advice. Do you known why ? Or do I take something wrong ? – Alvin Sep 08 '16 at 11:09
  • I read about jar file ago. That article say that when your resourse is in JAR file, it's not a File anymore. A File is only a physical file on the filesystem. So you can't use ClassLoader.getResourceAsStream(...) to find file in jar. – Numb Sep 09 '16 at 02:11