我有托盘来获取系统信息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();
}
}