加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 01:04:08 所属栏目:Java 来源:网络整理
导读:我试图检测jar中哪个类包含main或提供的方法名称(如果可能). 目前我有以下代码 public static void getFromJars(String pathToAppJar) throws IOException{ FileInputStream jar = new FileInputStream(pathToAppJar); ZipInputStream zipSteam = new ZipInp

我试图检测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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读