22 | * Queries the operating system environment. 23 | *
24 | */ 25 | package org.apache.commons.exec.environment; 26 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package org.apache.commons.exec.launcher; 21 | 22 | import java.io.File; 23 | import java.io.IOException; 24 | import java.nio.file.Path; 25 | import java.util.Map; 26 | 27 | import org.apache.commons.exec.CommandLine; 28 | 29 | /** 30 | * Abstracts platform-dependent implementations. 31 | */ 32 | public interface CommandLauncher { 33 | 34 | /** 35 | * Executes the given command in a new process. 36 | * 37 | * @param commandLine The command to execute. 38 | * @param env The environment for the new process. If null, the environment of the current process is used. 39 | * @return the newly created process. 40 | * @throws IOException if attempting to run a command in a specific directory. 41 | */ 42 | Process exec(CommandLine commandLine, Map72 | * Note that this method relies on the conventions of the OS, it will return false results if the application you are running doesn't 73 | * 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), 74 | * but this signals a failure on OpenVMS. So if you execute a new Java VM on OpenVMS, you cannot trust this method. 75 | *
76 | * 77 | * @param exitValue the exit value (return code) to be checked. 78 | * @return {@code true} if {@code exitValue} signals a failure. 79 | */ 80 | boolean isFailure(int exitValue); 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/exec/launcher/CommandLauncherFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package org.apache.commons.exec.launcher; 21 | 22 | import org.apache.commons.exec.OS; 23 | 24 | /** 25 | * Builds a command launcher for the OS and JVM we are running under. 26 | */ 27 | public final class CommandLauncherFactory { 28 | 29 | /** 30 | * Factory method to create an appropriate launcher. 31 | * 32 | * @return the command launcher. 33 | */ 34 | public static CommandLauncher createVMLauncher() { 35 | // Try using a JDK 1.3 launcher 36 | return OS.isFamilyOpenVms() ? new VmsCommandLauncher() : new Java13CommandLauncher(); 37 | } 38 | 39 | private CommandLauncherFactory() { 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/exec/launcher/CommandLauncherImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package org.apache.commons.exec.launcher; 21 | 22 | import java.io.File; 23 | import java.io.IOException; 24 | import java.util.Map; 25 | 26 | import org.apache.commons.exec.CommandLine; 27 | import org.apache.commons.exec.environment.EnvironmentUtils; 28 | 29 | /** 30 | * A command launcher for a particular JVM/OS platform. This class is a general purpose command launcher which can only launch commands in the current working 31 | * directory. 32 | */ 33 | public abstract class CommandLauncherImpl implements CommandLauncher { 34 | 35 | /** 36 | * Constructs a new instance. 37 | */ 38 | public CommandLauncherImpl() { 39 | // empty 40 | } 41 | 42 | @Override 43 | public Process exec(final CommandLine cmd, final Map31 | * Unlike Windows NT and friends, OS/2's cd doesn't support the /d switch to change drives and directories in one go. 32 | *
33 | *34 | * Note that this class is currently unused because the Java13CommandLauncher is used for 0S/2. 35 | *
36 | */ 37 | public class OS2CommandLauncher extends CommandLauncherProxy { 38 | 39 | /** 40 | * Constructs a new instance. 41 | * 42 | * @param launcher the command launcher to use. 43 | */ 44 | public OS2CommandLauncher(final CommandLauncher launcher) { 45 | super(launcher); 46 | } 47 | 48 | /** 49 | * Launches the given command in a new process, in the given working directory. 50 | * 51 | * @param cmd the command line to execute as an array of strings. 52 | * @param env the environment to set as an array of strings. 53 | * @param workingDir working directory where the command should run. 54 | * @throws IOException forwarded from the exec method of the command launcher. 55 | */ 56 | @Override 57 | public Process exec(final CommandLine cmd, final Map22 | * Operating system dependent command launchers are abstracted by the interface {@link org.apache.commons.exec.launcher.CommandLauncher}. 23 | *
24 | */ 25 | package org.apache.commons.exec.launcher; 26 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/exec/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | /** 21 | *22 | * The main Apache Commons Exec package. 23 | *
24 | *25 | * The main classes are {@link org.apache.commons.exec.CommandLine} and {@link org.apache.commons.exec.DefaultExecutor}. 26 | *
27 | */ 28 | package org.apache.commons.exec; 29 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/exec/util/DebugUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package org.apache.commons.exec.util; 21 | 22 | /** 23 | * Provides debugging support. 24 | */ 25 | public class DebugUtils { 26 | 27 | /** 28 | * System property to determine how to handle exceptions. When set to "false" we rethrow the otherwise silently catched exceptions found in the original 29 | * code. The default value is "true" 30 | */ 31 | public static final String COMMONS_EXEC_LENIENT = "org.apache.commons.exec.lenient"; 32 | 33 | /** 34 | * System property to determine how to dump an exception. When set to "true" we print any exception to stderr. The default value is "false" 35 | */ 36 | public static final String COMMONS_EXEC_DEBUG = "org.apache.commons.exec.debug"; 37 | 38 | /** 39 | * Handles an exception based on the system properties. 40 | * 41 | * @param msg message describing the problem. 42 | * @param e an exception being handled. 43 | */ 44 | public static void handleException(final String msg, final Exception e) { 45 | if (isDebugEnabled()) { 46 | System.err.println(msg); 47 | e.printStackTrace(); 48 | } 49 | if (!isLenientEnabled()) { 50 | if (e instanceof RuntimeException) { 51 | throw (RuntimeException) e; 52 | } 53 | throw new RuntimeException(e); 54 | } 55 | } 56 | 57 | /** 58 | * Determines if debugging is enabled based on the system property "COMMONS_EXEC_DEBUG". 59 | * 60 | * @return true if debug mode is enabled. 61 | */ 62 | public static boolean isDebugEnabled() { 63 | final String debug = System.getProperty(COMMONS_EXEC_DEBUG, Boolean.FALSE.toString()); 64 | return Boolean.TRUE.toString().equalsIgnoreCase(debug); 65 | } 66 | 67 | /** 68 | * Determines if lenient mode is enabled. 69 | * 70 | * @return true if lenient mode is enabled. 71 | */ 72 | public static boolean isLenientEnabled() { 73 | final String lenient = System.getProperty(COMMONS_EXEC_LENIENT, Boolean.TRUE.toString()); 74 | return Boolean.TRUE.toString().equalsIgnoreCase(lenient); 75 | } 76 | 77 | /** 78 | * Constructs a new instance. 79 | * 80 | * @deprecated Will be private in the next major version. 81 | */ 82 | @Deprecated 83 | public DebugUtils() { 84 | // empty 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/exec/util/MapUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * https://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package org.apache.commons.exec.util; 21 | 22 | import java.util.HashMap; 23 | import java.util.Map; 24 | import java.util.Objects; 25 | 26 | /** 27 | * Helper classes to manipulate maps to pass substition map to the CommandLine. This class is not part of the public API and could change without warning. 28 | */ 29 | public class MapUtils { 30 | /** 31 | * Clones a map. 32 | * 33 | * @param source the Map to clone. 34 | * @param22 | * Gathers utilities. 23 | *
24 | */ 25 | package org.apache.commons.exec.util; 26 | -------------------------------------------------------------------------------- /src/main/javadoc/overview.html: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 |The code was ported from Apache Ant and extensively 29 | tested on various platforms. So there is no reason not to use it and it is very likely 30 | better than any home-grown library.
31 |It is recommended to use CommandLine.addArgument() instead of CommandLine.parse(). Using 37 | CommandLine.parse() the implementation tries to figure out the correct quoting using your 38 | arguments and file names containing spaces. With CommandLine.addArgument() you can 39 | enable/disable quoting depending on your requirements. Having said that this is the 40 | recommended approach using Ant anyway.
41 |This functionality is largely depend on the operating system - on Unix it works 47 | mostly and under Windows not at all (see 48 | Bug 4770092). In terms of stability and cross-platform support try to start your applications directly and 49 | avoid various wrapper scripts.
50 |Well - one out of 55 regression tests fails. The 56 | EnvironmentUtilTest.testGetProcEnvironment() test fails because it detects no environment 57 | variables for the current process but there must be one since we require JAVA_HOME to be 58 | set. Not sure if this is a plain bug in java-gcj-4.2.1 or requires a work around in 59 | commons-exec
60 |Assuming that you have an environment not listed on the test 67 | matrix and want to make sure that everything works fine you can run easily run the 68 | regression tests. Make a SVN checkout and run 'ant test-distribution' to create the test 69 | distribution in './target'. On a production box downloading the ready-to-run test 70 | distribution might be even more handy ( 71 | https://people.apache.org/~sgoeschl/download/commons-exec/). Unpack the 'zip' or 72 | 'tar.gz' file and start the tests. Independent from the result we very much appreciate 73 | your feedback ... :-)
74 |Please check if the shell scripts under "./src/test/script" are executable - assuming 78 | that they are not executable the "testExecute*" and "testExecuteAsync*" test will 79 | fail. We try very hard to keep the executable bit but they have somehow the tendency 80 | to be lost ... 81 |
82 |30 | For information about reporting or asking questions about security, please see 31 | Apache Commons Security. 32 |
33 |This page lists all security vulnerabilities fixed in released versions of this component. 34 |
35 |Please note that binary patches are never provided. If you need to apply a source code patch, use the building instructions for the component version 36 | that you are using. 37 |
38 |39 | If you need help on building this component or other help on following the instructions to mitigate the known vulnerabilities listed here, please send 40 | your questions to the public 41 | user mailing list. 42 |
43 |If you have encountered an unlisted security vulnerability or other unexpected behavior that has security impact, or if the descriptions here are 44 | incomplete, please report them privately to the Apache Security Team. Thank you. 45 |
46 |None.
49 |