java代码检测jar_使用java代码检测jar内的main.

搜索热词

我试图检测jar中哪个类包含main或提供的方法名称(如果可能).

目前我有以下代码

public static void getFromJars(String pathToAppJar) throws IOException{

FileInputStream jar = new FileInputStream(pathToAppJar);

ZipInputStream zipSteam = new ZipInputStream(jar);

ZipEntry ze;

while ((ze = zipSteam.getNextEntry()) != null) {

System.out.println(ze.toString());

}

zipSteam.close();

}

这将允许我在这些包下获取包和类,但我不知道是否有可能在类中获取方法.

此外,我不知道这种方法是否适用于jar中的几个pkgs的情况,因为每个包都可以有一个带有main的类.

我很感激任何想法.

最佳答案

感谢fvu的评论,我最终得到了以下代码.

public static void getFromJars(String pathToAppJar) throws IOException,ClassNotFoundException

{

FileInputStream jar = new FileInputStream(pathToAppJar);

ZipInputStream zipSteam = new ZipInputStream(jar);

ZipEntry ze;

URL[] urls = { new URL("jar:file:" + pathToAppJar+"!/") };

URLClassLoader cl = URLClassLoader.newInstance(urls);

while ((ze = zipSteam.getNextEntry()) != null) {

// Is this a class?

if (ze.getName().endsWith(".class")) {

// Relative path of file into the jar.

String className = ze.getName();

// Complete class name

className = className.replace(".class","").replace("/",".");

Class> klazz = cl.loadClass(className);

Method[] methodsArray = klazz.getMethods();

}

}

zipSteam.close();

}

我删除了使用找到的方法的代码,因为这对于这个答案并不重要

相关文章

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值