Java 使用onejar插件的反射
我正在使用,运行生成的jar时出现问题: 20204[main]ERROR org.reflections.reflections-给定的扫描URL为 空的。在配置中设置URL 在我看来,反射不支持onejar正在使用的类加载器。我一直在代码中使用以下Java 使用onejar插件的反射,java,classloader,Java,Classloader,我正在使用,运行生成的jar时出现问题: 20204[main]ERROR org.reflections.reflections-给定的扫描URL为 空的。在配置中设置URL 在我看来,反射不支持onejar正在使用的类加载器。我一直在代码中使用以下反射配置: Reflections reflections = new Reflections("path.to.my.package"); 有什么想法吗?谢谢。是的,这是一个已知的问题。有一个工作围绕着 使用自定义UrlType和自定义Vfs.
反射配置
:
Reflections reflections = new Reflections("path.to.my.package");
有什么想法吗?谢谢。是的,这是一个已知的问题。有一个工作围绕着 使用自定义UrlType和自定义Vfs.Dir;使用OneJarURLConnection从嵌入的JAR中正确读取内容;使用“标准”Vfs.File 此处讨论的问题和解决方案: 来源:如果您需要: 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);
}
}
}
谢谢这个看起来不错。将在需要时稍后尝试。
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);
}
}
}