java手动加载jar

    @RequestMapping("/testJar")
    public @ResponseBody String exteriorJar(int ys, int csd,int jg,int sf,int yf,int sd){
        String result = "error";
        //从数据库中读取算法类
        String sql = "select EXTERIOR from T_EXTERIOR where id=2";
        InputStream ins = null;
        try{
            Connection con = jdbcTemplate.getDataSource().getConnection();
            Statement ps = con.createStatement();
            ResultSet rs = ps.executeQuery(sql);
            while(rs.next()){
                Blob blob = rs.getBlob("EXTERIOR");
                ins = blob.getBinaryStream();
            }
            File file = new File("test.jar");
            OutputStream os = new FileOutputStream(file);
            byte[] bt = new byte[1024];
            int size = 0;
            while((size=ins.read(bt))!=-1){
                os.write(bt,0,size);
            }
            os.flush();
            os.close();
            ins.close();
            
            URLClassLoader loader = new URLClassLoader(new URL[]{new URL("file:"+file.getAbsolutePath())});
            Class cs = loader.loadClass("com.tf.userApp.bean.Exterior");
            Object obj = cs.newInstance();
            
            Method[] methods = obj.getClass().getMethods();
            for(Method method : methods){
                if(method.getName().equals("add")){
                    Class[] css = method.getParameterTypes();
                    Object[] params = new Object[css.length];
                    params[0] = ys;
                    params[1] = csd;
                    params[2] = jg;
                    params[3] = sf;
                    params[4] = yf;
                    params[5] = sd;
                    /**
                    int index = 0;
                    for(Class cs : css){
                        if(String.class == cs){
                            params[index++] = "String val is :" + index;
                        }
                    }*/
                    Object rt = method.invoke(obj, params);
                    if(rt.getClass() == Integer.class){
                        result = String.valueOf((Integer)rt);
                    }else if(rt.getClass()==String.class){
                        result = (String)rt;
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    } 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值