Christopher Loen Christopher Loen - 3 months ago 4x
Bash Question

Running a shell command in a specific directory in java

I'm currently developing a little software in Java and I'm facing a problem I'm not able to solve. In a few words, I am on ArchLinux and I need to run "makepkg" in a specific directory. Of course I tried with

Runtime.getRuntime().exec("cd foo && makepkg");

But I discovered that I cannot cd in directories. Someone has an idea on how to do this? Thanks anyway


A process executor is not a shell. It's done for launching a process. A thing that can help you is to launch the process from a specified directory.

You can create a ProcessBuilder instance and set the working directory. It is my way of doing.

    ProcessBuilder pb = new ProcessBuilder("makepkg"); File("foo"));
    final Process process = pb.start();
    // then you read the flow with process.getInputStream() for example