简体   繁体   English

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

[英]Programmatically compiling .java files, error

When run this code to compile a .java file in this directory: 当运行以下代码来编译此目录中的.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);

With this method: 使用此方法:

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");
    }
}

I get this error in the console of Eclipse Juno(Java 6): 我在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

Please could someone explain to me why this is happening and show me how to fix this? 请有人可以向我解释为什么会发生这种情况,并告诉我如何解决此问题? thanks! 谢谢!

-Regards Mark -关于马克

Your filename ends with .Java - it should be .java . 您的文件名以.Java -应该为.java Otherwise the compiler doesn't expect it to be a source file, and tries to process it as a flag instead. 否则,编译器不希望它成为源文件,而是尝试将其作为标志处理。

.java not .Java .java不是.Java

String name = title + ".Java";

change this to 更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM