[英]Creating Jar and loading classes in it
I need to create a jar and load classes inside jar using classLoader. 我需要创建一个jar并使用classLoader在jar中加载类。 I use following code to create folder structure inside jar.This works fine but when i try to load class from jar it throws exception ClassNotFound. 我使用以下代码在jar中创建文件夹结构。这很好,但是当我尝试从jar中加载类时会抛出ClassNotFound异常。
public void createJar() throws IOException
{
//FileOutputStream stream = new FileOutputStream("D:\\MyFolder\\apache-tomcat-6.0.32\\webapps\\SpringTest\\WEB-INF\\lib\\serviceJar.jar");
FileOutputStream stream = new FileOutputStream("D:/MyFolder/apache-tomcat-6.0.32/webapps/SpringTest/WEB-INF/lib/serviceJar.jar");
JarOutputStream target = new JarOutputStream(stream, new Manifest());
add(new File("D:/myJar"), target);
target.close();
stream.close();
}
private void add(File source, JarOutputStream target) throws IOException
{
byte buffer[] = new byte[10204];
try
{
if (source.isDirectory())
{
String name = source.getPath().replace("\\", "/");
if (!name.isEmpty())
{
if (!name.endsWith("/"))
name += "/";
JarEntry entry = new JarEntry(name);
entry.setTime(source.lastModified());
target.putNextEntry(entry);
target.closeEntry();
}
for (File nestedFile: source.listFiles())
add(nestedFile, target);
return;
}
JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
// JarEntry entry = new JarEntry(source.getPath());
entry.setTime(source.lastModified());
target.putNextEntry(entry);
FileInputStream in = new FileInputStream(source);
while (true) {
int nRead = in.read(buffer, 0, buffer.length);
if (nRead <= 0)
break;
target.write(buffer, 0, nRead);
}
in.close();
target.closeEntry();
}
catch(Exception e){
}
finally
{
}
}
Classes to be jared are at location "D:\\myJar\\spring\\controller\\MyController.class" and " D:\\myJar\\spring\\service\\MyService.class".So jar is created as I want and also it contains folder structure(Mycontroller.class is inside jar at location \\myJar\\spring\\controller).But when i try to load it with following code I get exception 要震击的类位于“ D:\\ myJar \\ spring \\ controller \\ MyController.class”和“ D:\\ myJar \\ spring \\ service \\ MyService.class”的位置。因此,可以根据需要创建jar并包含文件夹结构(Mycontroller.class在jar里面的\\ myJar \\ spring \\ controller内)。但是当我尝试使用以下代码加载它时,出现异常
public void loadJar() {
//File root = new File("D:\\MyFolder\\apache-tomcat-6.0.32\\webapps\\SpringTest\\WEB-INF\\lib\\serviceJar.jar");//
File root = new File("D:/MyFolder/apache-tomcat-6.0.32/webapps/SpringTest/WEB-INF/lib/serviceJar.jar");//
URLClassLoader classLoader;
try {
classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
Class<?> cls = Class.forName("myJar.spring.controller.MyController", true, classLoader);
Object instance = cls.newInstance(); // Should print constructor content
System.out.println(instance);
Method m =cls.getMethod("printThis", null);
m.invoke(instance, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But if I create jar without package structure inside it.It works fine. 但是如果我创建的jar里面没有包结构,那就可以了。 I have checked all my paths and they are correct.Also location of class file inside Jar is correct. 我已经检查了所有路径,它们都是正确的.Jar中类文件的位置也是正确的。 Please explain why am I getting ClassNotFound Exception .Also suggest if any change is required in createJar() or add() code. 请解释为什么我会收到ClassNotFound Exception。还建议在createJar()或add()代码中是否需要任何更改。
You may need to add class-path on the go, the following link could be of help. 您可能需要随时添加类路径,以下链接可能会有所帮助。
How do you change the CLASSPATH within Java? 您如何在Java中更改CLASSPATH?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.