C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Arguments: We pass a variable number of arguments to command—it combines these strings into a command string.
Start: With start we invoke the command. On a Windows operating system, Notepad opens the specified file.
Java program that uses ProcessBuilder, start
import java.io.IOException;
import java.lang.ProcessBuilder;
public class Program {
public static void main(String[] args) throws IOException {
// Create ProcessBuilder.
ProcessBuilder p = new ProcessBuilder();
// Use command "notepad.exe" and open the file.
p.command("notepad.exe", "C:\\file.txt");
p.start();
}
}
Tip: To pass an argument to the exe, specify a second argument to command. This can be a target file location to open.
Java program that uses ProcessBuilder
import java.io.IOException;
import java.lang.ProcessBuilder;
public class Program {
public static void main(String[] args) throws IOException {
String folder = "C:\\Program Files\\Adobe\\Adobe Photoshop CC\\";
String exe = "photoshop.exe";
// Create and start Process with ProcessBuilder.
ProcessBuilder p = new ProcessBuilder();
p.command(folder + exe);
p.start();
}
}
File: The file must exist on the disk for this example to correctly work. We can separately create the file.
Output: The program runs 7za.exe with no arguments, so the EXE does nothing of importance. We can pass further arguments to Zza.
7-Zip Command-LineJava program that uses redirectOutput
import java.io.File;
import java.io.IOException;
public class Program {
public static void main(String[] args) throws IOException {
// Create ProcessBuilder and target 7-Zip executable.
ProcessBuilder b = new ProcessBuilder();
b.command("C:\\7za.exe");
// Redirect output to this file.
b.redirectOutput(new File("C:\\folder\\test.txt"));
b.start();
}
}
Contents: test.txt
7-Zip (A) 9.07 beta Copyright (c) 1999-2009 Igor Pavlov 2009-08-29
Usage: 7za <command> [<switches>...] <archive_name> [<file_names>...]
[<@listfiles...>]
Note: This program starts Internet Explorer and opens Wikipedia in it. This is not ideal for systems that use other browsers.
Note 2: Sometimes a specific browser is standardized upon in an organization. This command would work there.
Java program that uses ArrayList for command
import java.io.IOException;
import java.util.ArrayList;
public class Program {
public static void main(String[] args) throws IOException {
ProcessBuilder b = new ProcessBuilder();
// Create an ArrayList with two values.
// ... This starts a specific browser, which is not ideal.
ArrayList<String> values = new ArrayList<>();
values.add("C:\\Program Files\\Internet Explorer\\iexplore.exe");
values.add("http://en.wikipedia.org/");
// Pass List to command method.
b.command(values);
b.start();
}
}
Java program that uses destroyForcibly
import java.io.IOException;
import java.lang.ProcessBuilder;
public class Program {
public static void main(String[] args)
throws IOException, InterruptedException {
ProcessBuilder p = new ProcessBuilder();
p.command("notepad.exe");
// Start notepad.
Process notepad = p.start();
System.out.println("Start");
// Sleep for 5 seconds.
Thread.sleep(5000);
// Kill notepad.
notepad.destroyForcibly();
System.out.println("End");
}
}
Output
Start
End