使用 java 执行 shell 命令时与进程通信
communicating with the process while executing a shell command with java
我要从 java 执行 shell 命令,我需要在执行命令时将参数传递给输出流..
以下是shell命令
./darknet detect cfg/yolo-voc.2.0.cfg backup/yolo-voc_20000.weights
执行此命令时,它会生成终端中图像文件的路径,我可以提供图像的路径,如下所示
Loading weights from backup/yolo-voc_21000.weights...Done!
Enter Image Path:
从终端执行时,我可以在那里提供路径。
我设法通过 java 进程执行了这个命令,而且当我为命令提供图像 uri 时,我也可以获得输出。这是代码
public static void execCommand(String command) {
try {
Process proc = Runtime.getRuntime().exec(command);
// Read the output
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
//reader.readLine();
while ((line = reader.readLine()) != null) {
System.out.print(line + "\n");
s.add(line);
}
// proc.waitFor();
} catch (IOException e) {
System.out.println("exception thrown: " + e.getMessage());
}
}
但我想要的是在运行时提供图像路径,而不是在命令执行开始时提供图像路径。
尝试如下写入输出流仍然没有成功
public static void execCommand(String command) {
try {
Process proc = Runtime.getRuntime().exec(command);
// Read the output
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
writer.append("data/test2.jpg");
writer.newLine();
//reader.readLine();
while ((line = reader.readLine()) != null) {
System.out.print(line + "\n");
s.add(line);
}
// proc.waitFor();
} catch (IOException e) {
System.out.println("exception thrown: " + e.getMessage());
}
}
您需要调用 writer.flush()
才能实际输出一些内容到下划线 InputStream
因此您的代码应如下所示:
public static void execCommand(String command) {
try {
Process proc = Runtime.getRuntime().exec(command);
// Read the output
BufferedReader reader = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
String line = "";
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
writer.append("data/test2.jpg");
writer.newLine();
// **** add flush here ****
writer.flush();
// and remember to close your resource too
writer.close();
//reader.readLine();
while ((line = reader.readLine()) != null) {
System.out.print(line + "\n");
s.add(line);
}
// ***** close your reader also ****
reader.close();
// proc.waitFor();
} catch (IOException e) {
System.out.println("exception thrown: " + e.getMessage());
}
}
我要从 java 执行 shell 命令,我需要在执行命令时将参数传递给输出流..
以下是shell命令
./darknet detect cfg/yolo-voc.2.0.cfg backup/yolo-voc_20000.weights
执行此命令时,它会生成终端中图像文件的路径,我可以提供图像的路径,如下所示
Loading weights from backup/yolo-voc_21000.weights...Done!
Enter Image Path:
从终端执行时,我可以在那里提供路径。
我设法通过 java 进程执行了这个命令,而且当我为命令提供图像 uri 时,我也可以获得输出。这是代码
public static void execCommand(String command) {
try {
Process proc = Runtime.getRuntime().exec(command);
// Read the output
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
//reader.readLine();
while ((line = reader.readLine()) != null) {
System.out.print(line + "\n");
s.add(line);
}
// proc.waitFor();
} catch (IOException e) {
System.out.println("exception thrown: " + e.getMessage());
}
}
但我想要的是在运行时提供图像路径,而不是在命令执行开始时提供图像路径。 尝试如下写入输出流仍然没有成功
public static void execCommand(String command) {
try {
Process proc = Runtime.getRuntime().exec(command);
// Read the output
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
writer.append("data/test2.jpg");
writer.newLine();
//reader.readLine();
while ((line = reader.readLine()) != null) {
System.out.print(line + "\n");
s.add(line);
}
// proc.waitFor();
} catch (IOException e) {
System.out.println("exception thrown: " + e.getMessage());
}
}
您需要调用 writer.flush()
才能实际输出一些内容到下划线 InputStream
因此您的代码应如下所示:
public static void execCommand(String command) {
try {
Process proc = Runtime.getRuntime().exec(command);
// Read the output
BufferedReader reader = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
String line = "";
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
writer.append("data/test2.jpg");
writer.newLine();
// **** add flush here ****
writer.flush();
// and remember to close your resource too
writer.close();
//reader.readLine();
while ((line = reader.readLine()) != null) {
System.out.print(line + "\n");
s.add(line);
}
// ***** close your reader also ****
reader.close();
// proc.waitFor();
} catch (IOException e) {
System.out.println("exception thrown: " + e.getMessage());
}
}