微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用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("/",".");
            Classgetmethods();
        }
    }
    zipSteam.close();
}

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

原文地址:https://www.jb51.cc/java/437218.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐