Java code to execute a .sh file

后端 未结 5 1001
暖寄归人
暖寄归人 2020-12-22 05:34

I have a .sh file stored in some Linux system. The full path of the file is:

/comviva/CPP/Kokila/TransactionHandler/scripts/stopTH.sh
         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-22 05:46

    I did a quick test here, the following works assuming you have /bin/bash on your machine:

    my /tmp/test.sh:

    #!/bin/bash
    echo `ls`
    

    my java code:

    try {
        InputStream is = Runtime.getRuntime().exec("/bin/bash /tmp/test.sh").getInputStream();
        int i = is.read();
        while(i > 0) {
            System.out.print((char)i);
            i = is.read();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    output: all files in current directory

    edit: i kinda overlooked that "execute from windows" comment. I don't know what you mean with that.

提交回复
热议问题