    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 *  contributor license agreements.  See the NOTICE file distributed with
    004 *  this work for additional information regarding copyright ownership.
    005 *  The ASF licenses this file to You under the Apache License, Version 2.0
    006 *  (the "License"); you may not use this file except in compliance with
    007 *  the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 */
    017
    018package org.apache.commons.exec.launcher;
    019
    020import java.io.File;
    021import java.io.IOException;
    022import java.util.Map;
    023
    024import org.apache.commons.exec.CommandLine;
    025
    026/**
    027 * A command launcher for Windows XP/2000/NT that uses 'cmd.exe' when launching commands in directories other than the current working directory.
    028 */
    029public class WinNTCommandLauncher extends CommandLauncherProxy {
    030
    031    /**
    032     * Constructs a new instance.
    033     *
    034     * @param launcher the command launcher to use.
    035     */
    036    public WinNTCommandLauncher(final CommandLauncher launcher) {
    037        super(launcher);
    038    }
    039
    040    /**
    041     * Launches the given command in a new process, in the given working directory.
    042     *
    043     * @param cmd        the command line to execute as an array of strings.
    044     * @param env        the environment to set as an array of strings.
    045     * @param workingDir working directory where the command should run.
    046     * @throws IOException forwarded from the exec method of the command launcher.
    047     */
    048    @Override
    049    public Process exec(final CommandLine cmd, final Map<String, String> env, final File workingDir) throws IOException {
    050        if (workingDir == null) {
    051            return exec(cmd, env);
    052        }
    053
    054        // Use cmd.exe to change to the specified directory before running the command.
    055        // @formatter:off
    056        return exec(new CommandLine("cmd")
    057                .addArgument("/c")
    058                .addArguments(cmd.toStrings()), env);
    059        // @formatter:on
    060    }
    061}