在运行时加载jar [英] Loading jars at runtime

查看:108
本文介绍了在运行时加载jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在运行时将jar文件添加到classpath。我使用此代码

I am trying to add jar file to classpath at runtime. I use this code

 public static void addURL(URL u) throws IOException {

            URLClassLoader sysloader = (URLClassLoader) ClassLoader
                    .getSystemClassLoader();
            Class<URLClassLoader> sysclass = URLClassLoader.class;

            try {
                Method method = sysclass.getDeclaredMethod("addURL", parameters);
                method.setAccessible(true);
                method.invoke(sysloader, new Object[] { u });
                System.out.println(u);
            } catch (Throwable t) {
                t.printStackTrace();
                throw new IOException("Error");
            }

        }

系统输出打印此网址:

file:/B:/Java/Tools/mysql-connector-java-5.1.18/mysql-connector-java-5.1.18/mysql-connector-java-5.1.18-bin.jar

我查了这条路径小心翼翼,这个罐子存在。即使这个测试显示com.mysql.jdbc。
驱动程序类存在。

I was check this path carefully, this jar exist. Even this test show that com.mysql.jdbc. Driver class exists.

javap -classpath "B:\Java\Tools\mysql-connector-java-5.1.18\
mysql-connector-java-5.1.18\mysql-connector-java-5.1.18-bin.jar" com.mysql.jdbc.
Driver
Compiled from "Driver.java"
public class com.mysql.jdbc.Driver extends com.mysql.jdbc.NonRegisteringDriver i
mplements java.sql.Driver{
    public com.mysql.jdbc.Driver()       throws java.sql.SQLException;
    static {};
}

但是当我使用这个Class.forName时,我仍然得到java.lang.ClassNotFoundException (驱动器)。
这段代码出了什么问题?

But I still get java.lang.ClassNotFoundException when I use this Class.forName(driver). What is wrong with this code?

推荐答案

网址没问题,不过你尝试从类路径加载一个jar ,这意味着你需要先把文件放在cp中。
在你的情况下,你想要加载一个不在类路径中的jar,所以你必须使用
URLClassLoader,对于JAR,你也可以使用JARClassLoader
如果你想要一些示例课:
http://docs.oracle.com/javase/tutorial /deployment/jar/jarclassloader.html

The URL is ok, nevertheless you try to load a jar from classpath, so it means that yo need to have the file in cp first. In your case you want to load a jar that is not in classpath so you have to use URLClassLoader and for JAR you can use also the JARClassLoader If you want some sample lesson on it: http://docs.oracle.com/javase/tutorial/deployment/jar/jarclassloader.html

这里我自己运行的一个示例看看是否有帮助。它搜索不在我的类路径中的Log4j的Logger类,当然我在调用构造函数时遇到异常,因为我没有将正确的参数传递给构造函数

Here a sample I ran by myself see if helps you. It search the Logger class of Log4j that is not in my classpath, of course i got exception on invocation of the constructor since i did not pass the right params to the constructor

package org.stackoverflow;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;

public class URLClassLoaderSample
{
  public static void main(String[] args) throws Exception
  {
    File f = new File("C:\\_programs\\apache\\log4j\\v1.1.16\\log4j-1.2.16.jar");
    URLClassLoader urlCl = new URLClassLoader(new URL[] { f.toURL()},System.class.getClassLoader());
    Class log4jClass = urlCl.loadClass("org.apache.log4j.Logger");
    log4jClass.newInstance();
  }
}



Exception in thread "main" java.lang.InstantiationException: org.apache.log4j.Logger
    at java.lang.Class.newInstance0(Class.java:357)
    at java.lang.Class.newInstance(Class.java:325)
    at org.stackoverflow.URLClassLoaderSample.main(URLClassLoaderSample.java:19)

由于错误的调用导致异常,但在这个阶段我们已经找到了类

Exception due to the wrong invocation, nevertheless at this stage we already found the class

这篇关于在运行时加载jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆