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