mikeb
mikeb

Reputation: 11267

getResourceAsStream to get a file inside the war

I have a maven project that builds a war file with a file inside it I want to read.

No exceptions are thrown...

EDIT Note that this is not in a servlet:

@Path("")
@Stateless
public class MessageRestService {

//String liquibaseTenantDefinitionFile = "/WEB-INF/liquibase/db.tenant.xml";
//String liquibaseTenantDefinitionFile = "/WEB-INF/classes/liquibase/db.tenant.xml";
//String liquibaseTenantDefinitionFile = "WEB-INF/classes/liquibase/db.tenant.xml";
//String liquibaseTenantDefinitionFile = "/liquibase/db.tenant.xml";
String liquibaseTenantDefinitionFile = "liquibase/db.tenant.xml";

public InputStream getTenantFile(){
    try {
        return getClass().getClassLoader()
                .getResourceAsStream(liquibaseTenantDefinitionFile);
    } catch(Exception e){
        System.err.println(e);
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
    return null;
}

This file does exist in the war file:

jar -tf target/grest-0.0.1-SNAPSHOT.war | grep db.tenant.xml
WEB-INF/classes/liquibase/db.tenant.xml

The method always returns null, I've tried all kinds of permutations:

I cannot figure out how to do this. I'm using wildfly and testing with the Arquillian jboss container.

What am I doing wrong?

Upvotes: 1

Views: 3198

Answers (2)

user207421
user207421

Reputation: 310869

Spot the difference:

"/WEB-INF/liquibase/db.tenant.xml";

and

/WEB-INF/classes/liquibase/db.tenant.xml

but actually they are both wrong. It should be:

/liquibase/db.tenant.xml

as the CLASSPATH starts at /WEB-INF/classes.

Upvotes: 1

bmargulies
bmargulies

Reputation: 100013

The root of your war file is not in your webapp class loader. The WEB-INF/classes directory is in your webapp class loader. You should move the resources to there, and then just use paths rooted at WEB-INF/classes.

Upvotes: 0

Related Questions