简体   繁体   English

从Java代码运行.sh

[英]Running .sh from Java code

I tried to run .sh file from my java code. 我试图从我的Java代码运行.sh文件。 I've seen a similar questions ( How to run Unix shell script from Java code? ) and tried the answers that was written there. 我看到了类似的问题( 如何从Java代码运行Unix shell脚本? ),并尝试了在那里写的答案。 However, I expect the process exit value will be 0 (terminate normally), while in fact I got 127. Here is my code: 但是,我希望进程出口值将为0(通常终止),而实际上我得到了127。这是我的代码:

public int performATRs() throws IOException{

    String[] command = {"sh", "/ThematicAnalysis/flexiTerm/FlexiTerm.sh"};
    Process process = Runtime.getRuntime().exec(command);
    InputStream inputStream = process.getInputStream();
    InputStreamReader streamReader = new InputStreamReader(inputStream);
    BufferedReader bufReader = new BufferedReader(streamReader);
    try{
        process.waitFor();
        System.out.println("Waiting...");
        System.out.println("Returned Value :" + process.exitValue());
    }catch(InterruptedException e){
        System.out.println(e.getMessage());
    }
    while(bufReader.ready())
        System.out.println(bufReader.readLine());
    return process.exitValue();
} 

I tried to run FlexiTerm.sh from my java code. 我试图从我的Java代码运行FlexiTerm.sh。 It works when I run it from the terminal though. 当我从终端运行它时,它可以工作。 Thank you very much for your help :) 非常感谢您的帮助 :)

127 means it wasn't able to execute /ThematicAnalysis/flexiTerm/FlexiTerm.sh , you should double check that the path to that script is correct. 127表示它无法执行/ThematicAnalysis/flexiTerm/FlexiTerm.sh ,您应仔细检查该脚本的路径是否正确。 I suspect it isn't, given that a Unix-style system wouldn't normally have a top-level directory named "ThematicAnalysis". 我怀疑不是这样,因为Unix风格的系统通常不会有一个名为“ ThematicAnalysis”的顶级目录。 If you're looking for that script at a location relative to your Java program's working directory then you should remove the leading forward slash. 如果要在相对于Java程序的工作目录的某个位置查找该脚本,则应删除前导斜杠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM