0
我已經裝盤得到與Runtime.getRuntime().exec(cmd);
爲什麼檢索系統信息返回異常的零點?
系統的信息,我得到以下錯誤:
java.lang.NullPointerException
at java.lang.Runtime.exec(Runtime.java:422)
at java.lang.Runtime.exec(Runtime.java:326)
at mytest.SystemInfoToTextArea.doInBackground(SystemInfoToTextArea.java:31)
at mytest.SystemInfoToTextArea.doInBackground(SystemInfoToTextArea.java:16)
任何建議歡迎類
代碼
public class SystemInfoToTextArea extends SwingWorker<String, Void> {
private JTextArea textArea;
private String cmd;
public SystemInfoToTextArea(JTextArea textArea, String cmd) {
textArea = textArea;
cmd = cmd;
}
@Override
protected String doInBackground() throws Exception {
String outputMem = null;
try {
String lineMem;
outputMem = "";
Process procMem = Runtime.getRuntime().exec(cmd);
BufferedReader input =
new BufferedReader
(new InputStreamReader(procMem.getInputStream()));
while ((lineMem = input.readLine()) != null) {
outputMem = (lineMem + "\n");
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
return outputMem;
}
@Override
protected void done() {
super.done();
try {
textArea.append(get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public static void main(String... args){
JFrame frame = new JFrame();
frame.setSize(700,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JTextArea textArea = new JTextArea("System info");
panel.add(textArea);
frame.setContentPane(panel);
frame.setVisible(true);
SystemInfoToTextArea systemInfoToTextArea = new SystemInfoToTextArea(textArea,"cmd /C dir");
systemInfoToTextArea.execute();
}
}
甚至不重新分配,這是一個沒有操作:) –
我使用Intellij,他沒有警告或抱怨。 – itro
在修正構造函數之後我發生這個錯誤:'java.io.IOException:CreateProcess:free -m -t -o error = 2 \t at java.lang.ProcessImpl.create(Native Method) \t at java.lang。 ProcessImpl。(ProcessImpl.java:81) \t在java.lang.ProcessImpl.start(ProcessImpl.java:30) \t在java.lang.ProcessBuilder.start(ProcessBuilder.java:451) \t在java.lang.Runtime中.exec(Runtime.java:591) \t在java.lang.Runtime.exec(Runtime.java:429) \t在java.lang.Runtime.exec(Runtime.java:326) \t在mytest.SystemInfoToTextArea。 doInBackground(SystemInfoToTextArea.java:31) \t' –
itro