gpt4 book ai didi

java - 编写标准输入并等待标准输出

转载 作者:行者123 更新时间:2023-11-29 07:22:50 24 4
gpt4 key购买 nike

我正在尝试创建一个让 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...所以我认为我不需要再刷新任何东西,因为构造函数是:

新的 PrintWriter(netshOutStream, true); (就像 Shiny 先生告诉我的那样)...

假设我决定在第一个输出行可用时中断 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 ***");
}
}

我得到的输出:

Executing: int ip set address "Local Area Connection 6" static .69.69.69 255.255.255.0 netsh>.69.69.69 is not an acceptable value for addr. Executing: int ip set address "Local Area Connection 6" static 69.69.69.69

为什么第二个命令没有执行???

255.255.255.0

* DONE *

更新 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 默认值。

有人知道该由谁来做吗?

最佳答案

您正在关闭输出流。

关于java - 编写标准输入并等待标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1638312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com