2

I'm using Apache Commons Exec to run a bat file in my specified directory.

File file = new File("C:\\Users\\Aaron\\Documents\\MinecraftForge\\forge\\mcp");
    for(String s : file.list())
    {
        if(s.equals("recompile.bat"))
        {
            DefaultExecutor executor = new DefaultExecutor();
            executor.setWorkingDirectory(file);
            CommandLine commandLine = new CommandLine("recompile.bat");

            try
            {
                executor.execute(commandLine);
            } catch (ExecuteException e)
            {
                e.printStackTrace();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

The code will only try to run the bat file if it can find the file I'm looking for, but the code comes up with

java.io.IOException: Cannot run program "recompile.bat" (in directory "C:\Users\Aaron\Documents\MinecraftForge\forge\mcp"): CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at org.apache.commons.exec.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:58)
    at org.apache.commons.exec.DefaultExecutor.launch(DefaultExecutor.java:254)
    at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:319)
    at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:160)
    at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:147)
    at mod.learncraft.packer.Packager.<init>(Packager.java:24)
    at mod.learncraft.packer.Packager.main(Packager.java:38)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 9 more
`

This makes no sense to me, since it seems like the file name lines up with a file in the directory, but the process builder can't find it.

Ankit
  • 6,554
  • 6
  • 49
  • 71
Horotho
  • 23
  • 3
  • Make sure that the batch file exists within the context your are executing it. That is, (from you code), the batch file must exists within the current directory that your program is begin executed in. If it isn't, then you need to provide a path to the batch file and you may also need to change it's execution context to reflect this location – MadProgrammer Jul 31 '13 at 23:45
  • The code will only run if one of the file names matches "recompile.bat", and it does indeed run (as it gives me an error, and not just nothing). – Horotho Jul 31 '13 at 23:55

1 Answers1

2

the problem is that, you are not running the program from "C:\Users\Aaron\Documents\MinecraftForge\forge\mcp" thats y CommandLine is not able to find the file specified by argument. so you should try passing the absolute path

or, modify your code a bit:

File file = new File("C:\\Users\\Aaron\\Documents\\MinecraftForge\\forge\\mcp");
    for(File s : file.listFiles())
    {
        if(s.getName().equals("recompile.bat"))
        {
            DefaultExecutor executor = new DefaultExecutor();
            executor.setWorkingDirectory(file);
            CommandLine commandLine = new CommandLine(s.getAbsolutePath());

            try

        {
            executor.execute(commandLine);
        } catch (ExecuteException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
Ankit
  • 6,554
  • 6
  • 49
  • 71
  • Sweet, it works, thanks! So if I'm running from a different directory, I need the absolute path? – Horotho Jul 31 '13 at 23:59
  • @Horotho not necessary, even relative would work, but then you should adjust the path to actually be relative to directory from where the code is running, you might need to go back and forth a few directories like ("../../maindir/") – Ankit Aug 01 '13 at 00:04
  • Seems redundant to have to specify both the working directory of the batch file as well as the absolute path to that batch file, but hey ho... – Matthew Wise Sep 10 '14 at 14:36