1

Im generating a report from a template html file in my program. It resides in /src/main/resources and its name is "template.html". Im using ClassLoader inside the code like this:

   private String readTemplateFile() {
        String str = "";
        URL url = ClassLoader.getSystemResource("template.html");

        try {
            FileReader input = new FileReader(url.getFile());
            BufferedReader bufRead = new BufferedReader(input);
            String line;

            line = bufRead.readLine();
            str = line;
            while (line != null) {
                line = bufRead.readLine();
                str += line + "\n";

            }
            bufRead.close();

        } catch (IOException e) {
        }

        return str;
    }

It is doing nicely when you run the code inside IDE but when i make a runnable jar from it, it is generating an empty report. What is the solution? Thanks for reading.

Sean Patrick Floyd
  • 274,607
  • 58
  • 445
  • 566
MartK
  • 594
  • 2
  • 7
  • 19
  • `e.printStackTrace();` try this and tell us the error message while running from jar – jmj Nov 16 '10 at 08:47
  • Sorry i couldnt get the log in text so i got print screen of it:http://img51.imageshack.us/img51/7410/captureueu.jpg i browsed the jar with 7zip and there is a "template.html" in the root directory. – MartK Nov 16 '10 at 08:57

1 Answers1

4

If it's in a jar, it's no longer a file.

Use ClassLoader.getResourceAsStream() to retrieve the resource as an InputStream.

Then convert the InputStream to a String.

Community
  • 1
  • 1
Sean Patrick Floyd
  • 274,607
  • 58
  • 445
  • 566