| Start |
|---|
| Pitfall: Runtime.exec, Process and ProcessBuilder don't understand shell syntax The First of all, here is an example of using Spaces in pathnamesSuppose that we generalize the example above so that we can create an arbitrary directory: This will typically work, but it will fail if will be split into: and this will cause the "mkdir" command to fail because it expects one argument, not two. Faced with this, some programmers try to add quotes around the pathname. This doesn't work either: will be split into: The extra double-quote characters that were added in attempt to "quote" the spaces are treated like any other non-whitespace characters. Indeed, anything we do quote or escape the spaces is going to fail. The way to deal with this particular problems is to use the This will work if Redirection, pipelines and other shell syntaxSuppose that we want to redirect an external command's input or output, or run a pipeline. For example: or (The first example lists the names of all Java files in the file system, and the second one prints the These are not going to work as expected. In the first case, the "find" command will be run with "2>/dev/null" as a command argument. It will not be interpreted as a redirection. In the second example, the pipe character ("|") and the works following it will be given to the "find" command. The problem here is that the In a few cases (for example, simple redirection) you can easily achieve the desired effect using or But note that in the second example, we needed to escape the wildcard character ("*") because we want the wildcard to be interpreted by "find" rather than the shell. Shell builtin commands don't workSuppose the following examples won't work on a system with a UNIX-like shell: or There are a couple of reasons why this won't work: This reasoning applies to all shell builtin commands. 1 - You can use 2 - This is a bit rough and ready ... but once again, the failings of this approach are not relevant to the example. |
| Simple example (Java version < 1.5) This example will call the windows calculator. It's important to notice that the exit code will vary accordingly to the program/script that is being called. |
| ch.vorburger.exec Launching external processes from Java using the raw java.lang.ProcessBuilder API directly can be a little cumbersome. The Apache Commons Exec library (opens new window) makes it a little easier. The ch.vorburger.exec library (opens new window) further extends upon Commons Exec to make it truly convenient: |
| Using the ProcessBuilder class The ProcessBuilder class makes it easy to send a command through the command line. All it requires is a List of Strings that make up the commands to be entered. You simply call the start() method on your ProcessBuilder instance to execute the command. If you have a program called Add.exe which takes two arguments and adds them, the code would look something like this: Some things to keep in mind:
|
| Blocking vs. Non-Blocking Calls In general when making a call to the command line, the program will send the command and then continue its execution. However you may want to wait for the called program to finish before continuing your own execution (ex. The called program will write data to a file and your program needs that to access that data.) This can easily be done by calling the Usage example: RemarksNotice that the API recommends that, as of version 1.5, the preferred way to create a Process is using Another important remark is that the exit value produced by |
Read Article
https://develop.horje.com/learn/1434/reference