首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用反射与onejar插件

使用反射与onejar插件
EN

Stack Overflow用户
提问于 2012-12-17 06:32:34
回答 1查看 2.2K关注 0票数 1

我将倒影onejar maven插件结合使用,在运行结果jar时遇到了问题:

20204主错误org.reflections.Reflections给定的扫描urls为空.在配置中设置urls

在我看来,反射似乎不支持onejar所使用的类加载器。我在代码中使用了以下Reflections配置:

代码语言:javascript
复制
Reflections reflections = new Reflections("path.to.my.package");

有什么想法吗?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-05 04:47:46

是的一个已知的问题。周围有个工作..。

使用自定义UrlType和自定义Vfs.Dir;使用OneJarURLConnection正确地从嵌入式JAR读取内容;使用“标准”Vfs.File。

此处讨论的问题和解决方案:http://code.google.com/p/reflections/issues/detail?id=128

如果您需要它,请提供:

OneJarDir

代码语言:javascript
复制
package com.sdl.ws.integration.profserv.shared.onejar;


import com.simontuffs.onejar.OneJarURLConnection;
import org.reflections.vfs.Vfs;
import org.reflections.vfs.ZipDir;
import org.reflections.vfs.ZipFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;


public class OneJarDir implements Vfs.Dir {

    private JarFile oneJarFile = null;
    private List<Vfs.File> oneJarClassFiles = new ArrayList<Vfs.File>();

    private OneJarURLConnection oneJarConnection;

    public OneJarDir(OneJarURLConnection oneJarConnection) {

        this.oneJarConnection = oneJarConnection;

        try {
            this.oneJarConnection.connect();
            this.oneJarFile = this.oneJarConnection.getJarFile();
            Enumeration<JarEntry> entries = oneJarFile.entries();

            while (entries.hasMoreElements()) {
                oneJarClassFiles.add(new ZipFile(new ZipDir(oneJarFile), entries.nextElement()));
            }

        } catch (IOException e) {
            throw new RuntimeException("Can't create One-Jar VFS directory", e);
        }
    }

    public String getPath() {
        return oneJarConnection.getURL().getPath();
    }

    public Iterable<Vfs.File> getFiles() {
        return oneJarClassFiles;
    }

    public void close() {
        try {
            if (oneJarConnection != null)
                oneJarConnection.getInputStream().close();
        } catch (IOException e) {
            throw new RuntimeException("Can't close VFS JAR stream", e);
        }
    }
}

OneJarUrlType

代码语言:javascript
复制
package com.sdl.ws.integration.profserv.shared.onejar;

import com.simontuffs.onejar.OneJarURLConnection;
import org.reflections.vfs.Vfs;

import java.io.IOException;
import java.net.URL;

public class OneJarUrlType implements Vfs.UrlType {

    private static final String _JAR_DIR = "jar!";

    public boolean matches(URL url) {

        // check if "double-jarred' by one-jar; this would appear to conflict with the standard JAR loader, so it either needs to be first (which it is)
        // OR the standard needs to be removed. This match assumes a nested JAR, unlike the standard JAR type.
        String externalForm = url.toExternalForm();

        // ugly, but should be much faster than regex.
        int idx1 = externalForm.indexOf(_JAR_DIR);
        return (idx1 > 0 && externalForm.indexOf(_JAR_DIR, idx1 + _JAR_DIR.length()) > 0);
    }

    public Vfs.Dir createDir(URL url) {
        try {
            return new OneJarDir(new OneJarURLConnection(url));
        } catch (IOException e) {
            throw new RuntimeException("Can't open One-Jar embedded JAR", e);
        }
    }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13909364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档