Use URLClassLoader load jar files into classpath

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class LoadJarClassloader {
private static final String PROTOCAL_PREFIX = "file:///";

public static ClassLoader getProjectClassLoader(String[] classPaths) throws MalformedURLException {

// compute the project classpaths
// REVIEW: Are the classpaths returned by computeDefaultRuntimeClassPath
// enough to load class?
URL[] urls = new URL[classPaths.length];
for (int i = 0; i < classPaths.length; i++) {
urls[i] = new URL(PROTOCAL_PREFIX + computeForURLClassLoader(classPaths[i]));
}
return new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
}

/**
* load <code>Class</code> in java project
*
* @param project
* <code>IJavaProject</code>
* @param className
* name of class to load
* @return <code>Class</code>
* @throws ClassNotFoundException
* @throws CoreException
* @throws MalformedURLException
*/
public static Class loadClass(String className) throws ClassNotFoundException, MalformedURLException {
ClassLoader loader = getProjectClassLoader(new String[] { "E:\\Projects\\LoadJar\\lib\\commons-dbcp-1.2.1.jar",
"E:\\Projects\\LoadJar\\lib\\02182701940.jar" });
Class clazz = loader.loadClass(className);
loader = null;
return clazz;
}
public static ClassLoader getClassLoader() throws ClassNotFoundException, MalformedURLException {
ClassLoader loader = getProjectClassLoader(new String[] { "E:\\Projects\\LoadJar\\lib\\commons-dbcp-1.2.1.jar",
"E:\\Projects\\LoadJar\\lib\\mysql-connector-java-3.0.14-mikuni-bin.jar" });
// Class clazz = loader.loadClass(className);
// loader = null;
return loader;
}
public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
Class clazz = loadClass("com.mysql.jdbc.Driver");
Driver driver = (Driver)clazz.newInstance();
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "reversea1";
// 加載驅動程序以連接數據庫
// Class.forName("com.mysql.jdbc.Driver",true,getClassLoader());
Properties info = new Properties();
info.setProperty("user", username);
info.setProperty("password", password);
Connection connection = driver.connect(url, info );

System.out.println(connection.toString());
}

/**
* transform the <code>IType</code> to <code>Class</code>
*
* @param type
* <code>IType</code>
* @return <code>Class</code>
* @throws ClassNotFoundException
* @throws MalformedURLException
*/
// public static Class typeToClass(IType type)
// ClassNotFoundException,MalformedURLException {
// if (null != type && (type.isClass() || type.isInterface())) {
// String className = type.getFullyQualifiedName('$');
// return loadClass(type.getJavaProject(), className);
// }
//
// return null;
// }
/**
* because of URLClassLoader assumes any URL not ends with '/' to refer to a
* jar file. so we need to append '/' to classpath if it is a folder but not
* ends with '/'.
*
* @param classpath
* @return
*/
private static String computeForURLClassLoader(String classpath) {
if (!classpath.endsWith("/")) {
File file = new File(classpath);
if (file.exists() && file.isDirectory()) {
classpath = classpath.concat("/");
}
}
return classpath;
}
}
發佈了26 篇原創文章 · 獲贊 0 · 訪問量 1848
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章