Alex
Alex

Reputation: 646

How to read file from ear/war/jar?

I have EAR which contains another WAR. The project structure is shown below:

project.ear
 |-- lib
 |-- META-INF
 |-- project-web.war
 |    |-- META-INF
 |    |-- WEB-INF
 |    |    |-- classes
 |    |    |    `-- com
 |    |    |         `-- example
 |    |    |              `-- services
 |    |    |                   `-- ListPageService.class
 |    |    |-- lib
 |    |    |-- web.xml
 |    |    `-- weblogic.xml
 |    `-- content.html
 `-- project-services.jar

In the WAR, there's a JAX-RS class ListPageService which needs to read the content.html file. How do I access that file?

Upvotes: 3

Views: 5990

Answers (2)

Henry
Henry

Reputation: 43758

Edit: this answer is no longer applicable to the question as it is formulated now. The suggested method works only to get resources from the classpath.

Have a look at the Class.getResourceAsStream method. It allows you to access files found in the class path.

Upvotes: 2

Ian Roberts
Ian Roberts

Reputation: 122394

The usual way to get at resources in a WAR file is via ServletContext.getResource or getResourceAsStream. You should be able to get access to the ServletContext in a JAX-RS class by declaring a field annotated with javax.ws.rs.core.Context

@Context ServletContext servletContext;

then in the request handling method you can say

URL content = servletContext.getResource("/content.html");
// alternatively
// InputStream content = servletContext.getResourceAsStream("/content.html");

Upvotes: 3

Related Questions