作为标题,
我基本上具有如下所示的结构:Structure of the Project
有两个基本文件夹:com和JsonJ,带有jar文件。
我如何获取作为库的jar文件,并在代码的帮助下将其作为库添加到项目中?
如果您的问题是加载JAR,然后从中加载类,则必须创建一个新的URLClassLoader
实例,然后使用该实例来加载您的类。这是一个示例方法。
public class AddToClasspathAndLoad{
public static void main( String[] args ) throws MalformedURLException{
URL[] urls = new URL[] { new URL( "file:///<absolute-path-to-jar-or-folder>" ) };
URLClassLoader cl = ( (URLClassLoader) AddToClasspathAndLoad.class.getClassLoader() ).newInstance( urls );
tryIt( cl );
}
private static void tryIt(URLClassLoader cl) {
try {
Class<?> c = cl.loadClass( "<fully-qualified-class-name>" );
System.out.println( c.getName() );
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}