我正在做一个小型Java项目,它使用Processbuilder
类创建一个简单的Java IDE。
IDE输入Java文件名(例如:test.java
)并仅编译存在于同一项目文件夹中的Java文件。
但是我对processBuilder.start()
的错误消息感到困难。
任何人都可以看到我的代码以及我的代码有什么问题吗?
我无法处理错误消息并修复我的代码。
单击编译按钮时出现错误消息:
java.lang.NullPointerException
at java.lang.ProcessBuilder.start(Unknown Source)
at sample$2.mouseClicked(sample.java:138)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source) .. etc
我的一些代码在这里:
static String file_Name;
static String JAVA_FILE_LOCATION;
static String[] command = {"javac", JAVA_FILE_LOCATION};
此外,我在String
事件发生之前获得输入并初始化btn_Compile
个变量。
(EX:file_Name = "test.java"
,
JAVA_FILE_LOCATION = "javaProjectFolder\project\test.java"
)
void test() throws IOException, InterruptedException {
btn_Compile.addMouseListener(new MouseAdapter() { // JButton btn_Compile
public void mouseClicked(MouseEvent e) {
try {
ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();
int idx = file_Name.lastIndexOf(".");
String _fileName = file_Name.substring(0, idx); //
process = new ProcessBuilder(new String[] {"java", "-cp", "bin", _fileName}).start();
if (process.getErrorStream().read() != -1) {
inputStream = process.getErrorStream();
}
else { // compile succeed
System.out.println("compiled successfully ...");
inputStream = process.getInputStream();
}
}
catch (Exception e2) {
e2.printStackTrace();
}
}
});
}
public static void main(String[] args) throws IOException, InterruptedException {
sample s = new sample();
s.test();
}