0

I have java web app. I am using ANT to build the application. I have a file in the resources folder. I want to open that file using a pdfViewer. The frame of the pdf viewer is getting launched. But I am not able to access the file present in the resources folder. here is my code.

final String fileName="Installation.pdf"; 

int pagenum = 0;
RandomAccessFile raf = new RandomAccessFile (new File(fileName), "r");
FileChannel fc = raf.getChannel ();
ByteBuffer buf = fc.map (FileChannel.MapMode.READ_ONLY, 0, fc.size ());
PDFFile pdfFile = new PDFFile (buf);

enter image description here I am getting a FileNotFoundException. The main problem is that I am not able to locate the file in the resources folder. What Please help.

The above is working properly when I run it as a stand alone java application.

Droidme
  • 1,223
  • 6
  • 25
  • 45
  • Can you provide your directory hierarchy? – CAMOBAP Nov 05 '12 at 13:18
  • By "pdfViewer" DYM mean a specific API, or do you simply mean a generic viewer for PDF documents? – Andrew Thompson Nov 05 '12 at 13:23
  • You could simply *open* the file instead of launching pdf viewer. People usually have correct system association for pdf files. Here is the relevant answer: [Using Ant, how do I open a file in a browser?](http://stackoverflow.com/a/6597772/772981) – Jarekczek Nov 05 '12 at 16:46
  • Now I see this question is not about ant, so I'm removing this tag. What built the application should not matter, only the contents of jar and classpath. – Jarekczek Nov 05 '12 at 16:52
  • Is this question really about pdf? If you get the exception in `new File` call, why asking about pdfs? Perhaps this is a duplicate of: [Getting the inputstream from a classpath resource (XML file)](http://stackoverflow.com/q/793213/772981). – Jarekczek Nov 05 '12 at 16:58
  • @Jarekczek This file is goin to be on a client's server and it would be btr to have it in our war file. – Droidme Nov 06 '12 at 05:24
  • @CAMOBAP I have updated the file structure. – Droidme Nov 06 '12 at 05:30
  • The main problem I am facing is that I am not able to locate the file in the resources folder – Droidme Nov 06 '12 at 05:32
  • Are you tried use `new File(getClass().getResource("/resources/Installation.pdf"))` instead of `new File(fileName), "r"`? – CAMOBAP Nov 06 '12 at 06:58
  • @CAMOBAP I have tried it. It gives me a nullPointerException – Droidme Nov 06 '12 at 07:22
  • @CAMOBAP I am using JSF – Droidme Nov 06 '12 at 07:33
  • Have you seen [this solutions](http://stackoverflow.com/questions/6319689/unable-to-read-java-file-from-jsf)? – CAMOBAP Nov 06 '12 at 08:21
  • @CAMOBAP Tht comes as inputstream. I need a file. anyway I can do tht? – Droidme Nov 06 '12 at 08:34
  • You can use `URL url = classLoader.getResource(...)` instead of `classLoader.getResourceAsStream(...)`, and after you get the `URL` you can create `File` from it – CAMOBAP Nov 06 '12 at 08:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19140/discussion-between-camobap-and-droidme) – CAMOBAP Nov 06 '12 at 08:40

1 Answers1

0

Try this:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource("/resources/Installation.pdf");

if (url != null) {

    try {
        RandomAccessFile raf = new RandomAccessFile (new File(url), "r");

        ...

    } finally {
        input.close();
    }
}
CAMOBAP
  • 5,523
  • 8
  • 58
  • 93