2

Im working on a little programm, which should do the annoying copy&paste stuff for me. So i've written a code that allows me to insert the right titles, numbers etc. and that would then copy my inputs in 4 different files at the right place. I have prepared some files (.xml and .html) so that editing is easier. Im using a keyword, and then .replaceAll("", "");

My plan is to keep the prepared files as a resource, so i could drag my .jar anywhere on my computer an let the programm generate new files from my resources and also edit files that are in the same directory as my .jar.

Okay so whenever i launch my application via eclipse, everything is fine. The programm creates new files based on my inputs using my templates, and edits the existing files i chose to be edited. But when i export everything into a .jar the programm stops at a point, without an error or anything like that.

jarpath = getClass().getProtectionDomain().getCodeSource()
            .getLocation().getPath().substring(1);
//substring removes a / 

jarpathparent = jarpath.replace("alpha.jar", "");
// replacing with "" to get the only the directory where the .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 a little dialog-class i created, to spot where the programm stops. The fourth dialogbox is the last one thats popping up, so the problem seem so be

 content = FileUtils.readFileToString(file);

I dont know exactly whats wrong, but maybe it's because i use apache-commons-io (FileUtils) or something like that?

And maybe a pastebin-link with the whole code: http://pastebin.com/Px7SygYu (The imagelabel and the .wav are just for amusement.)

___________EDIT________________

Okay so i can now load the resource content

by using

InputStream input = getClass().getResourceAsStream("/classpath/to/my/file");

and a code-snippet from

http://www.mkyong.com/java/how-to-convert-inputstream-to-string-in-java/

So i now have the content of the file as a string, and want to write it to a new file (in the directory, where i put my .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 it now crashes at FileUtils.writeStringToFile(...); but i dont det the dialog with the exception that should be caught in my try catch. i checked the outputpath, as you can see in the code, and it seems to be okay. Any other ideas on how to fix, or even how to write my string(contains the .html template) to a File ?

  • Are you *sure* it simply hangs, rather than throwing an `IOException` out of `generate` to some place that ignores it? I suggest putting your own `try ... catch` around the problematic line to double check; printing the exception stack trace if one is caught. – Jason C Aug 12 '13 at 20:32
  • i did surround the line with a try/catch-block, and then used a dialogbox again to throw the error at me.but the box didnt even popped up. – user2676280 Aug 12 '13 at 20:37
  • have a look at [this](http://stackoverflow.com/a/3370096/594406) answer to see how to access files within your jar – Katona Aug 12 '13 at 20:40
  • Thx Katona, i can now load the resource.But i cant write it to a new file now -.- – user2676280 Aug 13 '13 at 12:25

1 Answers1

0

In a jar, the jarpath via getCodeSource() will contain

jar:PATH_OF_JAR!PATH_IN_JAR

where PATH_OF_JAR is

file:/.... .jar

With Java 7 you could use a Zip FileSystem to do file based maintenance.

As, in general, it is a bad way to change running code, reconsider using a build pipe line where the jar is generated. As fan of maven I often follow this path, of generated resources.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138