0

I am trying to run a shell command - cat input.txt | shellscript.sh

as you can see the shell script requires input so i made an input txt.

This commands runs perfectly well in terminal. But I am not sure how it works in java.

so to make this work, I made another script called command.sh which just has this shell commmand - cat input.txt | shellscript.sh

and i placed them all in the same directories. When i run it, there are no errors but the commands inside the script does not seem to run.

      public class testing {

    public static void main(String[] args) throws IOException,InterruptedException {
    Process p = new ProcessBuilder("/bin/sh", "/Random Files/command.sh").start();



    }
}

Any idea how to get it working? or can I just call the command - cat input.txt | shellscript.sh somehow to make it work? Also can you please tell me how to get the output?

user2775042
  • 509
  • 2
  • 6
  • 21
  • http://stackoverflow.com/questions/18240944/cannot-launch-shell-script-with-arguments-using-java-processbuilder – Adil Shaikh Oct 27 '15 at 05:51

1 Answers1

0

Months ago I did this using the following code , for your reference , may be it will be helpful.

To understand the difference between the two methods have look Difference between ProcessBuilder and Runtime.exec() , also ProcessBuilder vs Runtime.exec()

public class ShellCommandsHandler {
    private static final  Logger log = Logger.getLogger(ShellCommandsHandler.class);


    public static void execute(String script){

        try {
            Process p = Runtime.getRuntime().exec(script);

            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line=null;

            while((line=input.readLine()) != null) {
                System.out.println(line);
            }

            int exitVal = p.waitFor();
            System.out.println("Exited with error code "+exitVal);
            log.debug(("Exited with error code "+exitVal));

        } catch(Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    public static void execute(String[] code){

        //String[] StringMklink = {"cmd.exe", "/c",  "mklink"+" "+"/j"+" "+"\"D:/ Games\""+" "+"\"D:/Testing\""};

        try{
            ProcessBuilder pb=new ProcessBuilder(code);
            pb.redirectErrorStream(true);
            Process process = pb.start();
            BufferedReader inStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            while(inStreamReader.readLine() != null){
                System.out.println(inStreamReader.readLine());
            }
        } catch (IOException e) {
            System.out.println(e.toString());
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    public static void main(String[] args){
        execute("cmd.exe /c xcopy \"D:\\Downloads\\Temp\\data\\*.*\" \"D:\\Downloads\\\" /E");

    }

}
Community
  • 1
  • 1
SSH
  • 1,609
  • 2
  • 22
  • 42