2

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();
}
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
SongDoHou
  • 31
  • 7
  • Do you actually initialise `file_Name` and `JAVA_FILE_LOCATION`? I get no exception. Also, take a look at [this](https://stackoverflow.com/questions/3132302/unknown-source-in-exception-stack-trace). – bcsb1001 Nov 25 '17 at 14:12
  • 1
    Have you already debugged your code and checked the content of `command` at line 138? Or what if you insert `System.out.println(command)` after `try {`? – Gerold Broser Nov 25 '17 at 16:45
  • `JAVA_FILE_LOCATION` is not initialized, so it is null. This means your next line of code is actually doing this: `static String[] command = {"javac", null};` Changing JAVA_FILE_LOCATION later does nothing to alter the elements of the `command` array. You must reassign `command[1]`. – VGR Nov 25 '17 at 19:45
  • Also consider [`javax.tools.JavaCompiler`](https://stackoverflow.com/search?tab=votes&q=%5bjava%5d%20javax.tools.JavaCompiler). – trashgod Nov 25 '17 at 20:29
  • You could make use `JavaCompiler` instead [for example](https://stackoverflow.com/questions/21544446/how-do-you-dynamically-compile-and-load-external-java-classes/21544850#21544850) – MadProgrammer Nov 25 '17 at 20:49
  • Why are you making two `ProcessBuilder`s? You're also not waiting for the execution of the first `ProcessBuilder` to finish before executing the second, which means the first could still be running – MadProgrammer Nov 25 '17 at 20:51
  • Thanks to all of you. the command was problem. I fix it. I really appreciate. – SongDoHou Nov 26 '17 at 04:59

0 Answers0