Foren

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

Pernes Panta, geändert vor 6 Jahren.

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

New Member Beiträge: 6 Beitrittsdatum: 07.11.17 Neueste Beiträge
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.
thumbnail
Olaf Kock, geändert vor 6 Jahren.

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

Liferay Legend Beiträge: 6403 Beitrittsdatum: 23.09.08 Neueste Beiträge
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.
thumbnail
Kyle Joseph Stiemann, geändert vor 6 Jahren.

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

Liferay Master Beiträge: 760 Beitrittsdatum: 14.01.13 Neueste Beiträge
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
thumbnail
Olaf Kock, geändert vor 6 Jahren.

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

Liferay Legend Beiträge: 6403 Beitrittsdatum: 23.09.08 Neueste Beiträge
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