Java InputStream与作为输入的shell脚本一起使用Runtime.getRunTime().exec()。

huangapple go评论56阅读模式
英文:

Java InputStream with a shell script as input to Runtime.getRunTime().exec()

问题

我正在运行一个使用Jar文件的Java程序。在打包的Jar文件中,我有我的Shell脚本。我理解通过getResourcesAsStream访问该文件的最佳方式,因为我无法通过常规目录结构访问它。所以这是我正在使用的方法:

InputStream input = this.getClass().getResourceAsStream("/script/sample_script.sh");

然而,我唯一知道的执行方式是逐行通过循环和BufferedReader来执行脚本。像“if”或“while”等语句会导致错误。是否有一种方法可以使用流来实现所需的结果?

以下是我的当前代码:

InputStream input = this.getClass().getResourceAsStream("/script/sample_script.sh");
BufferedReader brCmd = new BufferedReader(new InputStreamReader(input));
String cmd;
while ((cmd = brCmd.readLine()) != null){
     proc = Runtime.getRuntime().exec(cmd);
     proc.waitFor();
}

我尝试过逐行将整个文件连接成字符串并使用它,但似乎不起作用。如何让我的脚本文件使用InputStream运行?任何帮助将不胜感激。

英文:

I am running a java program using a Jar file. Inside this packaged jar file I have my shell script. I understand that the best way to access the file would be through getResourcesAsStream because I can't access it via regular directory structures. So this is what I'm using:

    InputStream input = this.getClass().getResourceAsStream("/script/sample_script.sh");

However, The only way I know how to execute this is if I go through the script line by line using a loop and bufferReader. I Statements like "If" or "While" loops get me an error. Is there a way to input the stream to achieve the desired results.

Here is my current code:

InputStream input = this.getClass().getResourceAsStream("/script/sample_script.sh");
BufferedReader brCmd = new BufferedReader(new InputStreamReader(input));
String cmd;
while ((cmd = brCmd.readLine()) != null){
     proc = Runtime.getRuntime().exec(cmd);
     proc.waitFor();
}

I've tried concatenating the whole file line by line to a string and using that but it doesn't seem to work. How can I get to make my script file run using inputStream? Any help would be appreciated

答案1

得分: 1

你可以通过InputStream读取脚本文件,并将其写入系统临时目录下的临时文件,然后以以下方式执行此文件:Runtime.getRuntime.exec("/bin/sh " + tempFilePath);

关于重新将脚本文件重写到另一个临时文件的原因,你说你通过直接运行jar文件来执行程序,因此脚本文件被打包到jar文件中,而Java不会解压缩jar文件到文件系统。为了让cmd.exe能够访问脚本文件,我们需要手动重写脚本文件。

顺便说一下,你可以从系统属性中获取系统临时目录路径,或者使用apache common.io:FileUtils.getTempDirectory()

英文:

You could read the script file through InputStream and write it to a temp file under the system temp directory, and execute this file in following way:Runtime.getRuntime.exec("/bin/sh " + tempFilePath);

About reason to rewrite the script file to another temp file, you said you are executing your program by run the jar file directly, thus the script file is packed into the jar file and java won't unpack the jar file to the file system. To let the script file be accessible to cmd.exe, we need to rewrite the script file manually.

By the way, you could get the system temp directory path from system properties, or using apache common.io: FileUtils.getTempDirectory().

huangapple
  • 本文由 发表于 2020年8月10日 17:41:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63337727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定