Java编译类文件
2021/12/17 22:20:24
本文主要是介绍Java编译类文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
编译类
package com.spasvo.test; import java.io.*; import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import bsh.*; import javax.tools.*; class MyJavaObject extends SimpleJavaFileObject{ private StringBuilder builder ; /** * Construct a SimpleJavaFileObject of the given kind and with the * given URI. * */ public MyJavaObject(String name) { super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension),Kind.SOURCE); builder = new StringBuilder(); } public void setBuilder(String fileName) throws IOException { File file = new File(fileName); InputStream inputStream = new FileInputStream(file); Reader reader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(reader); String line ; while ((line = bufferedReader.readLine()) != null){ builder.append(line); builder.append("\n"); } } @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { // return super.getCharContent(ignoreEncodingErrors); return builder; } public void append(String str){ builder.append(str); builder.append("\n"); } } public class Main { public static void main(String[] args) throws IOException, ParseException { JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); Iterable<String> options = Arrays.asList("-g","-d","E:\\export"); List<JavaFileObject> objects = new ArrayList<>(); MyJavaObject object = new MyJavaObject("Person"); object.setBuilder("e:\\Person.java"); objects.add(object); JavaCompiler.CompilationTask task = javaCompiler.getTask(null, null, diagnostic -> { System.out.println(diagnostic.toString()); }, options, null, objects); task.call(); } }
测试类
class Person { public String name; public int age; public void funcForTest(String testParma1,int testParma2){ for (int i = 0; i < 10; i++) { //报错句子 System.out.println("for test") } } public void ifElseTest(int name){ if(name == 1){ System.out.println("if test"); }else if(name == 2){ System.out.println("else if test"); }else { System.out.println("else test"); } } public void whileTest(){ while (true){ System.out.println("while test"); } } public void tryCatchTest(){ try { System.out.println("try test"); }catch (Exception e){ System.out.println("catch test"); }finally { System.out.println("finally test"); } } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
运行结果
/Person.java:10: 错误: 需要';' System.out.println("for test") ^ Process finished with exit code 0
这篇关于Java编译类文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求