0

I want to call shell script on windows environment using java code. I am trying to execute code below to run my test script(not actual script):

Java Code:

public static void main (String args[]) {
        Runtime r = Runtime.getRuntime();
        try {
            Process p = r.exec("C:\\cygwin\\bin\\bash -c '/cygdrive/d/scripts/test.sh'");
            InputStream in = p.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            System.out.println("OUT:");
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }

            in = p.getErrorStream();
            br = new BufferedReader(new InputStreamReader(in));
            System.out.println("ERR:");
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }

            p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

test.sh

#!/bin/bash

rm -rf ./test

But I am getting these error and script in not able to remove the directory.

ERR: /cygdrive/d/ereader/scripts/test.sh: line 2: $'\r': command not found /cygdrive/d/ereader/scripts/test.sh: line 3: rm: command not found

Another thing, when I run the script from the cygwin terminal it works fine. I checked the path variable they all are fine. But I try to execute same script through java code it gives error..

Now how to tell java program where to refer for rm commands?

3 Answers 3

3

The giveaway is the '\r' error.

Windows and Unix (which includes Mac and Linux) use different representations of a new line. Windows uses '\r\n' while Unix simply uses '\n'. Most programming editors account for this and only insert a '\n' so they work with Unix tools.

I would suggest retyping your shell script in another editor like Notepad++ (which only inserts '\n'), or make sure your current editor is set to use Unix newlines. You have to retype it or the bad '\r\n' sequences will get copied over. You might have some luck in doing a replace-all but that always acts flaky for me.

Sign up to request clarification or add additional context in comments.

1 Comment

/r is removed but what about main rm command error... how can i Remove it ?
1

probably you will need to set the PATH variable first thing in the test.sh script. Make sure rm is in the PATH you set

4 Comments

path variable in already set... #echo $PATH /usr/local/bin:/usr/bin:/cygdrive/c/springsource/springsource/maven-2.2.1.RELEAS E/bin:/cygdrive/c/Program Files/TortoiseSVN/bin
this output is from an echo $PATH in test.sh?
no this output is from the cygwin terminal echo $PATH... also the script when run from cygwin terminal works fine.. but when run from java program gives error.. there is something missing in java program only
@dhroove how about setting the PATH in test.sh and see if the problem goes ways?
1

Not really answer. You could try following to narrow down the problem

  1. Use ProcessBuilder instead of Process. It is much more friendly to handle arguments.
  2. Set absolute path to rm (/bin/rm ) in the script
  3. remove using absolute path to directory or remove after verifying you are in the correct directory..

The bash prompt you have is result of cygwin.bat calling bash with --login. It will have path variables and other usesul stuff sources. bash -c does not do it.

  1. Try launching bash.exe with bash -l -c <command> : This sources the bash profile.

5 Comments

i don't think so replacing rm by /bin/rm is a good idea.. I have very long scripts make by unix administrators... There must be some other way out...
and moreover i have written... script in running fine when it is triggered from cygwin window... so there is something missing in java code..
i tried launching bash shell with -l-c command... I am getting following error ERR: /usr/bin/bash: --: invalid option Usage: /usr/bin/bash [GNU long option] [option] ... /usr/bin/bash [GNU long option] [option] script-file ... GNU long options: --debug --debugger --dump-po-strings --dump-strings --help --init-file --login --noediting --noprofile --norc --posix --protected --rcfile --restricted --verbose --version Shell options: -irsD or -c command or -O shopt_option (invocation only) -abefhkmnptuvxBCHP or -o option
sorry of the bad formatting of above comment just.. copy and paste it in a notepad.. than u will able to get better idea.
@dhroove: there was a space between -l and -c ( you could try -lc though)

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.