【发布时间】:2009-10-28 16:15:36
【问题描述】:
我正在尝试创建一个保持 netsh windows 命令行工具打开的线程,这样我就可以执行 netsh 命令而无需每次都打开它。
问题是,一旦我创建了线程,只有第一个命令调用有效……随后的调用似乎没有效果。
这是我的代码:
public class NetshThread implements Runnable{
private static Process netshProcess = null;
private static BufferedInputStream netshInStream = null;
private static BufferedOutputStream netshOutStream = null;
public BufferedReader inPipe = null;
public void run(){
startNetsh();
}
public void startNetsh(){
try {
netshProcess = Runtime.getRuntime().exec("netsh");
netshInStream = new BufferedInputStream(netshProcess.getInputStream());
netshOutStream = new BufferedOutputStream(netshProcess.getOutputStream());
inPipe = new BufferedReader(new InputStreamReader(netshInStream));
} catch (IOException e) {
e.printStackTrace();
}
}
public void executeCommand(String command){
System.out.println("Executing: " + command);
try {
String str = "";
netshOutStream.write(command.getBytes());
netshOutStream.close();
while ((str = inPipe.readLine()) != null) {
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void closeNetsh(){
executeCommand("exit");
}
public static void main(String[] args){
NetshThread nthread = new NetshThread();
nthread.run();
String command = "int ip set address " +
"\"Local Area Connection 6\" static .69.69.69 255.255.255.0";
nthread.executeCommand(command);
command = "int ip set address " +
"\"Local Area Connection 6\" static 69.69.69.69 255.255.255.0";
nthread.executeCommand(command);
System.out.println("*** DONE ***");
}
}
谢谢!!! =)
更新 1:
好的...我现在使用的是 PrintWriter...所以我想我不需要再刷新任何东西了,因为构造函数是:
new PrintWriter(netshOutStream, true); (就像闪亮先生告诉我的那样)......
假设我决定在第一个输出行可用时中断 while 循环...我也不工作...下一个命令不会执行...我的代码现在看起来像:
import java.io.*;
public class NetshThread implements Runnable{
private static Process netshProcess = null;
private static BufferedInputStream netshInStream = null;
private static BufferedOutputStream netshOutStream = null;
public BufferedReader inPipe = null;
private PrintWriter netshWriter = null;
public void run(){
startNetsh();
}
public void startNetsh(){
try {
netshProcess = Runtime.getRuntime().exec("netsh");
netshInStream = new BufferedInputStream(netshProcess.getInputStream());
netshOutStream = new BufferedOutputStream(netshProcess.getOutputStream());
netshWriter = new PrintWriter(netshOutStream, true);
inPipe = new BufferedReader(new InputStreamReader(netshInStream));
} catch (IOException e) {
e.printStackTrace();
}
}
public void executeCommand(String command){
System.out.println("Executing: " + command);
try {
String str = "";
netshWriter.println(command);
while ((str = inPipe.readLine()) != null) {
System.out.println(str);
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void closeNetsh(){
executeCommand("exit");
}
public static void main(String[] args){
NetshThread nthread = new NetshThread();
Thread xs = new Thread(nthread);
xs.run();
String command = "int ip set address " +
"\"Local Area Connection 6\" static .69.69.69 255.255.255.0";
nthread.executeCommand(command);
command = "int ip set address " +
"\"Local Area Connection 6\" static 69.69.69.69 255.255.255.0";
nthread.executeCommand(command);
System.out.println("*** DONE ***");
}
}
我得到的输出:
执行:int ip set address "Local 区域连接 6" 静态 .69.69.69 255.255.255.0 netsh>.69.69.69 不是 addr 可接受的值。 执行:int ip set address "Local 区域连接 6" 静态 69.69.69.69
为什么第二条命令没有执行???
255.255.255.0
* 完成 *
更新 2:
在一位老师在西班牙 Windows 环境中试用我的应用程序之前,一切似乎都很好......
我的代码如下所示:
扫描仪 fi = new Scanner(netshProcess.getInputStream());
public void executeCommand(String command) {
System.out.println("Executing: " + command);
String str = "";
netshWriter.println(command);
fi.skip("\\s*");
str = fi.nextLine();
System.out.println(str);
}
我需要以某种方式将 netshWriter 编码设置为 windows 默认值。
谁能知道谁来做这个?
【问题讨论】:
-
我相信这是因为 readLine() 正在阻塞等待来自命令的更多输入。看我之前的回答
-
您不想实际创建一个 java 线程 - 这可能会导致问题(在 startNetsh 完成之前调用 executeCommand)。在发送下一个命令之前,您需要阅读 netsh 的所有(并且仅是所有)输出。如果您只读取一行,并且它试图向您写入多行,那么它可能正在等待 write() 调用并且不会接收您的下一个命令。
标签: java process input standards