0

How to run this code using command prompt. it always show null pointer exception.

/*Program1.java*/
import javax.swing.JFrame;

public class Program1 {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setVisible(true);
    }
}

/*Program2.java*/
import javax.tools.*;

public class Program2 {
    public static void main(String[] args){
        String fileToCompile = "Program1.java";
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        int compilationResult = compiler.run(null, null, null, fileToCompile);

        if(compilationResult == 0){
            System.out.println("Compilation is successful");
        }else{
            System.out.println("Compilation failed");

        }
    }
}

and how to add the tools.jar in a classpath.

user3276091
  • 263
  • 3
  • 10
  • 1
    java -classpath jarName.jar this is how you add a jar in classpath when working with command line – Vihar Sep 02 '14 at 10:37
  • 1
    What has this got to do with compilation problems? Seems like it compiles just fine, it's the running part that 's bothering you. – Stultuske Sep 02 '14 at 10:42
  • @Stultuske yeah the running part sorry. can you help me? – user3276091 Sep 02 '14 at 10:43
  • 2
    Does http://stackoverflow.com/questions/2543439/null-pointer-exception-while-using-java-compiler-api?rq=1 help? – Deltharis Sep 02 '14 at 10:45
  • 1
    check where the nullpointer exception is thrown, then check what you did wrong, or forgot to do. One of the instances you are using is still null the moment you call a method on it (most likely). Try and get that fixed. – Stultuske Sep 02 '14 at 10:45

0 Answers0