[英]Running a script in terminal from java
I am running a script in a java program using: 我正在使用以下程序在Java程序中运行脚本:
Runtime.getRuntime().exec()
I am able to open the terminal application using this. 我可以使用此打开终端应用程序 。
If I give command to run the script. 如果我给出命令来运行脚本。 It's happening but I am not able to get the logs in the terminal.
它正在发生,但是我无法在终端中获取日志。 I am using MAC.
我正在使用MAC。 I want to get the logs in the terminal.
我想在终端中获取日志。
You can use a Process variable to get what return from that command, and use method such as: getInputStream(), getOutputStream(), getErrorStream(). 您可以使用Process变量获取该命令的返回值,并使用诸如getInputStream(),getOutputStream(),getErrorStream()之类的方法。 Example:
例:
Process p = null;
try {
p = Runtime.getRuntime().exec(....your stuff here)
p.getOutputStream().close(); // close stdin of child
InputStream processStdOutput = p.getInputStream();
Reader r = new InputStreamReader(processStdOutput);
BufferedReader br = new BufferedReader(r);
String line;
while ((line = br.readLine()) != null) {
//System.out.println(line); // the output is here
}
p.waitFor();
}
catch (InterruptedException e) {
...
}
catch (IOException e){
...
}
finally{
if (p != null)
p.destroy();
}
The Process
object returned by the method call above has an getInputStream()
method (as well as ones for the error and output streams). 上面的方法调用返回的
Process
对象具有getInputStream()
方法(以及用于错误和输出流的方法)。 You have to read from those if you want to grap the inputs and outputs of your script. 如果要掌握脚本的输入和输出,则必须阅读这些内容。
For reference: http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html 供参考: http : //docs.oracle.com/javase/7/docs/api/java/lang/Process.html
in terminal, using > to output the log to file. 在终端中,使用>将日志输出到文件。 For example: ls / > rootfolder.txt
例如:ls /> rootfolder.txt
Using that way, you can output the log to file and then read the log from the file. 使用这种方法,您可以将日志输出到文件,然后从文件中读取日志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.