Here you can find the source of loadJarFile(File jarFile, String className)
Parameter | Description |
---|---|
jarFile | jar file to search |
className | class name to load |
Parameter | Description |
---|---|
Exception | if any problem |
@SuppressWarnings("unchecked") public static final <T> T loadJarFile(File jarFile, String className) throws Exception
//package com.java2s; /*//w w w .jav a2s. c om * BeanGenerator * * Copyright (C) 2011 galaxyworld.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.io.File; import java.net.URL; import java.net.URLClassLoader; public class Main { /** * Loads class <i>className</i> from the jar file <i>jarFile</i>. * * @param jarFile jar file to search * @param className class name to load * @return new class instance * @throws Exception if any problem */ @SuppressWarnings("unchecked") public static final <T> T loadJarFile(File jarFile, String className) throws Exception { URL[] urls = new URL[] { jarFile.toURI().toURL() }; URLClassLoader classLoader = new URLClassLoader(urls, Thread.currentThread().getContextClassLoader()); Class<?> cls = classLoader.loadClass(className); return (T) cls.newInstance(); } }