安装 exe 后 WMIC Java 命令不工作
WMIC Java command not working after exe installation
一个非常简单的代码运行在调试模式下完美但在安装exe后无法运行,在return中没有给出response/result。甚至没有错误可追踪。
在构建 .exe 并在我的 PC 上安装后,它发生了,非常奇怪。
尝试过进程生成器但同样的事情,无论如何 check/trace 它。也许路径?
StringBuilder b = new StringBuilder();
Process p = Runtime.getRuntime().exec("wmic diskdrive get signature");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
b.append(line);
}
请注意:命令前的 CMD /c return 实际环境中的空响应。
带有参数的内部 windows 命令,例如 "wmic diskdrive ..."
可以通过 将其包装在 cmd window.
中轻松执行
这是 运行 wmic 命令的工作代码片段,封装在 cmd window:
导入java.io.*;
public class Wmic {
public static void main(String[] args) {
StringBuilder b = new StringBuilder();
try {
// Initialize a process object to run
// wmic command and its parameters
// inside a cmd window
Process process = Runtime.getRuntime()
.exec("cmd /c C:\Windows\System32\wbem\WMIC.exe diskdrive get signature");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
b.append(line);
}
} catch (Exception ex) {
b.append(ex.toString());
}
System.out.println("Output: \n" + b.toString());
}
}
输出:
>javac Wmic.java
>java Wmic
Output:
Signature
更多信息:
https://mkyong.com/java/how-to-execute-shell-command-from-java/
ProcessBuilder
构造函数接受一个字符串列表。当使用 ProcessBuilder
到 运行 命令时,我将命令中的所有单词分隔成单独的字符串。
我在单独的线程中读取进程的输出。我总是等待我通过 ProcessBuilder
启动的命令终止。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
public class PrcBldTs {
public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder("wmic","diskdrive","get","signature");
try {
Process proc = pb.start();
StreamGobbler error = new StreamGobbler(proc.getErrorStream());
StreamGobbler output = new StreamGobbler(proc.getInputStream());
Thread stdout = new Thread(output);
Thread stderr = new Thread(error);
stdout.start();
stderr.start();
int result = proc.waitFor();
stdout.join();
stderr.join();
System.out.println("Exit status = " + result);
if (result != 0) {
System.out.println(error.getContents());
}
else {
System.out.println(output.getContents());
}
}
catch (IOException | InterruptedException x) {
x.printStackTrace();
}
}
}
class StreamGobbler implements Runnable {
private BufferedReader reader;
private StringWriter sw;
public StreamGobbler(InputStream is) {
InputStreamReader isr = new InputStreamReader(is);
reader = new BufferedReader(isr);
sw = new StringWriter();
}
public String getContents() {
return sw.toString();
}
public void run() {
try {
String line = reader.readLine();
while (line != null) {
sw.append(line);
sw.append(System.lineSeparator());
line = reader.readLine();
}
}
catch (IOException xIo) {
throw new RuntimeException(xIo);
}
}
}
运行 上面的代码给了我以下输出。
Exit status = 0
Signature
1145609371
一个非常简单的代码运行在调试模式下完美但在安装exe后无法运行,在return中没有给出response/result。甚至没有错误可追踪。
在构建 .exe 并在我的 PC 上安装后,它发生了,非常奇怪。 尝试过进程生成器但同样的事情,无论如何 check/trace 它。也许路径?
StringBuilder b = new StringBuilder();
Process p = Runtime.getRuntime().exec("wmic diskdrive get signature");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
b.append(line);
}
请注意:命令前的 CMD /c return 实际环境中的空响应。
带有参数的内部 windows 命令,例如 "wmic diskdrive ..." 可以通过 将其包装在 cmd window.
中轻松执行这是 运行 wmic 命令的工作代码片段,封装在 cmd window:
导入java.io.*;
public class Wmic { public static void main(String[] args) {
StringBuilder b = new StringBuilder();
try {
// Initialize a process object to run
// wmic command and its parameters
// inside a cmd window
Process process = Runtime.getRuntime()
.exec("cmd /c C:\Windows\System32\wbem\WMIC.exe diskdrive get signature");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
b.append(line);
}
} catch (Exception ex) {
b.append(ex.toString());
}
System.out.println("Output: \n" + b.toString());
}
}
输出:
>javac Wmic.java
>java Wmic
Output:
Signature
更多信息:
https://mkyong.com/java/how-to-execute-shell-command-from-java/
ProcessBuilder
构造函数接受一个字符串列表。当使用 ProcessBuilder
到 运行 命令时,我将命令中的所有单词分隔成单独的字符串。
我在单独的线程中读取进程的输出。我总是等待我通过 ProcessBuilder
启动的命令终止。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
public class PrcBldTs {
public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder("wmic","diskdrive","get","signature");
try {
Process proc = pb.start();
StreamGobbler error = new StreamGobbler(proc.getErrorStream());
StreamGobbler output = new StreamGobbler(proc.getInputStream());
Thread stdout = new Thread(output);
Thread stderr = new Thread(error);
stdout.start();
stderr.start();
int result = proc.waitFor();
stdout.join();
stderr.join();
System.out.println("Exit status = " + result);
if (result != 0) {
System.out.println(error.getContents());
}
else {
System.out.println(output.getContents());
}
}
catch (IOException | InterruptedException x) {
x.printStackTrace();
}
}
}
class StreamGobbler implements Runnable {
private BufferedReader reader;
private StringWriter sw;
public StreamGobbler(InputStream is) {
InputStreamReader isr = new InputStreamReader(is);
reader = new BufferedReader(isr);
sw = new StringWriter();
}
public String getContents() {
return sw.toString();
}
public void run() {
try {
String line = reader.readLine();
while (line != null) {
sw.append(line);
sw.append(System.lineSeparator());
line = reader.readLine();
}
}
catch (IOException xIo) {
throw new RuntimeException(xIo);
}
}
}
运行 上面的代码给了我以下输出。
Exit status = 0
Signature
1145609371