Ich möchte das encoding setzten beim compilieren zur Laufzeit, laut optionen ist es möglich. Wenn ich die option allerdings setzte sagt er das die Option nicht bekannt ist.
Code:
public static void main(String args[]) throws IOException {
System.out.println("Default Compiler: " + ToolProvider.getSystemJavaCompiler().getClass().getName());
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, Locale.UK, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(Arrays
.asList("src//de//compiler//Hello.java"));
JavacOption[] defaultFileManagerOptions = RecognizedOptions
.getDefaultFileManagerOptions(new RecognizedOptions.GrumpyHelper());
for (JavacOption o : defaultFileManagerOptions) {
System.out.println(o.toString());
System.out.println(o.hasArg());
System.out.println(o.getName());
System.out.println(compiler.isSupportedOption(o.toString()));
}
System.out.println(compiler.getSourceVersions());
System.out.println(compiler.isSupportedOption("-g"));
System.out.println(compiler.isSupportedOption("-verbose"));
System.out.println(compiler.isSupportedOption("-target"));
System.out.println(compiler.isSupportedOption("-Xlint"));
System.out.println(compiler.isSupportedOption(OptionName.ENCODING.name()));
List<String> options = new ArrayList<String>();
// options.add("-encoding");
System.out.println(OptionName.ENCODING.name());
System.out.println(new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding());
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null,
compilationUnits);
Boolean success = task.call();
for (Diagnostic<?> diagnostic : diagnostics.getDiagnostics()) {
System.console().printf(
"Code: %s%n" + "Kind: %s%n" + "Position: %s%n" + "Start Position: %s%n" + "End Position: %s%n"
+ "Source: %s%n" + "Message: %s%n", diagnostic.getCode(), diagnostic.getKind(),
diagnostic.getPosition(), diagnostic.getStartPosition(), diagnostic.getEndPosition(),
diagnostic.getSource(), diagnostic.getMessage(null));
}
fileManager.close();
System.out.println("Success: " + success);
}