解决了jar包无法扫描问题

This commit is contained in:
Biubiu
2020-01-15 10:29:57 +08:00
parent 4e968bcd08
commit 3147d9f9b7
4 changed files with 125 additions and 86 deletions

View File

@@ -1,24 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="Spring" name="Spring">
<configuration />
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: cglib:cglib:3.1" level="project" />
<orderEntry type="library" name="Maven: org.ow2.asm:asm:4.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>

View File

@@ -6,7 +6,7 @@
<name>javafx-plus</name>
<groupId>com.gitee.Biubiuyuyu</groupId>
<artifactId>javafx-plus</artifactId>
<version>1.0.0-RELEASE</version>
<version>1.1.1-RELEASE</version>
<packaging>jar</packaging>

View File

@@ -1,12 +1,13 @@
package cn.edu.scau.biubiusuisui.utils;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* @Author jack
@@ -24,6 +25,9 @@ public class ClassUtils {
URL url = classLoader.getResource(splashPath);
String filePath = StringUtils.getRootPath(url);
List<String> names = null;
if(filePath.endsWith("jar")){
nameList = readFromJarDirectory(filePath,base);
}else {
names = readFromDirectory(filePath);
for (String name : names) {
if (isClassFile(name)) {
@@ -32,6 +36,7 @@ public class ClassUtils {
nameList = getAllFXControllerClassName(base + "." + name, nameList);
}
}
}
return nameList;
}
@@ -55,6 +60,37 @@ public class ClassUtils {
}
private static List<String> readFromDirectory(String path) {
if (path == null) return null;
return readFromFileDirectory(path);
}
private static List<String> readFromJarDirectory(String path,String packageName) {
JarFile jarFile = null;
try {
jarFile = new JarFile(path);
} catch (IOException e) {
e.printStackTrace();
}
Enumeration<JarEntry> entrys = jarFile.entries();
List<String> classNames = new ArrayList<>();
while (entrys.hasMoreElements()) {
JarEntry jarEntry = entrys.nextElement();
if(!jarEntry.getName().endsWith(".class"))continue;
int packageNameIndex = jarEntry.getName().indexOf("/");
if("".equals(packageName)){
classNames.add(jarEntry.getName());
}else {
if(packageNameIndex == -1) continue;
String baseName = jarEntry.getName().substring(0, packageNameIndex);
if (baseName.equals(packageName)) {
classNames.add(StringUtils.trimExtension(jarEntry.getName()).replaceAll("/","."));
}
}
}
return classNames;
}
private static List<String> readFromFileDirectory(String path) {
File file = new File(path);
String[] names = file.list();
if (null == names) {
@@ -107,8 +143,6 @@ public class ClassUtils {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
// Field field1 = targetClass.getField(field.getName());
}
}
}

View File

@@ -0,0 +1,29 @@
package cn.edu.scau.biubiusuisui.utils;
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 ClassUtilsTest {
public static void main(String[] args) {
JarFile jarFile = null;
try {
jarFile = new JarFile("/Users/Jack/test.jar");
} catch (IOException e) {
e.printStackTrace();
}
Enumeration<JarEntry> entrys = jarFile.entries();
List<String> classNames = new ArrayList<>();
while (entrys.hasMoreElements()) {
JarEntry jarEntry = entrys.nextElement();
classNames.add(jarEntry.getName());
}
classNames.forEach(System.out::println);
}
}