CommandLauncher.java

  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. package org.apache.commons.exec.launcher;

  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.nio.file.Path;
  21. import java.util.Map;

  22. import org.apache.commons.exec.CommandLine;

  23. /**
  24.  * Abstracts platform-dependent implementations.
  25.  */
  26. public interface CommandLauncher {

  27.     /**
  28.      * Executes the given command in a new process.
  29.      *
  30.      * @param commandLine The command to execute.
  31.      * @param env         The environment for the new process. If null, the environment of the current process is used.
  32.      * @return the newly created process.
  33.      * @throws IOException if attempting to run a command in a specific directory.
  34.      */
  35.     Process exec(final CommandLine commandLine, final Map<String, String> env) throws IOException;

  36.     /**
  37.      * Executes the given command in a new process, in the given working directory.
  38.      *
  39.      * @param commandLine      The command to execute.
  40.      * @param env              The environment for the new process. If null, the environment of the current process is used.
  41.      * @param workingDirectory The directory to start the command in. If null, the current directory is used.
  42.      * @return the newly created process.
  43.      * @throws IOException if trying to change directory.
  44.      */
  45.     Process exec(final CommandLine commandLine, final Map<String, String> env, final File workingDirectory) throws IOException;

  46.     /**
  47.      * Executes the given command in a new process, in the given working directory.
  48.      *
  49.      * @param commandLine      The command to execute.
  50.      * @param env              The environment for the new process. If null, the environment of the current process is used.
  51.      * @param workingDirectory The directory to start the command in. If null, the current directory is used.
  52.      * @return the newly created process.
  53.      * @throws IOException if trying to change directory.
  54.      * @since 1.5.0
  55.      */
  56.     default Process exec(final CommandLine commandLine, final Map<String, String> env, final Path workingDirectory) throws IOException {
  57.         return exec(commandLine, env, workingDirectory != null ? workingDirectory.toFile() : null);
  58.     }

  59.     /**
  60.      * Tests whether {@code exitValue} signals a failure on the current system (OS specific).
  61.      * <p>
  62.      * <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
  63.      * 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),
  64.      * but this signals a failure on OpenVMS. So if you execute a new Java VM on OpenVMS, you cannot trust this method.
  65.      * </p>
  66.      *
  67.      * @param exitValue the exit value (return code) to be checked.
  68.      * @return {@code true} if {@code exitValue} signals a failure.
  69.      */
  70.     boolean isFailure(final int exitValue);
  71. }