The Runtime.exec methods may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock. 然后我测试了下, 结果出现错误 java.io.IOException: CreateProcess: /rsa_crypt.pl -d '326234072' error=2 at java.lang.Win32Process.create(Native Method) at java.lang.Win32Process.<init>(Win32Process.java:66) at java.lang.Runtime.execInternal(Native Method) at java.lang.Runtime.exec(Runtime.java:566) at java.lang.Runtime.exec(Runtime.java:428) at java.lang.Runtime.exec(Runtime.java:364) at java.lang.Runtime.exec(Runtime.java:326)是不是说在windows里不能调用啊?
Process proc = Runtime.getRuntime().exec(cmd);
try {
proc.waitFor();
} catch (InterruptedException e) {}
int code = proc.exitValue();
such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft
Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its
standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through
three streams (Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()).
The parent process uses these streams to feed input to and get output from the subprocess.
Because some native platforms only provide limited buffer size for standard input and output
streams, failure to promptly write the input stream or read the output stream of the subprocess
may cause the subprocess to block, and even deadlock. 然后我测试了下,
结果出现错误
java.io.IOException: CreateProcess: /rsa_crypt.pl -d '326234072' error=2
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Win32Process.java:66)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:566)
at java.lang.Runtime.exec(Runtime.java:428)
at java.lang.Runtime.exec(Runtime.java:364)
at java.lang.Runtime.exec(Runtime.java:326)是不是说在windows里不能调用啊?
String comm = "perl /var/www/extsuite/extman/tools/userctl.pl--mod=add -username="+uname +"-password="+pass;
runLinuxCmd(String cmd) //本类调用public String runLinuxCmd(String cmd) {
BufferedReader bf = null;
try {
Process process = Runtime.getRuntime().exec(cmd);
bf = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
String resutl="";
while ((line = bf.readLine()) != null) {
resutl =resutl+ line.trim()+"\n";
}
System.out.println("---------------try result------------------"+resutl);
return resutl;
} catch (java.io.IOException e) {
e.printStackTrace();
System.out.println("------------error--------------");
return null;
} finally {
if (bf != null) {
try {
bf.close();
bf = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
单独在shell环境下执行perl命令是没有问题,能够插入数据,但是java调用能够执行不报错, 但是没有返回值,我在网上看到有朋友说过要把perl的命令写进shell脚本去 , 再用java调用 shell命令 , 我还没有学过lniux不知道怎么写 , 写这个脚本的时候需要接收外部传过来的username 和 password参数给perl命令 ,那个哥哥姐姐做过 能跟我讲一下么 如果可以的话能发到我邮箱吗[email protected] 感激不尽如果我上面没有表述明白 下面 简单的表述一下的我问题: 我想在java中调用perl命令向数据库里面插入数据。 perl命令是: perl /var/www/extsuite/extman/tools/userctl.pl--mod=add -username="+uname +"-password="+pass;