java loadjar

package org.stock;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLClassLoader;

import org.stock.plugin.StockPlugin;

public class Application {
public static void main(String[] args) {
Application app = new Application();

for (double d = 0.1; d < 0.4; d += 0.05) {
Double[] a = {d, d,d};
Double b = app.tangWay(a);
System.out.println("r: " + app.round(d) +" it is third: " + app.round(b) +" 25/2: " + app.round(b*25/2) +" avg: " + app.round(Math.pow(b, 1/3.00)-1));
}

//System.out.println(app.calculateGrowR(3, 1.05, 2.05));

for (double d = 0.1; d < 0.4; d += 0.05) {
Double totalProfit = app.getTotalProfit(10,app.round(d));
System.out.println("10 years total: " + totalProfit + " r: " + app.round(d));
}

/*
for (double d = 10.0; d < 50.0; d += 1.0) {
Double r = app.calculateR(10, d);
Double r1 = app.calculateR1(10, d);
System.out.println("PE: " + Math.round(d) + " " + r + "-" + r1);
}
*/

String jarPath = "C:/test/n.jar";
loadJar(jarPath);
//利用反射
Class<?> aClass;
try {
aClass = Class.forName("org.stock.plugin.Factory");
Object instance = aClass.newInstance();
//调用方法
StockPlugin x = (StockPlugin)aClass.getDeclaredMethod("getInstance").invoke(instance);
System.out.println(x.calculate());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("i am ok~");
}

private Double getTotalProfit(int year, Double r) {

Double l = 0.0;

for (int i = 0; i < year; i++) {
l += Math.pow(1 + round(r), i);
//System.out.println(l);
}
return round(l);
}


private Double tangWay(Double[] list) {
Double total = 0D;
total = list[0]+1;
total = total * (list[1]+1);
total = total * (list[2]+1);
return total;
}

private Double calculateR(int year, Double last) {

double r1 = 0.0;
for (Double r = 0.01; r < 1; r += 0.01) {
Double l = 0.0;
// System.out.println(round(r));

for (int i = 0; i < year; i++) {
l += Math.pow(1 + round(r), i);
//System.out.println("ls is" + Math.pow(1 + round(r), i));
}
// System.out.println("ls is" + Math.round(l));
if (l >= last) {
r1 = r;
break;
}
}
return round(r1);
}

private Double calculateR1(int year, Double last) {

double r1 = 0.0;
for (Double r = 1.00; r >= 0.01; r -= 0.01) {
Double l = 0.0;
// System.out.println(round(r));

for (int i = 0; i < year; i++) {
l += Math.pow(1 + round(r), i);
}
// System.out.println(Math.round(l));
if (l <= last) {
r1 = r;
break;
}
}
return round(r1);
}

private Double round(Double d) {
BigDecimal b = new BigDecimal(d);
d = b.setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue();
return d;
}

private Double calculateGrowR(double year, Double start, Double end) {

double r1 = 0.0;
r1 = Math.pow(end / start, 1 / year) - 1;
return round(r1);
}

public static void loadJar(String jarPath) {
File jarFile = new File(jarPath);
//文件存在
if (jarFile.exists() == false) {
System.out.println("jar file not found.");
return;
}
//从URLClassLoader类加载器中获取类的addURL方法
Method method = null;
try {
method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
} catch (NoSuchMethodException | SecurityException e1) {
e1.printStackTrace();
}
// 获取方法的访问权限
boolean accessible = method.isAccessible();
try {
//修改访问权限为可写
if (accessible == false) {
method.setAccessible(true);
}
// 获取系统类加载器
URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
//获取jar文件的url路径
URL url = jarFile.toURI().toURL();
//jar路径加入到系统url路径里
method.invoke(classLoader, url);
} catch (Exception e) {
e.printStackTrace();
} finally {
method.setAccessible(accessible);
}
}

}

posted @ 2021-07-21 10:40  涵旭  阅读(131)  评论(0编辑  收藏  举报