指定动态编译的输出路径

Dan*_*ark 9 java dynamic-compilation java-6

我在Java 6中的动态编译工作正常.但是,我想改变输出路径.我已经尝试了很多东西(我会饶了你)无济于事.无论如何,这是工作代码

String[] filesToCompile = { "testFiles/Something.java" };
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(filesToCompile);
CompilationTask task = compiler.getTask(null, fileManager, null,null, null, compilationUnits);
System.out.println("Good? " + task.call());
Run Code Online (Sandbox Code Playgroud)

但输出转到源目录,这不是我想要的.

我怀疑答案可能在于答案,compiler.getTask但API并不是非常清楚某些参数可能意味着什么.或者也许使用fileManager.我试过了

fileManager.setLocation(StandardLocation.locationFor("testFiles2"), null);
Run Code Online (Sandbox Code Playgroud)

但同样,猜测可能不是一个好主意.

谢谢!

编辑:我也试过使用选项,这样(对不起,如果有更紧凑的方式):

    final List<String> optionsList = new ArrayList<String>();
    optionsList.add("-d what");
    Iterable<String> options = new Iterable<String>() {         
        public Iterator<String> iterator() {
            return optionsList.iterator();
        }
    };
Run Code Online (Sandbox Code Playgroud)

然后将选项传递给getTask,但错误消息是"无效标志".

Osc*_*Ryz 10

我今天面临同样的问题.

答案(使用常规getTask方法而不是`run)是在FileManager中指定输出目录:

fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(outputDir));
Run Code Online (Sandbox Code Playgroud)

就是这样!! :)

文档有点误导,我的意思是,样本可以派上用场.但最终它把我带到了那里.

编辑

这是一个正在运行的示例:

    // write the test class
    File sourceFile   = new File("First.java");
    FileWriter writer = new FileWriter(sourceFile);

    writer.write(
            "package load.test;\n" +
            "public class First{}"
    );
    writer.close();

    // Get the java compiler for this platform
    JavaCompiler compiler    = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(
            null,
            null,
            null);

    //--           H E R E    --// 
    // Specify where to put the genereted .class files
    fileManager.setLocation(StandardLocation.CLASS_OUTPUT, 
                            Arrays.asList(new File("/tmp")));
    // Compile the file
    compiler
        .getTask(null,
                fileManager,
                null,
                null,
                null,
                fileManager.getJavaFileObjectsFromFiles(Arrays.asList(sourceFile)))
        .call();
    fileManager.close();

    // delete the file
    sourceFile.deleteOnExit();
Run Code Online (Sandbox Code Playgroud)


Gim*_*ima 6

第一篇文章中的代码可以正常工作,但会抛出以下错误:

java.lang.IllegalArgumentException: invalid flag: -d folder
Run Code Online (Sandbox Code Playgroud)

这是因为通过传递"-d folder"使得解析器认为它正在解析一个选项.选项必须分开"-d", "folder".

工作示例如下:

JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager sjfm = javaCompiler.getStandardFileManager(null, null, null); 

String[] options = new String[] { "-d", "output" };
File[] javaFiles = new File[] { new File("src/gima/apps/flip/TestClass.java") };

CompilationTask compilationTask = javaCompiler.getTask(null, null, null,
        Arrays.asList(options),
        null,
        sjfm.getJavaFileObjects(javaFiles)
);
compilationTask.call();
Run Code Online (Sandbox Code Playgroud)