Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从模板创建类时应为“expected”_Java - Fatal编程技术网

Java 从模板创建类时应为“expected”

Java 从模板创建类时应为“expected”,java,Java,我有一个我正在创建的机器人,它可以从IRC通道获取输入,为机器人创建新的类,以便在运行时使用。但是,当它尝试编译该类时,会在类名处导致标识符预期错误。但是,如果我键入一个与bot使用模板创建的类相同的类,那么它编译时不会出现问题。以下是用于此过程的3种方法: //Create basic command public static int writeBasicCommand(String trigger, String output, boolean edit) { int succes

我有一个我正在创建的机器人,它可以从IRC通道获取输入,为机器人创建新的类,以便在运行时使用。但是,当它尝试编译该类时,会在类名处导致标识符预期错误。但是,如果我键入一个与bot使用模板创建的类相同的类,那么它编译时不会出现问题。以下是用于此过程的3种方法:

//Create basic command
public static int writeBasicCommand(String trigger, String output, boolean edit) {
    int success = 0, existenceError = 1, unknownError = 2;
    try {
        String filePath = "C:/commands/" + trigger + ".java"; //Location for new class
        File file = new File(filePath);

        //Check if command exists
        if (file.exists()) {
            if(!edit) {
                return existenceError;
            }
        } else if(edit) {
            return existenceError;
        }

        //Grab and modify template
        String template = readFile("C:/template.txt");
        String namedCom = template.replace("--COMMANDNAME--", trigger);
        String content = namedCom.replace("--COMMANDRESULT--", "\"" + output + "\"");

        //Write command
        WriteFile(content, file, false);
        if (Compile(filePath)==true) {
            System.out.println("Done");
            return success;
        } else {
            return unknownError;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return unknownError;
    }
}

//Compile new commands
public static boolean Compile(String fileToCompile) {
    System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.7.0_11");
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    int compilationResult = compiler.run(null, null, null, fileToCompile);
    if(compilationResult == 0) {
        System.out.println("Compilation is successful");
        return true;
    } else {
        System.out.println("Compilation Failed");
        if ((new File(fileToCompile).exists())) {
            new File(fileToCompile).delete();
        }
        return false;
    }
}

//Write to a file
public static boolean WriteFile(String fileContents, File destination, boolean append) {
    try {

        // if file doesnt exist, then create it
        if (!destination.exists()) {
            destination.createNewFile();
        }

        FileWriter fw = new FileWriter(destination.getAbsoluteFile(), append);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(fileContents);
        bw.close();
        fw.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

如果一个文件编译而另一个不编译,那么它们显然是不同的。我建议你使用一个diff工具来找出区别是什么。这看起来很可怕。你不能不使用命令模式或类似的方式生成代码吗?我不知道有什么区别。我在文本编辑器中并排打开了这两个页面,并没有看到它们之间的区别them@5gon12eder我查看了命令模式,但我不确定在这种情况下该模式是否有效。即使在关闭bot之后,我也需要能够保存该类以供以后使用。如果不真正知道您想要完成什么,很难告诉您解决方案。但是请注意,您可以轻松地持久化类的任何对象实例,而不是类到磁盘,并在以后使用Java附带的对象序列化基础结构重新加载它。