2011-12-16 20 views
11

मैं URLClassLoader का उपयोग कर बाहरी जार फ़ाइल को लोड और निष्पादित करना चाहता हूं।जावा से एक जार फ़ाइल के मुख्य वर्ग का नाम कैसे प्राप्त करें?

इससे "मुख्य श्रेणी" प्राप्त करने का सबसे आसान तरीका क्या है?

+3

मुझे यकीन नहीं है कि आप जो खोज रहे हैं, लेकिन यदि जार सही ढंग से रचित है, तो आप उम्मीद करेंगे कि जार में एक मेनिफेस्ट फ़ाइल है, जो निर्दिष्ट करता है कि मुख्य वर्ग कौन सा है। इस प्रकार आपको इसे समझने की आवश्यकता नहीं होगी। – hovanessyan

+0

मैं उस फ़ाइल को मैन्युअल पढ़ने के बिना सरल तरीकों की तलाश में हूं। क्या 'जावा -जर' कुछ लाइब्रेरी फ़ंक्शन का उपयोग मैनिफेस्ट से गुण पढ़ने के लिए करता है? –

+0

मुझे नहीं पता, यह संभावना है कि यह मामला है। आधिकारिक डॉक्टर केवल कहता है: मैनेजर में मेन-क्लास हेडर सेट करने के बाद, आप जावा कमांड के निम्न रूप का उपयोग करके JAR फ़ाइल चलाते हैं: जावा -जर जार-नाम निर्दिष्ट कक्षा का मुख्य तरीका मुख्य श्रेणी के शीर्षलेख में निष्पादित किया जाता है। यदि आप उस जार में मैनिफेस्ट की उपस्थिति को लागू कर सकते हैं, तो यह जाने का सबसे आसान (और शायद बेहतर) तरीका होगा। – hovanessyan

उत्तर

5

से here - Listing the main attributes of a jarfile

import java.util.*; 
import java.util.jar.*; 
import java.io.*; 

public class MainJarAtr{ 
    public static void main(String[] args){ 
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
     try { 
      System.out.print("Enter jar file name: "); 
      String filename = in.readLine(); 
      if(!filename.endsWith(".jar")){ 
       System.out.println("File not in jar format."); 
       System.exit(0); 
      } 

      File file = new File(filename); 
      if (file.exists()){ 
       // Open the JAR file 
       JarFile jarfile = new JarFile(filename); 

       // Get the manifest 
       Manifest manifest = jarfile.getManifest(); 

       // Get the main attributes in the manifest 
       Attributes attrs = (Attributes)manifest.getMainAttributes(); 

       // Enumerate each attribute 
       for (Iterator it=attrs.keySet().iterator(); it.hasNext();) { 
        // Get attribute name 
        Attributes.Name attrName = (Attributes.Name)it.next(); 
        System.out.print(attrName + ": "); 

        // Get attribute value 
        String attrValue = attrs.getValue(attrName); 
        System.out.print(attrValue); 
        System.out.println(); 
       } 
      } 
      else{ 
       System.out.print("File not found."); 
       System.exit(0); 
      } 
     } 
     catch (IOException e) {} 
    } 
} 
+1

'(-> (java.util.jar.JarFile।" Myjarfile.jar ") (.getManifest) (.getMainAttributes) (.getValue" मुख्य श्रेणी ") का उपयोग करना)' –

5

यह केवल तभी संभव होगा जब जार स्वयं निष्पादित हो; जो मामले में मुख्य वर्ग कुंजी Main-Class:

कुछ संदर्भ में जानकारी के साथ मैनिफ़ेस्ट फ़ाइल में निर्दिष्ट किया जाएगा यहाँ प्रदान की जाती है: http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

फिर आप इसे उपयोग करने java.util.JarFile का उपयोग jarfile डाउनलोड करने के लिए की आवश्यकता होगी; इस के लिए कुछ जावा कोड हो सकता है:

JarFile jf = new JarFile(new File("downloaded-file.jar")); 
if(jf.getManifest().getEntries().containsKey("Main-Class")) { 
    String mainClassName = jf.getManifest().getEntries().get("Main-Class"); 
} 
+0

हां, यह आत्म-निष्पादन है। मैं मैन्युअल रूप से विचार प्रकट फ़ाइल को पार्स किए बिना वह नाम प्राप्त करना चाहता हूं। –

6

मैं जानता हूँ कि यह एक पुराने सवाल है, लेकिन कम से कम JDK 1.7 के साथ, पहले से प्रस्तावित समाधान काम करने के लिए नहीं मालूम था। इस कारण से मैं मेरा पोस्टिंग कर रहा हूँ:

JarFile j = new JarFile(new File("jarfile.jar")); 
String mainClassName = j.getManifest().getMainAttributes().getValue("Main-Class"); 

कारण है कि अन्य समाधान के लिए मैं था काम नहीं किया क्योंकि j.getManifest().getEntries() को पता चला है मुख्य-क्लास विशेषता नहीं, कि बजाय द्वारा वापस सूची में शामिल किया गया था getMainAttributes() विधि।

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^