Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.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文件的编译错误_Java - Fatal编程技术网

以编程方式检查java文件的编译错误

以编程方式检查java文件的编译错误,java,Java,我想以编程方式检查java类是否有编译错误。 你能告诉我怎么做吗?你可以使用编译类,它会告诉你是否有编译错误 人为示例(使用字符串作为输入,但您可以读取文件): 其中compile可以如下所示: private static void compile(String className, String body, Path path) throws Exception { List<JavaSourceFromString> sourceCode = Arrays.asList

我想以编程方式检查java类是否有编译错误。 你能告诉我怎么做吗?

你可以使用编译类,它会告诉你是否有编译错误

人为示例(使用字符串作为输入,但您可以读取文件):

其中compile可以如下所示:

private static void compile(String className, String body, Path path) throws Exception {
    List<JavaSourceFromString> sourceCode = Arrays.asList(new JavaSourceFromString(className, body));

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(path.toFile()));
    boolean ok = compiler.getTask(null, fileManager, null, null, null, sourceCode).call();

    System.out.println("compilation ok = " + ok);
}
private static void compile(字符串类名、字符串正文、路径路径)引发异常{
List sourceCode=Arrays.asList(新的JavaSourceFromString(className,body));
JavaCompiler=ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager=compiler.getStandardFileManager(null,null,null);
setLocation(StandardLocation.CLASS_输出,Arrays.asList(path.toFile());
boolean ok=compiler.getTask(null,fileManager,null,null,null,sourceCode).call();
System.out.println(“编译ok=“+ok”);
}
您可以使用编译类,它会让您知道是否存在一些编译错误

人为示例(使用字符串作为输入,但您可以读取文件):

其中compile可以如下所示:

private static void compile(String className, String body, Path path) throws Exception {
    List<JavaSourceFromString> sourceCode = Arrays.asList(new JavaSourceFromString(className, body));

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(path.toFile()));
    boolean ok = compiler.getTask(null, fileManager, null, null, null, sourceCode).call();

    System.out.println("compilation ok = " + ok);
}
private static void compile(字符串类名、字符串正文、路径路径)引发异常{
List sourceCode=Arrays.asList(新的JavaSourceFromString(className,body));
JavaCompiler=ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager=compiler.getStandardFileManager(null,null,null);
setLocation(StandardLocation.CLASS_输出,Arrays.asList(path.toFile());
boolean ok=compiler.getTask(null,fileManager,null,null,null,sourceCode).call();
System.out.println(“编译ok=“+ok”);
}
来自

你需要先编译它。这可以使用以编程方式完成。这只需要在JRE之上的本地计算机上安装

你需要先编译它。这可以使用以编程方式完成。这只需要在JRE之上的本地计算机上安装


+1并找到一个示例+1并找到一个示例。