`

How to know the main class of a jar file?

阅读更多

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.   /**  
  8.    * @param args  
  9.    */  
  10.   public static void main(String[] args) {   
  11.     try {   
  12.       JarFile jar_file = new JarFile(args[0]);   
  13.       if (jar_file != null) {   
  14.         Manifest manifest = jar_file.getManifest();   
  15.         if (manifest != null) {   
  16.           String value = manifest.getMainAttributes().getValue("Main-Class");   
  17.           if (value != null) {   
  18.             System.out.println(value.replaceAll("/""."));   
  19.             return;   
  20.           }   
  21.         }   
  22.       }   
  23.     } catch (Throwable e) {   
  24.       // ignore it   
  25.     }   
  26.     System.out.println("UNKNOWN");   
  27.     System.exit(1);   
  28.   }   
  29.      
  30. }  
分享到:
评论

相关推荐

    How to run a JAR file

    How to run a JAR file

    abe.jar工具,解决Don't know how to process version 5

    在遇到“Don't know how to process version 5”的错误时,这通常意味着Abe.jar不支持当前APK的版本格式,这可能是由于APK格式的更新或Abe.jar自身的版本过旧。 首先,让我们深入了解一下Java和Android的关系。Java...

    Getting to Know Vue js

    You’ll also explore how to use Single File Components and the Vue.js Command Line Interface (CLI) to build components in a single file and add in build tools as you see fit. Getting started with a ...

    How_to_write_and_publish_a_scientific_paper

    How to write and publish a scientific paper ContentsChapter 1 What Is Scientific Writing? Chapter 2 Origins of Scientific Writing Chapter 3 What Is a Scientific Paper? Chapter 4 How to Prepare the ...

    how to make sense of any mess

    How to Make Sense of Any Mess: Information Architecture for Everybody Abby Covert Everything is getting more complex. It is easy to be overwhelmed by the amount of information we encounter each ...

    C++ How to Program, 10th Edition

    C++ How to Program presents leading-edge computing technologies in a friendly manner appropriate for introductory college course sequences, based on the curriculum recommendations of two key ...

    数据结构作业Hash表

    To complete this assessment, you need to complete the implementation of class Dictionary and complete the spell checking program contained in main.cpp. Following is an ordered list of steps that ...

    2012年MCM A题outstanding

    参赛资料指导PROBLEM A: The Leaves of a Tree "How much do the leaves on a tree weigh?" How might one estimate the actual weight of the leaves (or for that matter any other parts of the tree)? How might...

    Java邮件开发Fundamentals of the JavaMail API

    The first part of the API is the focus of this course --basically, how to send and receive messages independent of the provider/protocol. * The second part speaks the protocol-specific languages, ...

    MAKING THE CASE: How to Justify the Cost of a Rapid Prototyping System

    To designers, engineers and product... Yet, in spite of the obvious value, it may not be clear how to convince the management and accounting departments that the benefits justify the capital expenditure.

    Think Perl 6: How to Think Like a Computer Scientist

    Think Perl 6: How to Think Like a Computer Scientist by Laurent Rosenfeld English | 8 May 2017 | ASIN: B0716P9W11 | 466 Pages | AZW3 | 1.02 MB Want to learn how to program and think like a computer ...

    how to solve it

    George Polya revealed how the mathematical method of demonstrating a proof or finding an unknown can be of help in attacking any problem that can be "reasoned" out - from building a bridge to winning...

    How To Read A Book.pdf

    How to read a book efficiently. Part one The dimensions of reading Part two The third level of reading Part three Approaches to different kinds of reading Part four The ultimate goals of reading

    How to use SFTP

    The topic How to use SFTP (with client validation - password authentication) discusses the simplest form of client authentication, via password. In public key authentication, SSH clients and ...

    Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript[EPUB版]

    You’ll learn how to choose the right programming style for each project, manage unanticipated problems, and work more successfully with every facet of JavaScript programming from data structures to ...

Global site tag (gtag.js) - Google Analytics