Question

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.

Was it helpful?

Solution

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();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top