ben39
ben39

Reputation: 2577

Accessing a resource that is in separate JAR file from main code

I am writing a project that is packaged as a WAR file and is structured as follows:

MyProject.war

| --- META-INF/
| --- WEB-INF/
      |--- classes/ (My compiles project classes)
      |   |--- com
      |       |--- package
      |           |--- MyClass.class
      |--- lib/
          |--- random.jar
          |   |--- file1.txt
          |   |--- file2.txt
          |--- MyProject.jar
              |--- com
                  |--- package
                      |--- MyClass.class

How can I access file1.txt and file2.txt from my MyClass.java file as an input stream?

Upvotes: 0

Views: 195

Answers (1)

Perception
Perception

Reputation: 80598

As long as the JAR containing the resources is in your classpath, you should be able to load them via the context classloader.

final InputStream stream = Thread.currentThread().getContextClassLoader()
      .getResourceAsStream("file1.txt");

Upvotes: 1

Related Questions