tags:

views:

298

answers:

2

I have a need to generate complicated text files from a Java desktop application.

I've decided the code would be considerably easier to implement if I could copy a template file and tailor it to whatever needs to be constructed.

I will only be distributing a jar to the customers. Is there a best-practice on how to handle this?

  • Is it even possible to copy a resource from the jar at runtime?
  • Is it better to "auto-generate" a template on first execution of the jar?
+3  A: 

You can read a file stored in your jar using:

Class.getResourceAsStream(pathToFile);

See this question for details.

kgiannakakis
+1  A: 

Yes, you can read a resource from your JAR and write it to your file system. Ad kgiannakakis suggests, you'll want to get resource as stream. However, I'd suggest using this approach instead if you're going to be running in an application server:

Thread.currentThread().getContextClassLoader().getResourceAsStream()

It tend to be a more reliable source for a classloader.

Geoffrey Wiseman