尝试使用getSystemJavaCompiler()方法创建一个JavaCompiler对象。
在XP环境下,代码能正常运行,而在WIN7下就不能创建该对象。Debug 下查看该对象值为NULL、这个方法在WIN7下需要特殊执行么?

解决方案 »

  1.   

    Files[] files1 = ...; // input for first compilation task
    Files[] files2 = ...; // input for second compilation taskJavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);Iterable<? extends JavaFileObject> compilationUnits1 =
    fileManager.getJavaFileObjectsFromFiles(Arrays.asList(files1));
    compiler.getTask(null, fileManager, null, null, null, compilationUnits1).call();Iterable<? extends JavaFileObject> compilationUnits2 =
    fileManager.getJavaFileObjects(files2); // use alternative method
    // reuse the same file manager to allow caching of jar files
    compiler.getTask(null, fileManager, null, null, null, compilationUnits2).call();fileManager.close();
    http://www.jar114.com/jdk6/zh_CN/api/javax/tools/JavaCompiler.html
      

  2.   

    没有用啊、
    执行
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();后、compiler 永远是null
      

  3.   

    getSystemJavaCompiler
    public static JavaCompiler getSystemJavaCompiler()Gets the Java™ programming language compiler provided with this platform. 返回:
    the compiler provided with this platform or null if no compiler is provided或许无法在当前操作系统中的环境变量找到吧 
      

  4.   


    原来机子上有1.5 和 1.6 2个版本的JDK,现在卸载只剩一个。能使用了。谢谢哈~