gpt4 book ai didi

java - 通过 onejar 插件使用反射

转载 作者:行者123 更新时间:2023-11-30 04:34:09 25 4
gpt4 key购买 nike

我正在使用reflectionsonejar maven plugin ,并且我在运行生成的 jar 时遇到问题:

20204 [main] ERROR org.reflections.Reflections - given scan urls are empty. set urls in the configuration

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

Reflections reflections = new Reflections("path.to.my.package");

有什么想法吗?谢谢。

最佳答案

是的,一个已知问题。有一个解决办法...

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

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

如果需要的话来源:

OneJarDir

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

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);
}
}

}

关于java - 通过 onejar 插件使用反射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13909364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com