0

The section of code in question is:

String fileToCompile = "C:/Users/Jeff/Documents/Test/Compiler 6/examplejavafile.java";//Absolute path
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
FileOutputStream errorStream = new FileOutputStream("Errors.txt");
int compilationResult = compiler.run(null, null, errorStream, "-verbose", fileToCompile);
if(compilationResult == 0){
    System.out.println("Compilation is successful");
}else{
    System.out.println("Compilation Failed");
}

When I run this section of code it gives me NPE on:

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

Now I've been told that this is an error in the environment, but I have JDK installed and my CLASSPATH variable is set to "C:\Program Files\Java\jdk1.7.0_04\bin". I might be a noob and not understand how to run a program through JDK instead of JRE, but regardless I need help. (Also, I am using Eclipse, if it's any different.)

Jeff Demanche
  • 624
  • 7
  • 25

3 Answers3

0

Look up the Run Configuration for the project (under the Run menu). Under JRE is should specify the project execution environment is using 'JavaSE-1.6' (jsk1.6.20_32) or such.

John D
  • 2,307
  • 17
  • 28
  • This makes sense, but the only options I see there say (jre7), which leads me to believe that I installed JDK wrong; Is there something I need to do to eclipse to update my installation? – Jeff Demanche Jun 06 '12 at 00:18
0

ToolProvider.getSystemJavaCompiler() is not available.

Is tools.jar missing from the classpath?

Set class path to the tools.jar file which can found in jdk\jre directory.

System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.7.0_02");

Akhilesh Dhar Dubey
  • 2,152
  • 2
  • 25
  • 39
-1

You need to import the ToolProvider since it is not defined, unless you just didn't include that information.

import javax.tools.ToolProvider;
richardhsu
  • 878
  • 5
  • 13
  • Yes I have imported all the necessary tools. It's a console error on runtime. – Jeff Demanche Jun 05 '12 at 23:45
  • Hmm, I can't seem to see why that line would throw a NPE or even the line before it. I just ran the code and it seems to work fine on Ubuntu. Sorry :( – richardhsu Jun 05 '12 at 23:59
  • 1
    Please consider another [question](http://stackoverflow.com/questions/9403655/javacompiler-returning-null). In two words - if you are on Windows then check if JAVA_HOME points to JDK and not runtime environment. – Fedor Rusak Feb 13 '13 at 08:44