动态设置classpath

时间:2017-10-08 10:17:23

标签: java maven classloader urlclassloader

我正在编写一个计算器,可以动态加载maven依赖项所需的所有类。例如slf4j,... 但我有一个问题。因为我不想将我的类路径设置为manifest,我的customClassLoader会自行完成。 但是我的记录器是一个私有的静态final字段,JVM想在我调用customClassLoader之前加载它并且我得到错误!我该怎么办?

public class Calculator{

private List<String> expressions = new ArrayList<String>() ;
private List<String> results = new ArrayList<String>();
private final Logger logger = LoggerFactory.getLogger(Calculator.class);
public Calculator(){
    //System.out.println("HELLO");
    OperatorsCatalog catalog = new OperatorsCatalog();
}
public void calculate(String inputaddress){
    //this.loadAddedClasses();
    try{
        logger.debug("Start to read the input file");
        this.read(inputaddress);
        logger.info("Input file is read");

        for(int i = 0;i<expressions.size();i++){
            ExpressionCalculator e = new ExpressionCalculator(expressions.get(i),OperatorsCatalog.getKnownOperators());
            e.evaluate();
            results.add(e.getResult());

        }
        logger.info("All evaluations ended");
        logger.debug("Writing to file started");
        this.write();

    }
    catch(FileNotFoundException  e){
        logger.warn("Can not find Input file",e);
    }
    catch(IOException er){
        logger.warn(er.getMessage());
    }

}
public void read(String inputaddress)throws FileNotFoundException,IOException{

    CustomReader reader = new CustomReader();
    expressions =reader.read(inputaddress);
}
public void write(){
    CustomWriter writer = new CustomWriter();
    writer.write(results);
}
/*public void loadAddedClasses(){

    CustomClassLoader classloader = new CustomClassLoader();
    classloader.loadClasses();

}*/
public static void main(String args[]) {
System.out.println("HELLO");
    CustomClassLoader classloader = new CustomClassLoader();
    classloader.loadClasses();
    Calculator calculator = new Calculator();
    calculator.calculate(args[0]);


}

}

0 个答案:

没有答案