ITeye博客

How to know the main class of a jar file?

willpower · 2007-04-02 15:18 · Study 学习 · Markdown

Easy. Here is an implementation from Hadoop.

java 代码

  1. /**
  2. * A micro-application that prints the main class name out of a jar file.
  3. * @author Owen O'Malley
  4. */
  5. public class PrintJarMainClass {
  6. /**
  7. * @param args
  8. */
  9. public static void main(String[] args) {
  10. try {
  11. JarFile jar_file = new JarFile(args[0]);
  12. if (jar_file != null) {
  13. Manifest manifest = jar_file.getManifest();
  14. if (manifest != null) {
  15. String value = manifest.getMainAttributes().getValue("Main-Class");
  16. if (value != null) {
  17. System.out.println(value.replaceAll("/", "."));
  18. return;
  19. }
  20. }
  21. }
  22. } catch (Throwable e) {
  23. // ignore it
  24. }
  25. System.out.println("UNKNOWN");
  26. System.exit(1);
  27. }
  28. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。