[英]Execute shell command with `java.lang.ProcessBuilder`
I wrote the following little script in CFML that executes a shell command, my problem is with the output, as long as the output is a single line is ok, if is multi-line I get only the first line, I tried to do a while loop on the script (commented on the code) but doesn't work and Java throws a memory error java.lang.OutOfMemoryError: Java heap space
. 我在CFML中编写了以下小脚本来执行shell命令,我的问题是输出,只要输出是单行就可以了,如果是多行,我只会得到第一行,我试图做一个虽然在脚本上执行了while循环(在代码中添加了注释),但是它不起作用,并且Java引发了内存错误
java.lang.OutOfMemoryError: Java heap space
。 What can I do? 我能做什么?
<cfscript>
str = ":>exec uname";
exec_init=str.split(":>exec ");
exec=exec_init[2].split(" ");
p = createObject("java","java.lang.ProcessBuilder").init(exec).start();
i = createObject("java","java.io.InputStreamReader").init(p.getInputStream());
br = createObject("java","java.io.BufferedReader").init(i);
line=br.readLine();
//while (isDefined("line")) {
//writeoutput(line);
//}
br.close();
i.close();
</cfscript>
<cfdump var="#line#">
Output: 输出:
Linux
If I issue a command like 如果我发出这样的命令
ls
with multi-line output I get eg 与多行输出我得到例如
README.TXT
instead: 代替:
README.TXT VERSION.txt _-Railo-Getting-Started-_.html bin etc jre lib license-eplv10-aslv20.html modules notice.html resources start start.d start.ini start.jar stop webapps
Found the problem, I needed to add again line = br.readLine();
发现问题后,我需要再次添加
line = br.readLine();
in my loop. 在我的循环中。 Working code:
工作代码:
<cfscript>
str = ":>exec ls -al";
exec_init=str.split(":>exec ");
exec=exec_init[2].split(" ");
p = createObject("java","java.lang.ProcessBuilder").init(exec).start();
i = createObject("java","java.io.InputStreamReader").init(p.getInputStream());
br = createObject("java","java.io.BufferedReader").init(i);
line = br.readLine();
while (isDefined("line")) {
writeoutput(line);
line = br.readLine();
}
br.close();
i.close();
</cfscript>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.