java - Problems loading resource files -
im working on little programm, should annoying copy&paste stuff me. i've written code allows me insert right titles, numbers etc. , copy inputs in 4 different files @ right place. have prepared files (.xml , .html) editing easier. im using keyword, , .replaceall("", "");
my plan keep prepared files resource, drag .jar anywhere on computer let programm generate new files resources , edit files in same directory .jar.
okay whenever launch application via eclipse, fine. programm creates new files based on inputs using templates, , edits existing files chose edited. when export .jar programm stops @ point, without error or that.
jarpath = getclass().getprotectiondomain().getcodesource() .getlocation().getpath().substring(1); //substring removes / jarpathparent = jarpath.replace("alpha.jar", ""); // replacing "" directory .jar is. public void generate(string s) throws ioexception { string path = "", content = ""; file file = null, outputfile = null; switch (s) { case "index": dialog.info("", "1"); path = getclass().getresource("index.html").tostring() .replace("file:/", ""); dialog.info("", "2"); file = new file(path); dialog.info("", "3"); outputfile = new file(jarpathparent + "/index.html"); dialog.info("", "4"); content = fileutils.readfiletostring(file); dialog.info("", "5"); content = content.replaceall("xfortitle", title); dialog.info("", "6"); fileutils.writestringtofile(outputfile, content); dialog.info("", "7"); break; case "index2": . . .
i have used little dialog-class created, spot programm stops. fourth dialogbox last 1 thats popping up, problem seem
content = fileutils.readfiletostring(file);
i dont know whats wrong, maybe it's because use apache-commons-io (fileutils) or that?
and maybe pastebin-link whole code: http://pastebin.com/px7sygyu (the imagelabel , .wav amusement.)
___________edit________________
okay can load resource content
by using
inputstream input = getclass().getresourceasstream("/classpath/to/my/file");
and code-snippet from
http://www.mkyong.com/java/how-to-convert-inputstream-to-string-in-java/
so have content of file string, , want write new file (in directory, put .jar)
inputstream input = getclass().getresourceasstream("index.html"); content = getstringfrominputstream(input); dialog.info("", "2"); outputfile = new file(jarpathparent + "index.html"); dialog.info("", "3"); content = content.replaceall("xfortitle", title); dialog.info("", "4"); dialog.info("", outputfile.getpath()); try { fileutils.writestringtofile(outputfile, content); } catch (exception e) { dialog.vieltext("", e.tostring()); } dialog.info("", "5");
so crashes @ fileutils.writestringtofile(...); dont det dialog exception should caught in try catch. checked outputpath, can see in code, , seems okay. other ideas on how fix, or how write string(contains .html template) file ?
in jar, jarpath
via getcodesource()
contain
jar:path_of_jar!path_in_jar
where path_of_jar is
file:/.... .jar
with java 7 use zip filesystem file based maintenance.
as, in general, bad way change running code, reconsider using build pipe line jar generated. fan of maven follow path, of generated resources.
Comments
Post a Comment