0

So I've been fiddling with this for an hour or so now, checking out different methods of running a bat file from inside the java package.

I've got it inside resources/scripts, two things I've tried.

String filepath = curDir + "\\JavaArchive\\Mainfolder\\resources\\scripts\\Test.bat";
    try {
        Runtime.getRuntime().exec("cmd /c start " + filepath);
    } catch (IOException ex) {
        Logger.getLogger(DropDownTest.class.getName()).log(Level.SEVERE, null, ex);
    }

and

        try {
            ClassLoader loader = DropDownTest.class.getClassLoader();
            URL resource = loader.getResource("/scripts/Test.bat");
            Process exec;
            exec = Runtime.getRuntime().exec(resource.getPath());

            InputStream inputStream = exec.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }           } catch (IOException ex) {
            Logger.getLogger(DropDownTest.class.getName()).log(Level.SEVERE, null, ex);
        }

Nothing has worked out at all, not exactly sure where to go from here.

Any help with figuring this out would be appreciated.

Edit: Flagged as a duplicate question (Very helpful >.>)

After reading that post, while it didn't really help me at all, it still only renders the first method I tried as a dud, but not the second method.

Thanks

Yuuen
  • 45
  • 7
  • 1
    Possible duplicate of [Executing batch file from runnable jar gives path not found error](http://stackoverflow.com/questions/10913320/executing-batch-file-from-runnable-jar-gives-path-not-found-error) – Andreas Fester Apr 11 '16 at 07:38
  • @AndreasFester That only covers the first part of what I said, I feel it's a bit unfair to flag a post as a duplicate without reading the full post. The thread you listed barely relates to my problem and doesn't pose a relevant solution. – Yuuen Apr 11 '16 at 07:54
  • Try removing the leading slash at `loader.getResource("/scripts/Test.bat");` and make sure that your script is located at `src/scripts/Test.bat` (not in the package directory which contains your java class). With that, your code works for me. Besides than that, you can avoid downvotes and close votes by following some rules, like adding error messages and output you get. `Nothing has worked out at all` is not very helpful. – Andreas Fester Apr 11 '16 at 08:00
  • @AndreasFester Well to be honest the error messages weren't very helpful either, I thought that if there was an obvious mistake with my code someone would pick it out. – Yuuen Apr 11 '16 at 08:01
  • Well, sometimes even strange error messages can be helpful if someone has seen them earlier ;-) Anyway, does removing the slash work for you? – Andreas Fester Apr 11 '16 at 08:03
  • @AndreasFester Yes and no, after adjusting the file path to it's correct location, it's definitely starting the cmd process but it's not displaying the command prompt. So I tried building the jar to see if that would change anything, but it stopped working. – Yuuen Apr 11 '16 at 08:06
  • Right, just tried the same - that approach only works when script is still available on the file system (that is what I actually expected). It does not work anymore once the script is packaged into a jar file, since `Runtime.exec` can not acces the script in the jar file. So, the duplicate is still true: *You need to extract the script from the jar file to a temporary location, and then execute it from there*. That is what the accepted answer from the linked question says. – Andreas Fester Apr 11 '16 at 08:10
  • @AndreasFester Damn, guess i'll have to go for my external subfolder idea after all, putting the lengthy stuff in the bats. I assumed the issue was just with the windows file system not understanding how to view a .jar but if I can't run anything from inside the jar, that limits the hell out of what I had planned. Does this same problem occur when trying to open a txt file from inside the archive? Thanks for the help by the way. – Yuuen Apr 11 '16 at 08:14

0 Answers0