1

I am trying to deploy my application to openshift and I got to a problem running it. The application is deployed locally to the exactly same version of JBoss (JBoss AS 7.1.1 Final) and it is working, while it does not work when deployed to openshift. I am not using any scaling.

The problem is with the code sample:

  String s= SimpleClass.class.getResource("/myproperties.properties").getPath();
  prop.load(new FileInputStream(s));

When deployed locally to JBoss AS, it works like expected. However when I deploy it to the openshift instance, the exception occurs:

java.io.FileNotFoundException: /content/Mailinglist.war/WEB-INF/classes/myproperties.properties (No such file or directory)
java.io.FileInputStream.open(Native Method)
java.io.FileInputStream.<init>(FileInputStream.java:146)
java.io.FileInputStream.<init>(FileInputStream.java:101)
mailinglistonline.server.export.util.PropertiesParser.parseDatabaseConfigurationFile(PropertiesParser.java:15)

EDIT:

The Mailinglist.war/WEB-INF/classes/myproperties.properties suffix is the correct and the same as I have locally, however the /content prefix is quite weird, since probably no /content directory exists in the openshift. This path should be probably going to the JBoss instance that deployed that .war. The question is mainly why is such path/prefix generated and if I can somehow get it working.

Matej Briškár
  • 599
  • 4
  • 17
  • Did you verify if the path - `/content/Mailinglist.war/WEB-INF/classes/myproperties.properties`, really exists? – Bhesh Gurung Apr 29 '14 at 16:14
  • I doubt it exists. (cd /content => No such file or directory exists) The `Mailinglist.war/WEB-INF/classes/myproperties.properties` suffix is the correct and the same as I have locally, however the `/content` prefix is quite weird. The question is mainly why is such path/prefix generated and if I can somehow get it working. I will provide that information to the question – Matej Briškár Apr 29 '14 at 16:25
  • 2
    Not sure why that is not working. But you could try `prop.load(SimpleClass.class.getResourceAsStream("/myproperties.properties"))`, or `prop.load(SimpleClass.class.getClassLoader().getResourceAsStream("myproperties.properties"))` instead. – Bhesh Gurung Apr 29 '14 at 16:51
  • Actually, this one works, thank you very much. Any idea why this happens? – Matej Briškár Apr 30 '14 at 09:30
  • 1
    The methods that I suggested are for reading resources in the classpath. The solution you have in your post is to read files from the actual file system. Most probably on local jboss, the archive file (Mailinglist.war) is exploded into a directory and deployed, so you end up with a valid filesystem path. While on openshift, archive must be deployed as is. When you try to get the path, you are actually trying to the path to the file that is inside another **file** (i.e. Mailinglist.war), which is inherently incorrect when talking about the filesystem path, so end result is an incorrect path. – Bhesh Gurung Apr 30 '14 at 15:28
  • All in all, when you need to read a resource in the classpath (in-memory stuffs), don't try to read it as filesystem file. Always read them as the classpath resource, that way no matter how your application is deployed, it will always succeed. – Bhesh Gurung Apr 30 '14 at 15:31

1 Answers1

2

Instead of using Class.getResource() you should use Class.getResourceAsStream() and load your properties that way. Using the resource path would only work if you deploy your WAR exploded. I don't believe deployments are exploded on OpenShift.

Something like the following would be a better way to handle it.

Properties props = new Properties();
props.load(SimpleClass.class.getResourceAsStream("/myproperties.properties"));
James R. Perkins
  • 16,800
  • 44
  • 60