获取jar包路径,遍历

import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;



import com.genuitec.eclipse.j2eedt.taglib.DirTaglibInfo;

public class T {
public static void main(String[] args) throws IOException {
        
    URL url = DirTaglibInfo.class.getProtectionDomain().getCodeSource().getLocation();
    String filePath = URLDecoder.decode(url.getPath(), "UTF-8");
    if(filePath.endsWith(".jar"))
    System.out.println(filePath);
    
        //java.util.jar.JarFile file = new JarFile("E:\\frame\\jdbc\\mysql.jar");
    java.util.jar.JarFile file = new JarFile(filePath);
        Enumeration<JarEntry> entrys = file.entries();
        while(entrys.hasMoreElements()){
            JarEntry jar = entrys.nextElement();
            System.out.println(jar.getName());
        }
        
        
        file.close();
        
        
        
      //filePath= filePath.substring(0, filePath.lastIndexOf("/") + 1);
        
    }
}

转载于:https://my.oschina.net/dlpinghailinfeng/blog/123528

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java中的反射机制来获取Jar包中指定包路径下的类。以下是一个示例代码: ```java import java.io.IOException; import java.net.JarURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class JarUtils { public static List<Class<?>> getClassesFromJar(String jarFilePath, String packagePath) throws IOException, ClassNotFoundException { List<Class<?>> classList = new ArrayList<>(); JarFile jarFile = new JarFile(jarFilePath); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry jarEntry = entries.nextElement(); String name = jarEntry.getName().replace("/", "."); if (name.startsWith(packagePath) && name.endsWith(".class")) { String className = name.substring(0, name.length() - 6); Class<?> clazz = Class.forName(className); classList.add(clazz); } } return classList; } public static void main(String[] args) throws IOException, ClassNotFoundException { String jarFilePath = "/path/to/your/jar/file.jar"; String packagePath = "com.example.package"; List<Class<?>> classList = getClassesFromJar(jarFilePath, packagePath); for (Class<?> clazz : classList) { System.out.println(clazz.getName()); } } } ``` 在上面的示例代码中,`getClassesFromJar` 方法接收两个参数:`jarFilePath` 表示 Jar 包文件的路径,`packagePath` 表示要查找的包路径。该方法会遍历 Jar 包中的所有类,找到指定包路径下的类,并将其添加到 `classList` 列表中返回。你可以在 `main` 方法中调用该方法,并打印出每个找到的类的完整类名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值