使用自定义类加载器加载资源包
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class JarClassLoader extends ClassLoader {
private String path;
public JarClassLoader(String path) {
this.path = path;
}
@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
{
Class<?> c = findLoadedClass(name);
if (c == null) {
try {
c = findSystemClass(name);
} catch (Exception e) {
}
if (c != null)
return c;
try {
byte data[] = loadClassData(name);
c = defineClass(name, data, 0, data.length);
if (c == null)
throw new ClassNotFoundException(name);
if (resolve)
resolveClass(c);
} catch (Exception e) {
e.printStackTrace();
}
}
return c;
}
private byte[] loadClassData (String classEntry) throws IOException {
System.out.println(classEntry);
String filename = classEntry.replace('.', File.separatorChar) + ".class";
JarFile jar = new JarFile(path);
JarEntry entry = jar.getJarEntry(filename);
InputStream is = jar.getInputStream(entry);
int data;
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
while ((data = is.read()) != -1) {
byteStream.write(data);
}
return byteStream.toByteArray();
}
}
我有一个类加载器(上面发布)来从 jar 中加载我需要的所有类文件。它还包含 MySQL java 驱动程序文件,当它尝试加载这些文件时,我在 com.mysql.jdbc.LocalizedErrorMessages 上收到错误,因为这是一个 ResourceBundle。我的问题是如何使用自定义类加载器加载资源包?感谢您的帮助。
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class JarClassLoader extends ClassLoader {
private String path;
public JarClassLoader(String path) {
this.path = path;
}
@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
{
Class<?> c = findLoadedClass(name);
if (c == null) {
try {
c = findSystemClass(name);
} catch (Exception e) {
}
if (c != null)
return c;
try {
byte data[] = loadClassData(name);
c = defineClass(name, data, 0, data.length);
if (c == null)
throw new ClassNotFoundException(name);
if (resolve)
resolveClass(c);
} catch (Exception e) {
e.printStackTrace();
}
}
return c;
}
private byte[] loadClassData (String classEntry) throws IOException {
System.out.println(classEntry);
String filename = classEntry.replace('.', File.separatorChar) + ".class";
JarFile jar = new JarFile(path);
JarEntry entry = jar.getJarEntry(filename);
InputStream is = jar.getInputStream(entry);
int data;
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
while ((data = is.read()) != -1) {
byteStream.write(data);
}
return byteStream.toByteArray();
}
}
I have a ClassLoader (posted above) to load all the class files I need out of a jar. That also contains the MySQL java driver files, and when it's trying to load those I get an error on com.mysql.jdbc.LocalizedErrorMessages because this is a ResourceBundle. My question is how to I load resource bundles using a custom class loader? Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要重写并实现 findResource()/findResources() 方法。这些方法在加载资源包属性文件和其他非类文件时使用。
You'll need to override and implement findResource()/findResources() methods. These methods are used when loading resource bundle property files and other non-class files.