Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Pernes Panta
How to get real path to my deployed exploded war in Liferay 7?
November 14, 2017 6:04 AM
Answer

Pernes Panta

Rank: New Member

Posts: 4

Join Date: November 7, 2017

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:
1
2FacesContext context = FacesContext.getCurrentInstance();
3String realPathPrefix = context.getExternalContext().getRealPath(".") + "/WEB-INF/classes/";
4File 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
RE: How to get real path to my deployed exploded war in Liferay 7?
November 14, 2017 6:49 AM
Answer

Olaf Kock

LIFERAY STAFF

Rank: Liferay Legend

Posts: 4148

Join Date: September 23, 2008

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
RE: How to get real path to my deployed exploded war in Liferay 7?
November 14, 2017 12:56 PM
Answer

Kyle Joseph Stiemann

LIFERAY STAFF

Rank: Liferay Master

Posts: 578

Join Date: January 14, 2013

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
RE: How to get real path to my deployed exploded war in Liferay 7?
November 14, 2017 12:24 PM
Answer

Olaf Kock

LIFERAY STAFF

Rank: Liferay Legend

Posts: 4148

Join Date: September 23, 2008

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

Participate in the State of Liferay Community 2017. Help the community and even win some prizes!