Logical
Logical

Reputation: 31

Run Windows .exe in Java with Parameters

I have been fighting with Java trying to run an exe command in windows, I can launch notepad, but any time I try passing arguments I get nothing. I have searched over the last several days with tons of helpful ways to launch exe files, but I simply cannot figure out why none will run with arguments. Here is one of the examples I have tried today, using ProcessBuilder for starters.

public static void main(String[] args) throws Exception{
     ProcessBuilder p = new ProcessBuilder("C:/my/path/phantomjs.exe", "script.js", "site.com", ">", "output.txt");
     p.start();
}

Upvotes: 0

Views: 1983

Answers (1)

VGR
VGR

Reputation: 44413

Redirection (the > character) is not actually part of the command. It's parsed by cmd.exe (or the Unix/Linux shell).

You want this:

ProcessBuilder p = new ProcessBuilder("C:/my/path/phantomjs.exe", "script.js", "site.com");
p.redirectOutput(new File("output.txt"));
p.start();

You probably should look at the summaries of all the ProcessBuilder methods available to you.

Upvotes: 3

Related Questions