0

I'm trying to run this script shell from java, but it's not working.

I get this error message:

Process exited with an error: 1 (Exit value: 1)

Can someone help?

String pwd = "blabla";

String s_key = "0000";

String path = "C:/Files/scripts"; 

CommandLine commandLine = CommandLine.parse("C:\\Program Files (x86)\\Git\\bin\\git.exe");

commandLine.addArgument("fileName.sh");

commandLine.addArgument(password);

commandLine.addArgument(s_key);

DefaultExecutor defaultExecutor = new DefaultExecutor();

ByteArrayOutputStream sdtout = new ByteArrayOutputStream();

ByteArrayOutputStream sdterr = new ByteArrayOutputStream();

PumpStreamHandler streamHandler = new PumpStreamHandler(sdtout, sdterr);

defaultExecutor.setStreamHandler(streamHandler);

defaultExecutor.execute(commandLine);

Here is the script

#!/bin/sh

pwd=$1
s_key=$2
....
echo $pwd

it works well with git bash

  $ ./fileName.sh blabla 0000
  nkfjWmiG7dDnYUmjr6VD0A==
3
  • Have you tried inspecting the Exception or the stderr/stdout of the executor? commons.apache.org/proper/commons-exec/apidocs/org/apache/… Commented Apr 18, 2017 at 16:31
  • you said "works well with git bash", in order to execute the shell you need to open the Git Bash console ? o you can execute the shell directly on the Windows cmd console ? Commented Apr 18, 2017 at 16:47
  • @reos yes i use git bash console, i've updated the question with console output. The script doesn't work on windows cmd console... thanks Commented Apr 19, 2017 at 7:52

2 Answers 2

1

There are some points to be aware of.

  1. If you want to run the git bash command you need to execute the git-bash.exe, on the cmd console you need to execute this command:
%windir%\system32\cmd.exe /c ""C:\Program Files\Git\git-bash.exe" --login -i -- D:\temp\test.sh param1"
  1. If you want to execute it from a java app it's the same, the command you need to execute is git-bash.exe not git.exe. This is an example of running a command from java. I'm not using the objects that you're using but the simple java objects. However you can adapt it to your code.
 public static void main(String[] args) throws IOException {
          String[] command = {"C:\\\\Program Files\\\\Git\\\\git-bash.exe",
                  "D:\\temp\\test.sh",
                  "param1"};
          ProcessBuilder processBuilder = new ProcessBuilder(command);
          processBuilder.redirectErrorStream(true);
          processBuilder.start();
      }
Sign up to request clarification or add additional context in comments.

1 Comment

the --login -i part might be important. So you should add it to your command. String[] command = {"C:\\\\Program Files\\\\Git\\\\git-bash.exe", "--login", "-i", "D:\\temp\\test.sh", "param1"};
0

There are a couple issues with your code:

  • You don't seem to be inspecting the stderr/stdout of the program, or inspecting the Exception that gets thrown.
  • Git.exe doesn't take shell scripts as the first argument. As @reos says, you probably need to invoke git-bash.exe rather than git.exe

2 Comments

thanks for your reply; i've updated the code with the stderr/stdout. now the error message is Process exited with an error: 1 (Exit value: 1). where should i put the file "fileName.sh" in the code?
Updated per reos' answer. Try invoking git-bash.exe rather than git.exe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.