'How do I get real path as string in Grails [duplicate]
Developed project using grails-3.3.1 and generated a Runnable WAR file. When I run using the command:
java -jar build/libs/myproject-0.1.war
It is returning null for the following line:
serveltContext.getRealPath("/someSource");
But it is working fine when deployed in a Tomcat Container.
Then tried the following way:
servletContext.getResource("someSource").getPath();
It is returning, but not the one as expected and not what getRealPath() returns.
It is returning like this:
D:/myprojects/myproject/build/libs/myproject-01.war*/
which serves no use for me. Found answers suggesting to use getResourceAsStream() but I don't want a resource, I want only String.
Solution 1:[1]
I had the same issue with Grails 4.0.11. Seems like serveltContext.getRealPath is deprecated and returns null. Your solution didn't work either for me, so I decided to go with the following approach: (found on https://howtodoinjava.com/spring-boot2/read-file-from-resources/)
import org.springframework.core.io.ClassPathResource
import org.springframework.core.io.Resource
Resource resource = new ClassPathResource("pdfResources/fonts/SFUIText-Medium.ttf")
String path = resource?.getPath()
Given the fact, that my app is called "filetest", the file is located on filetest/src/main/resources/pdfResources/fonts/SFUIText-Medium.ttf
This works with "grails run-app" and "grails war" as embedded runnable war file. Hope it helps, as it took me quite a while.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sullivan |
