关于网友提出的“64位WIN SERVER 2008用JAVA调用批处理卡住了”问题疑问,本网通过在网上对“64位WIN SERVER 2008用JAVA调用批处理卡住了”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题:64位WIN SERVER 2008用JAVA调用批处理卡住了
描述: 我在本机测试正常。本机是32位WIN7,调用bat正常运行走下去,可是部署到64位WIN8服务器上时在调用BAT批处理的时候能够调用,但是走不下去,运行bat文件输出这步一直没走到。卡住了。求大神帮忙看下。
代码如下:
/**
* 运行bat文件
*
* @param batName
*/
public void runbat(String batName) {
try {
Process ps = Runtime.getRuntime().exec(batName);
InputStream in = ps.getInputStream();
int c;
while ((c = in.read()) != -1) {
System.out.print(in.toString());// 查看输出
}
in.close();
ps.waitFor();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 执行bat
* @param url
*/
public void downFileToRegistDCOM(String url){
DownFile d = new DownFile();
try {
System.out.println("======开始注册dcm文件=======");
d.runbat("D:\\dcm\\b.bat");
System.out.println("======完成注册dcm文件=======");
d.runbat("D:\\dcm\\c.bat");
System.out.println("======完成删除dcm文件=======");
} catch (Exception e) {
e.printStackTrace();
}
}
b.bat内容如下:
@echo off
cd /d E:\pcas\dcm4cheTest\dcm4cheTest\dcm4che-2.0.27\bin
dcmsnd DCM4CHEE@localhost :11112 D:\dcm\CT_1.dcm
ping -n 5 127.0.0.1
exit
解决方案1: 运行bat文件有问题,后来重新了,就正常了。代码如下:
/**
* 运行bat文件
*
* @param batName
*/
public void runbat(String batName) {
try {
Process ps = Runtime.getRuntime().exec(batName);
ps.getOutputStream().close();
StreamGobbler errorGobbler = new StreamGobbler(ps.getErrorStream(), "Error");
StreamGobbler outputGobbler = new StreamGobbler(ps.getInputStream(), "Output");
errorGobbler.start();
outputGobbler.start();
ps.waitFor();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("child thread done");
}
class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (type.equals("Error"))
System.out.println(line);
else
System.out.println(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
以上介绍了“64位WIN SERVER 2008用JAVA调用批处理卡住了”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/1212133.html