Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7047211

ToolProvider.getSystemJavaCompiler() options do not work.

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 6u25
    • Fix Version/s: None
    • Component/s: tools
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_vista

      Description

      FULL PRODUCT VERSION :
      java version "1.6.0_25"
      Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
      Java HotSpot(TM) 64-Bit Server VM (build 20.0-b11, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.0.6002]

      A DESCRIPTION OF THE PROBLEM :
      Options that the compiler tool lists as valid do not work.

      The compiler tool obtained with
          JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
      reports the usual options when run without options or an input file, but some options do not work.

      Of the options that I tried, I was able to get
        -verbose
      to work,
      but not
        -d
        -version
        -help
      .

      The -d option would be especially helpful in programs that compile generated code.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile and run source code given below.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      All options should work, since they are listed by this version of the compiler
      ACTUAL -
      >java -cp C:\Users\cowang\Documents\Projects\TLanguage\build\test CompilerToolOptionTester

      WITHOUT FILE option =
      Usage: javacTask <options> <source files>
      where possible options include:
        -g Generate all debugging info
        -g:none Generate no debugging info
        -g:{lines,vars,source} Generate only some debugging info
        -nowarn Generate no warnings
        -verbose Output messages about what the compiler is doing
        -deprecation Output source locations where deprecated APIs are used
        -classpath <path> Specify where to find user class files and annotation processors
        -cp <path> Specify where to find user class files and annotation processors
        -sourcepath <path> Specify where to find input source files
        -bootclasspath <path> Override location of bootstrap class files
        -extdirs <dirs> Override location of installed extensions
        -endorseddirs <dirs> Override location of endorsed standards path
        -proc:{none,only} Control whether annotation processing and/or compilation is done.
        -processor <class1>[,<class2>,<class3>...]Names of the annotation processors to run; bypasses default discovery proces
      s
        -processorpath <path> Specify where to find annotation processors
        -d <directory> Specify where to place generated class files
        -s <directory> Specify where to place generated source files
        -implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files
        -encoding <encoding> Specify character encoding used by source files
        -source <release> Provide source compatibility with specified release
        -target <release> Generate class files for specific VM version
        -version Version information
        -help Print a synopsis of standard options
        -Akey[=value] Options to pass to annotation processors
        -X Print a synopsis of nonstandard options
        -J<flag> Pass <flag> directly to the runtime system


      WITH FILE option =

      WITH FILE option = -verbose
      [parsing started string:///SimpleClass.java from JavaSourceFromString]
      [parsing completed 10ms]
      [search path for source files: C:\Users\cowang\Documents\Projects\TLanguage\build\test]
      [search path for class files: C:\Program Files\Java\jdk1.6.0_25\jre\lib\resources.jar,C:\Program Files\Java\jdk1.6.0_25jre\lib\rt.jar,C:\Program Files\Java\jdk1.6.0_25\jre\lib\sunrsasign.jar,C:\Program Files\Java\jdk1.6.0_25\jre\lib\jsse.j
      ar,C:\Program Files\Java\jdk1.6.0_25\jre\lib\jce.jar,C:\Program Files\Java\jdk1.6.0_25\jre\lib\charsets.jar,C:\Program F
      iles\Java\jdk1.6.0_25\jre\lib\modules\jdk.boot.jar,C:\Program Files\Java\jdk1.6.0_25\jre\classes,C:\Program Files\Java\j
      dk1.6.0_25\jre\lib\ext\dnsns.jar,C:\Program Files\Java\jdk1.6.0_25\jre\lib\ext\localedata.jar,C:\Program Files\Java\jdk1
      .6.0_25\jre\lib\ext\sunjce_provider.jar,C:\Users\cowang\Documents\Projects\TLanguage\build\test]
      [loading java\lang\Object.class(java\lang:Object.class)]
      [checking SimpleClass]
      [wrote SimpleClass.class]
      [total 60ms]

      WITHOUT FILE option = -help
      Exception in thread "main" java.lang.IllegalArgumentException: invalid flag: -help
              at com.sun.tools.javac.api.JavacTool.processOptions(JavacTool.java:236)
              at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:207)
              at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:53)
              at CompilerToolOptionTester.attemptWithoutFile(CompilerToolOptionTester.java:53)
              at CompilerToolOptionTester.main(CompilerToolOptionTester.java:89)

      >

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.IllegalArgumentException: invalid flag: -help
              at com.sun.tools.javac.api.JavacTool.processOptions(JavacTool.java:236)
              at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:207)
              at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:53)
              at CompilerToolOptionTester.attemptWithoutFile(CompilerToolOptionTester.java:53)
              at CompilerToolOptionTester.main(CompilerToolOptionTester.java:89)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.StringWriter;
      import java.net.URI;
      import java.util.ArrayList;
      import javax.tools.DiagnosticCollector;
      import javax.tools.JavaCompiler;
      import javax.tools.JavaFileObject;
      import javax.tools.SimpleJavaFileObject;
      import javax.tools.StandardJavaFileManager;
      import javax.tools.ToolProvider;


      public class CompilerToolOptionTester {

      private final String javaClassSource = "class SimpleClass {}";


      public CompilerToolOptionTester() {}


      private void attemptWithFile(String option) {
        System.out.println();
        System.out.println("WITH FILE option = "+option);
        ArrayList<String> options = constructOptions(option);
        
        ArrayList<JavaSourceFromString> compilationUnits = new ArrayList<JavaSourceFromString>(1);
        compilationUnits.add(new JavaSourceFromString("SimpleClass", javaClassSource));
        
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        
        StringWriter compilerOutput = null; // i.e., st err instead
        DiagnosticCollector<JavaFileObject> diagnostics = null; // i.e., st err instead
        compiler.getTask(compilerOutput, fileManager, diagnostics, options, null, compilationUnits)
                .call();
      }


      private void attemptWithoutFile(String option) {
        System.out.println();
        System.out.println("WITHOUT FILE option = "+option);
        ArrayList<String> options = constructOptions(option);

        ArrayList<JavaSourceFromString> compilationUnits = null;
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        if (compiler == null) {
          System.err.println("JAVA COMPILER TOOL NOT AVAILABLE");
          return;
          }
        StandardJavaFileManager fileManager = null;
        StringWriter compilerOutput = null; // i.e., st err instead
        DiagnosticCollector<JavaFileObject> diagnostics = null; // i.e., st err instead
        compiler.getTask(compilerOutput, fileManager, diagnostics, options, null, compilationUnits)
                .call();
      }

      private ArrayList<String> constructOptions(String option) {
        ArrayList<String> options = new ArrayList<String>(1);
        if (option != "") {
          options.add(option);
        }
        return options;
      }


      public static final class JavaSourceFromString extends SimpleJavaFileObject {
      final String sourceCode;

      JavaSourceFromString(String compilationUnitName, String sourceCode) {
        super( URI.create("string:///" + compilationUnitName.replace('.','/') + Kind.SOURCE.extension)
             , Kind.SOURCE
             );
        this.sourceCode = sourceCode;
      }

      @Override
      public CharSequence getCharContent(boolean ignoreEncodingErrors) {
        return sourceCode;
      }

      } // subclass JavaSourceFromString


      public static void main(String[] args) {
        CompilerToolOptionTester tester = new CompilerToolOptionTester();
        tester.attemptWithoutFile("");
        tester.attemptWithFile("");
        tester.attemptWithFile("-verbose");
        tester.attemptWithoutFile("-help");
      }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      if options are needed, write to file and use
      String runOutput = OS.runCommand("javac " +optionString+ " " +javaFileName);

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: