1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.exec.launcher;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.nio.file.Path;
23 import java.util.Map;
24
25 import org.apache.commons.exec.CommandLine;
26
27 /**
28 * Abstracts platform-dependent implementations.
29 */
30 public interface CommandLauncher {
31
32 /**
33 * Executes the given command in a new process.
34 *
35 * @param commandLine The command to execute.
36 * @param env The environment for the new process. If null, the environment of the current process is used.
37 * @return the newly created process.
38 * @throws IOException if attempting to run a command in a specific directory.
39 */
40 Process exec(final CommandLine commandLine, final Map<String, String> env) throws IOException;
41
42 /**
43 * Executes the given command in a new process, in the given working directory.
44 *
45 * @param commandLine The command to execute.
46 * @param env The environment for the new process. If null, the environment of the current process is used.
47 * @param workingDirectory The directory to start the command in. If null, the current directory is used.
48 * @return the newly created process.
49 * @throws IOException if trying to change directory.
50 */
51 Process exec(final CommandLine commandLine, final Map<String, String> env, final File workingDirectory) throws IOException;
52
53 /**
54 * Executes the given command in a new process, in the given working directory.
55 *
56 * @param commandLine The command to execute.
57 * @param env The environment for the new process. If null, the environment of the current process is used.
58 * @param workingDirectory The directory to start the command in. If null, the current directory is used.
59 * @return the newly created process.
60 * @throws IOException if trying to change directory.
61 * @since 1.5.0
62 */
63 default Process exec(final CommandLine commandLine, final Map<String, String> env, final Path workingDirectory) throws IOException {
64 return exec(commandLine, env, workingDirectory != null ? workingDirectory.toFile() : null);
65 }
66
67 /**
68 * Tests whether {@code exitValue} signals a failure on the current system (OS specific).
69 * <p>
70 * <strong>Note</strong> that this method relies on the conventions of the OS, it will return false results if the application you are running doesn't
71 * follow these conventions. One notable exception is the Java VM provided by HP for OpenVMS - it will return 0 if successful (like on any other platform),
72 * but this signals a failure on OpenVMS. So if you execute a new Java VM on OpenVMS, you cannot trust this method.
73 * </p>
74 *
75 * @param exitValue the exit value (return code) to be checked.
76 * @return {@code true} if {@code exitValue} signals a failure.
77 */
78 boolean isFailure(final int exitValue);
79 }