Joseph
Joseph

Reputation: 1463

Can't find file on classpath grails war/tomcat

I have put a file in my grails-app/conf package called size_config.xml . When the war is built then unpacked the file shows up in WEB-INF/classes with the expected name. However, when I try to reference the file in my application it claims that the file is not found. I've tried all combinations that I can think would be logical in trying to reference including:

new File("WEB-INF/classes/size_config.xml")
new File("classes/size_config.xml")
new File("size_config.xml")
new File("grails-app/conf/size_config.xml")

and none of these seem to work. When I run my local integration tests I use "grails-app/conf/size_config.xml" and it finds the file just fine. Since the file is being packed up fine, I'm assuming its not a deploy config issue, but rather some minor item I'm failing to see. Ideas?

Upvotes: 1

Views: 2576

Answers (2)

Arthur Neves
Arthur Neves

Reputation: 12138

try this out:

def servletContext = org.codehaus.groovy.grails.web.context.ServletContextHolder.servletContext 
def file = servletContext.getResource("/WEB-INF/classes/size_config.xml")

Upvotes: 3

BalusC
BalusC

Reputation: 1109532

You're attempting to treat classpath resources as local disk file system paths using File while relying on default working directory which is not controllable from inside the application. This is not right. You need to obtain it as a classpath resource. The /WEB-INF/classes is by default covered by the classpath. So you just need to obtain "size_config.xml" from the classpath.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream sizeConfig = classLoader.getResourceAsStream("size_config.xml");
// ...

Upvotes: 0

Related Questions