1

I need to be able to access my web application's css files, that are stored under src/main/resources/styles, from the backend java controller. I want to use them for creating PDF output with iText.

In other words, I want to do something like this:

CssFile cssFile1 = XMLWorkerHelper.getCSS(new FileInputStream("src/main/resources/styles/my.css"));

However, I'm clearly not going about this correctly, as I am receiving Exceptions like this:

java.io.FileNotFoundException: styles\standard.css (The system cannot find the path specified)

How can a retrieve these files in the controller?

I tried this, but it did not work, same error:

String rcp = econtext.getRequestContextPath();
CssFile cssFile1 = XMLWorkerHelper.getCSS(new FileInputStream(rcp + "src/main/resources/styles/my.css"));
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sairn
  • 461
  • 3
  • 24
  • 58

2 Answers2

2

The FileInputStream operates on the local disk file system and all relative paths are relative to the current working directory, which is the local disk file system folder which is been opened at exactly the moment the JVM is started. This is definitely not the root of src/main/resources folder.

Given that the /src/main/resources is recognizable as a Maven folder structure for root of classpath resources, then you just need to obtain it as classpath resource by ClassLoader#getResourceAsStream() instead.

InputStream input = getClass().getResourceAsStream("/styles/standard.css");
// ...

Or if the class is possibly packaged in a JAR loaded by a different classloader.

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("styles/standard.css");
// ...

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you *again*, BalusC!!! --Ironically, as I pass this stumbling block I've encountered yet another that you may soon see posted regarding iText...--But, let me try to fix it myself first, of course. – sairn Feb 07 '13 at 18:42
-1

Thats because the JSF is a web app. Now move the styles/my.css to WEB-INF/styles/my.css this ensures the files your accessing within the controller are part of the WebApp

and now you can access the resource using

XMLWorkerHelper.class.getResourceAsStream("styles/my.css")
Sudhakar
  • 4,823
  • 2
  • 35
  • 42
  • Wrong. 1) The `/WEB-INF` folder is not part of the classpath. 2) The `Class#getResourceAsString()` doesn't exist. – BalusC Feb 07 '13 at 18:22