使用自定义类加载器加载资源包

发布于 2024-12-12 11:04:12 字数 1706 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗地喜欢 2024-12-19 11:04:12

您需要重写并实现 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文