Christopher Loen
Christopher Loen

Reputation: 187

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

Upvotes: 0

Views: 2393

Answers (1)

davidxxx
davidxxx

Reputation: 131326

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");
    pb.directory(new File("foo"));
    final Process process = pb.start();
    // then you read the flow with process.getInputStream() for example

Upvotes: 2

Related Questions