Forums

angle-left Back

How to get real path to my deployed exploded war in Liferay 7?

PP
Pernes Panta, modified 7 Months ago.

How to get real path to my deployed exploded war in Liferay 7?

New Member Posts: 6 Join Date: 11/7/17 Recent Posts
I comme from liferay 6 and i want to migrate my portlets to liferay 7. I have alreaty some portlet migrate but i have a problem:
I can't more get access to my deployed exploed war folder.

Before i used this code below to access my deployed war folder:

FacesContext context = FacesContext.getCurrentInstance();
String realPathPrefix = context.getExternalContext().getRealPath(".") + "/WEB-INF/classes/";
File file = realPathPrefix + "/templates/myfiles.xml";


Now this code does not work anymore.
FacesContext.getCurrentInstance().getExternalContext().getRealPath(".") return NULL

can someone helhp me to fix that.

Note: The XML files contain email templates.
Olaf Kock, modified 7 Months ago.

RE: How to get real path to my deployed exploded war in Liferay 7?

LIFERAY STAFF Liferay Legend Posts: 4622 Join Date: 9/23/08 Recent Posts
Pernes Panta:
I comme from liferay 6 and i want to migrate my portlets to liferay 7. I have alreaty some portlet migrate but i have a problem:
I can't more get access to my deployed exploed war folder.


Somebody from the faces team might give you a more appropriate and helpful answer - I can only go so far to let you know that you can never assume you get an exploded war file in the first place. The application server is free to not explode the war file, but serve it from a zip file or any non-file-storage it prefers. You can't assume that files are available as physical files on disk - but in a web application you can typicall get the resources through the classloader through the class' getResourceAsStream method

However, I'm aware that any other answer might be more helpful, especially with regards to the faces implementation. To me it feels like you're finally finding an issue with your old implementation that happened to work in the circumstances that you found, but not within the specification options that were otherwise possible.

When Liferay 7 deploys a WAR file, it transforms it transparently into an OSGi bundle and no longer deploys it as a web application to the application server.
Kyle Joseph Stiemann, modified 7 Months ago.

RE: How to get real path to my deployed exploded war in Liferay 7? (Answer)

LIFERAY STAFF Liferay Master Posts: 655 Join Date: 1/14/13 Recent Posts
Hi Pernes,
Olaf's answer is excellent and exactly right. Rather than using ExternalContext.getRealPath(), you should be able to call ExternalContext.getResourceAsStream() to obtain the contents of your file:

InputStream inputStream = externalContext
.getResourceAsStream("/WEB-INF/classes/templates/myfiles.xml");


Then you can read the contents of the inputStream in whatever way you prefer.

- Kyle
Olaf Kock, modified 7 Months ago.

RE: How to get real path to my deployed exploded war in Liferay 7?

LIFERAY STAFF Liferay Legend Posts: 4622 Join Date: 9/23/08 Recent Posts
great - this also clarifies which class (and with it, which classloader) to choose. I'd have feared to grab the wrong one, thus I stayed very abstract... emoticon