0

I'm having a jar file say toolkit.jar which contains resource files under com.project.resources. When I tested independently I can read those resources with Class.getResource. But when I used the jar file in war module (web service), getResource always returns null. How can I access those resource file?

com/project/resources/ contains arial.ttf
com/project/toolkit/ contains ToolKit.java

renderer is ITextRenderer object.

renderer.getFontResolver().addFont(this.getClass().getResource("../resources/arial.ttf").
    toString(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

And this toolkit.jar is using in a web service.

N K
  • 3,217
  • 5
  • 23
  • 38

1 Answers1

-1

Use getResourceAsStream() instead of getResource

InputStream in =
new InputStreamReader(FileLoader.class.getClassLoader().getResourceAsStream("file.txt") );

If its doesn't work

The best way to look to a config file from a webapp is to implement the servlet method init(). Let’ try using the same code

InputStream in =
new InputStreamReader(FileLoaderServlet.class.getClassLoader().getResourceAsStream("file.txt") );

Refer https://haveacafe.wordpress.com/2008/10/19/how-to-read-a-file-from-jar-and-war-files-java-and-webapp-archive/