• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Freemarker loading templates from resources

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I have designed an app that takes user input through a gui and saves it into a file, then seeds a template file with this data and saves a new file... I want to deploy this app for both mac and windows, and of course have hit the filepath wall . i would like to store my templates in the jar itself, and have managed to put them in a "resources" folder but am unable to find a way to access them. I am using the freemarkerDo(Properties c, String filepath) method to create my final output, and i am at a loss as to how i can properly pass the filepath to this method... all i am doing now is freemarkerDo(data, "stipab.dat"); and that loads the file from the default directory... how do i properly write the string that points to the files that reside in the class's resources folder? ack, i am having a hell of a time describing this issue, let alone resolving it. any assistance would be very much appreciated!

edit:
i am also aware of a built in method from freemarker: void setClassForTemplateLoading(Class cl, String prefix); but it simply does not work... any advice?
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you try to load a file that is actually in the classpath of your application, for example in the classes directory, you can use



or, if you don't want to have a headache with paths under different OSs, you can get the absolute path, plateform dependent, this way : new File("/some/path").getAbsolutePath().

3rd trick, the static attribute File.pathSeparator varies if you are under unix, windows etc ... Under unix, it is initialized as "/" at the start of the JVM, as "\" under windows.

Hope this should help you ;)
 
Sancho Xavier
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, thanks for your help, i feel a bit closer to my goal since one of my methods requires a path as a String (freemarkerDo(Properties datatominglewithtemplate, String pathtotemplatefile) ), i have gone with the new File("/some/path").getAbsolutePath() method.
i wrote a test app that looked at a file in the same dir as the test jar and saved the absolute path in a String variable (i.e: InputDataLocation = new File("inputdata.dat").getAbsolutePath(); ) it then tests the accessibility of the same file using the absolute path rather than simply "inputdata.dat" (so "/Users/sancho/NetBeansProjects/filepathtest/dist/inputdata.dat" instead of simply "inputdata.dat") however the app cannot find the file this way! i imagine i must prepend something to this string or... i dont really know. currently i am developing this on a mac, but of course it will run on winxp as well... how would you recommend that i implement this cross platform file access? thanks again!

i guess ideally i would access these files through getResource() or getResourceAsStream(), but i need a String for the path, not a URL or InputStream... suggestions?
 
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic