0

I would like to know if its possible to 'package' (as single jar) a batch file together with my java application. My application executes a batch file and hence hard-coded I've had to write the path of the batch file. My concern is that i would like that i can call the batch file without the need to alter the path for each and every machine i deploy my application.... Using Eclipse as an IDE.

Here is my code which works fine, but as i said i would love to call the batch file for example as a normal java class import....

public class LogFileScanner 
{
                private final String dir_of_log = "<My Directory>";
                private final String dir_of_batch = "<My Directory>\\Java_Tools";
                private final int toGigaByte = (1024*1024)*1024;
                private double size_in_GIGA;
                private String cur;
        .
        .
        .

    try
    {
        Runtime.getRuntime().exec("cmd /c start DailyAuto2.bat", null, getBatchFile());
    }
        .
        .
        .

    private File getBatchFile()
    {
            File batchFile = new File(dir_of_batch);
            return batchFile;
    }
}

Thanks very much for any input given.

Solved my problem by using the following :

private File getBatchFile()
{
    dir = LogFileScanner.class.getResource("DailyAuto2.bat").toString();
    dir = dir.replace("/", "\\\\");
    dir = dir.substring(7, dir.length()-16);
    File batchFile = new File(dir);
    return batchFile;
}
Raedwald
  • 40,290
  • 35
  • 127
  • 207
HCL02
  • 86
  • 8

1 Answers1

1

place the batch file somewhere in the source tree so that it becomes a "resource" included in the .jar. then either

  • use java.lang.Class.getResourceAsStream(String) to get the contents or
  • use java.lang.Class.getResource(String) to get a URL and then find the absolute path with help of https://stackoverflow.com/a/5643787
Community
  • 1
  • 1
user829755
  • 1,310
  • 11
  • 24