Java教程

Java编译类文件

本文主要是介绍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编译类文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!