システム情報を取得するためのトレイがあります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();
}
}