以编程方式编译.java文件,错误

时间:2013-10-02 10:56:56

标签: java eclipse compilation javac

运行此代码以编译此目录中的.java文件时:

String title = txtName.getText();
        String name = title + ".Java";
        String src = Minecraft.getMinecraft().mcDataDir.getParent().toString() + "/saves/" + world.getWorldInfo().getWorldName().toString() + "/TechRev/Compiler/";
        compile(src + name);

使用这种方法:

public void compile(String file) {
    String fileToCompile = file;
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    int compilationResult = compiler.run(null, null, null, fileToCompile);
    if (compilationResult == 0) {
        System.out.println("Compilation is successful");
    } else {
        System.out.println("Compilation Failed");
    }
}

我在Eclipse Juno(Java 6)的控制台中收到此错误:

2013-10-02 20:47:47 [INFO] [STDERR] javac: invalid flag: /Users/marko5049/Desktop/Modding/forge/mcp/jars/saves/TechRev/TechRev/Compiler/TechRev.Java
2013-10-02 20:47:47 [INFO] [STDERR] Usage: javac <options> <source files>
2013-10-02 20:47:47 [INFO] [STDERR] use -help for a list of possible options
2013-10-02 20:47:47 [INFO] [STDOUT] Compilation Failed

请有人向我解释为什么会这样,并告诉我如何解决这个问题?谢谢!

-Regards 标记

2 个答案:

答案 0 :(得分:6)

您的文件名以.Java结尾 - 它应该是.java。否则,编译器不会期望它是源文件,而是尝试将其作为标志处理。

答案 1 :(得分:1)

.java 不是 .Java

String name = title + ".Java";

将此更改为

`String name = title + ".java"`;