Category Archives: apache-commons-exec

Executing shell-script with parameters using apache.commons.exec in Windows

I am trying to run a script using apache-commons-exec which was implemented using the java approximation to run. This script is executed in the production server (Linux) but I need to test it in my localhost to see that everything works OK.

Here is my code to launch cygwin and this code is working in the cmd.exe but it does not work when I try to launch it using commons.exec:

    OutputStream outputStream = new ByteArrayOutputStream();
    DefaultExecutor exec = new DefaultExecutor();
    exec.setWatchdog(new ExecuteWatchdog(1000));
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    exec.setStreamHandler(streamHandler);
    CommandLine cmdLine = CommandLine.parse("C:\\cygwin64\\bin\\bash");
    cmdLine.addArgument("-c");
    cmdLine.addArgument("/cygdrive/c/dev/launch.sh");
    int exit = exec.execute(cmdLine);
    logger.warn("Job exit: " + exit);

It returns 1 and no output or log error:

org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)

Is there anything missing? How can I catch the output properly?

Execute GIT command using Apache Commons Exec

I am trying to execute the below command using apache Commons Exec but it is giving error.

command:

git --git-dir=$[git_repo_dir]$/.git log $[prev_release_last_git_commit_id]$..$[current_release_last_git_commit_id]$ --name-status --ignore-blank-lines --no-merges --oneline --pretty=tformat:"###[$]%s[$],[$]%ad[$],[$]%an<%ae>[$],[$]%H[$]" --date=iso -- ./calypso-config | tr "\t" ","

error: Caused by: org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1) at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404) at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166) at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:153)

running simple cmd.exe commands with apache commons exec

I want to run cmd.exe commands from java (for example md C:\blabla to create a new directory C:\blabla ) My code looks like this and it runs without any errors:

import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;

public class Test {

    public static void main(String[] args) throws ExecuteException, IOException {
        CommandLine cmdLine = new CommandLine("cmd.exe");
        cmdLine.addArgument("md");
        cmdLine.addArgument("C:\\blabla");
        DefaultExecutor executor = new DefaultExecutor();
        executor.execute(cmdLine);
    }
}

But if I go to C:\ there is no folder blabla as I would expect, since manually typing md C:\blabla in cmd.exe works fine. I also tried "C:\Windows\System32\cmd.exe" instead of "cmd.exe" but no use.

The output in the Console looks like this:

Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\Selphiron\workspace\Test>

Where is the mistake?