I'm doing a small Java project which is making a simple Java IDE with using Processbuilder
class.
The IDE get input the Java file name (ex: test.java
) and only compiles the Java file where exists in the same project folder.
But I'm getting difficulty in error messages about processBuilder.start()
.
Can anyone see my code and what is wrong with my code?
I can't handle with error messages and fix my codes.
Here is error messages when I clicked compile button:
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
And my some codes here:
static String file_Name;
static String JAVA_FILE_LOCATION;
static String[] command = {"javac", JAVA_FILE_LOCATION};
Also, I get input and initialize above String
variables before btn_Compile
event occured.
(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();
}