当我在NetBeans中运行我的项目时,它运行正常,但是当我尝试在导出它之后运行它时,我遇到了这个异常:
尝试加载模块时java.lang.RuntimeException: No resource for micro/main/plugins
。就像我说Netbeans一切正常,但出口后没有。
当我解压缩.jar时,文件和文件夹确实存在。我无法忍受。
这是我用来获取所有类的代码:
private static ArrayList<Class<?>> getClassesForPackage(Package pkg) {
String pkgname = pkg.getName();
ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
File directory = null;
String fullPath;
String relPath = pkgname.replace('.', '/');
URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
if (resource == null) {
throw new RuntimeException("No resource for " + relPath);
}
fullPath = resource.getFile();
try {
directory = new File(resource.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException("", e);
} catch (IllegalArgumentException e) {
directory = null;
}
if (directory != null && directory.exists()) {
String[] files = directory.list();
for (int i = 0; i < files.length; i++) {
if (files[i].endsWith(".class")) {
String className = pkgname + '.' + files[i].substring(0, files[i].length() - 6);
try {
classes.add(Class.forName(className));
} catch (ClassNotFoundException e) {
throw new RuntimeException("ClassNotFoundException loading " + className);
}
}
}
} else {
try {
String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
JarFile jarFile = new JarFile(jarPath);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String entryName = entry.getName();
if (entryName.startsWith(relPath) && entryName.length() > (relPath.length() + "/".length())) {
String className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
try {
classes.add(Class.forName(className));
} catch (ClassNotFoundException e) {
throw new RuntimeException("ClassNotFoundException loading " + className);
}
}
}
} catch (IOException e) {
throw new RuntimeException(pkgname + " (" + directory + ") does not appear to be a valid package", e);
}
}
return classes;
}
代码失败:
for(Class c : getClasses(Commands.class.getPackage())){//This line
if(c.getSuperclass().equals(Commands.class)){
try {
c.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}