0

I am trying to load a local file located inside src/main/resources/META-INF/resources/resources/ directory. The application is a joinfaces based app that includes myfaces, omnifaces and rewrite. Before migrating my project (classic JSF WAR) to joinfaces, I was doing it by providing a real path to the file like this:

FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources");

Since my project uses omnifaces the getExternalContext() returns org.omnifaces.context.OmniExternalContext, while the whole expression returns null.

If I just run:

FacesContext.getCurrentInstance().getExternalContext().getRealPath("/")

it returns a path to a temporal directory created by the embedded tomcat (i.e.: C:\Users\User\AppData\Local\Temp\tomcat-docbase.4856998207101083356.8443\) that is empty.

In this case I do not really require an absolute path to a static resource contained inside my application, it was just one way to do it, but still I can find no method to achieve this and the joinfaces documentation is minimal. Any ideas?

akaine
  • 129
  • 9
  • You should not try to load a local file in `src/main/resources/META-INF/resources/resources/` since that is a path in your source in the IDE. Nor should you rely on the getRealPath. And a 'resources' folder inside the 'resources' inside the META-INF inside the resources folder sounds like something is wrong already. Please improve the basics. And joinfaces is 'spring-boot' related with embedded tomcat. So better to remove the java tag and add spring-boot in there. – Kukeltje Jan 05 '19 at 08:20
  • There is nothing wrong with the project. In joinfaces 4.x `META-INF/resources/` is the webroot (I know it's weird), and my webroot also got a directory named `resources`, which is normal for java webapps. Obviously `src/main/resources/` will not exist in the compiled JAR, I've mentioned it just to explain the project structure. Changed the tag to spring-boot. – akaine Jan 05 '19 at 09:49
  • This is the compiled jar structure http://ontalsoft.com.mx/downloads/jar_struct.png. – akaine Jan 05 '19 at 09:57
  • Mentioning it only caused confusion and a you can use normal war folder structure with joinfaces: https://github.com/joinfaces/joinfaces/issues/315 and please read https://stackoverflow.com/questions/12160639/what-does-servletcontext-getrealpath-mean-and-when-should-i-use-it – Kukeltje Jan 05 '19 at 10:01
  • Yes, I've seen this joinfaces discussion. The problem is that I am not using war packaging. Also all the source code examples in their github repos are of old versions, so you can't get any useful info on the project structure paths/alternatives from there either. And for the latter, I know it's a bad practice. The stackoverflow discussion you mentioned does not help, because it applies for standard web projects only which perfectly works in my case (as I mentioned in my first post). – akaine Jan 05 '19 at 10:09
  • It's not bad practice to use absolute paths per say but it is bad practice to use getRealPath since it is not portable (different versions of tomcat may behave differently even) so for me the effort to help further ends here (not using spring, joinfaces etc) Good luck – Kukeltje Jan 05 '19 at 10:19
  • Thanks anyway. I think I'll just move to alternative solutions. It was more about the interest to the cause of the issue and finding a way to detect physical path of a dynamic resource in joinfaces 4 projects. – akaine Jan 05 '19 at 10:22

0 Answers0