• Java读写Jar


    Java提供了读写jar的类库Java.util.jar,Java获取解析jar包的工具类如下:

    1. import java.io.File;
    2. import java.io.IOException;
    3. import java.net.URL;
    4. import java.net.URLClassLoader;
    5. import java.util.Enumeration;
    6. import java.util.HashMap;
    7. import java.util.HashSet;
    8. import java.util.Map;
    9. import java.util.Set;
    10. import java.util.jar.Attributes;
    11. import java.util.jar.JarEntry;
    12. import java.util.jar.JarFile;
    13. import java.util.jar.Manifest;
    14. /**
    15. * jar包解析器
    16. *
    17. */
    18. public class JarAnalyzer {
    19. /**
    20. * jar 文件路径
    21. */
    22. private String jarFilePath;
    23. /**
    24. * 构造函数
    25. *
    26. * @param jarFilePath 文件路径
    27. */
    28. public jarAnalyzer(String jarFilePath) {
    29. this.jarFilePath = jarFilePath;
    30. }
    31. /**
    32. * 获取jar包属性
    33. *
    34. * @return jar包所有属性
    35. * @throws IOException
    36. */
    37. public Map getJarAttrs() throws IOException {
    38. URL url = new File(this.jarFilePath).toURI().toURL();
    39. URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url});
    40. URL manifestUrl = urlClassLoader.findResource("META-INF/MANIFEST.MF");
    41. Manifest manifest = new Manifest(manifestUrl.openStream());
    42. Attributes mainAttributes = manifest.getMainAttributes();
    43. Map attrs = new HashMap<>();
    44. mainAttributes.forEach((key, value) -> {
    45. attrs.put(String.valueOf(key), String.valueOf(value));
    46. });
    47. return attrs;
    48. }
    49. /**
    50. * 获取入口类全路径名
    51. *
    52. * @return 入口类全路径名
    53. * @throws IOException
    54. */
    55. public String getProgamClass() throws IOException {
    56. for (String key : getJarAttrs().keySet()) {
    57. if ("program-class".equals(key)) {
    58. return getJarAttrs().get(key);
    59. }
    60. }
    61. return null;
    62. }
    63. /**
    64. * 获取jar包所有类名
    65. *
    66. * @return jar包所有类名
    67. * @throws IOException
    68. */
    69. public Set getAllClasses() throws IOException {
    70. File givenFile = new File(this.jarFilePath);
    71. Set classNames = new HashSet<>();
    72. try (JarFile jarFile = new JarFile(givenFile)) {
    73. Enumeration e = jarFile.entries();
    74. while (e.hasMoreElements()) {
    75. JarEntry jarEntry = e.nextElement();
    76. if (jarEntry.getName().endsWith(".class")) {
    77. String className = jarEntry.getName()
    78. .replace("/", ".")
    79. .replace(".class", "");
    80. classNames.add(className);
    81. }
    82. }
    83. return classNames;
    84. }
    85. }
    86. }

    测试类

    1. public static void main(String[] args) throws Exception {
    2. String jarPath = "/xxx/TopSpeedWindowing.jar";
    3. JarAnalyzer jarAnalyzer = new JarAnalyzer(jarPath);
    4. log.info("jar包所有属性:");
    5. jarAnalyzer.getJarAttrs().forEach((key, value) -> {
    6. log.info("key={},value={}", key, value);
    7. });
    8. log.info("MainClass -> {}", jarAnalyzer.getProgamClass());
    9. log.info("jar包含有的类:");
    10. jarAnalyzer.getAllClasses().forEach(clzz -> {
    11. log.info("className ->{}", clzz);
    12. });
    13. }

    测试解析结果

    参考文献:

    https://stackoverflow.com/questions/1272648/reading-my-own-jars-manifest

    https://www.baeldung.com/jar-file-get-class-names

  • 相关阅读:
    vscode快捷键大全中英文
    【开发环境】mysql5.7.xx win安装
    GitHub 开源大厂缓存架构 Redis 优化的文档被警告,900 页全是宝贝
    ip地址范围不是整体范围的子集
    随便看看官方教程-FPS Microgame
    五大步骤实现MapGIS Web 功能服务拓展
    学习如何将Jenkins与UI测试报告完美整合,事半功倍,轻松获取高薪职位!
    Fast Unsupervised Projection for Large-Scale Data
    YARN之Opportunistic Containers
    Redis主从复制的操作和配置
  • 原文地址:https://blog.csdn.net/xing_jian1/article/details/134491222