比如我有一个jar文件,里面有一个A类,
jar文件放在c:\下面我在一个JAVA工程的B类中,要加载这个jar文件,并获取A类的实例和执行A的test()方法,该怎么实现,谢谢各位~
(前提不将这个jar包放在classpath)
jar文件放在c:\下面我在一个JAVA工程的B类中,要加载这个jar文件,并获取A类的实例和执行A的test()方法,该怎么实现,谢谢各位~
(前提不将这个jar包放在classpath)
public static Class<?> forName(String className)
throws ClassNotFoundException
Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:
Class.forName(className, true, currentLoader)
where currentLoader denotes the defining class loader of the current class.
For example, the following code fragment returns the runtime Class descriptor for the class named java.lang.Thread: Class t = Class.forName("java.lang.Thread")
A call to forName("X") causes the class named X to be initialized.
Parameters:
className - the fully qualified name of the desired class.
Returns:
the Class object for the class with the specified name.
Throws:
LinkageError - if the linkage fails
ExceptionInInitializerError - if the initialization provoked by this method fails
ClassNotFoundException - if the class cannot be located
public static StringBuffer readPackageResource(String packageResource)
{
if (packageResource == null)
return null;
try
{
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream is = loader.getResourceAsStream(packageResource);
if (is == null)
{
loader = FileUtil.class.getClassLoader();
is = loader.getResourceAsStream(packageResource);
}
BufferedReader bf = new BufferedReader(new InputStreamReader(is));
StringBuffer recordBuf = new StringBuffer();
String record = null;
while ((record = bf.readLine()) != null)
{
recordBuf.append(record + "\n");
}
bf.close();
is.close();
return recordBuf;
}
catch (Exception ex)
{
CommonLog.debug("readPackageResource(): " + ex.toString());
ex.printStackTrace();
}
return null;
}
我现在怎么实现创建一个实例来运行这个class中的某个方法呢?
或者说是,怎么获取这个class实例的引用呢?