2009-12-31 11 views
113

से है, मैं अभी एडीई के सामने नहीं हूं, बस एपीआई चश्मा देख रहा हूं।निर्धारित करें कि कौन सी जेएआर फ़ाइल एक कक्षा

CodeSource src = MyClass.class.getProtectionDomain().getCodeSource(); 
if (src != null) { 
    URL jar = src.getLocation(); 
} 

मैं यह निर्धारित करना चाहता हूं कि कौन सी जेएआर फ़ाइल कक्षा से है। क्या ऐसा करने का यह तरीका है?

+0

वहाँ कंसोल से ऐसा करने के लिए कोई तरीका है? कुछ 'java -findjar -cp /some/path/with/libs/*.jar my.java.Class' -> 'my.jar' जैसा कुछ। – kub1x

उत्तर

148

हां। यह बूटस्ट्रैप क्लासलोडर द्वारा लोड की गई कक्षाओं को छोड़कर सभी वर्गों के लिए काम करता है। निर्धारित करने के लिए दूसरा रास्ता है:

Class klass = String.class; 
URL location = klass.getResource('/' + klass.getName().replace('.', '/') + ".class"); 

रूप notnoop ने बताया getProtectionDomain().getCodeSource().getLocation() विधि वर्ग फ़ाइल के ही स्थान देता है। उदाहरण के लिए:

jar:file:/jdk/jre/lib/rt.jar!/java/lang/String.class 
file:/projects/classes/pkg/MyClass$1.class 

klass.getResource() विधि जार फ़ाइल या CLASSPATH

file:/Users/home/java/libs/ejb3-persistence-1.0.2.GA.jar 
file:/projects/classes 
+0

यह क्लास नाम से क्लास फ़ाइल में मैपिंग को धारण करता है। क्या यह अनाम कक्षाओं के लिए ठीक से काम करेगा? नेस्टेड कक्षाएं? –

+1

यह कक्षा के यूआरएल को जार खुद ही नहीं बताता है। जार फ़ाइल खोजने के लिए यूआरएल को पार्स किया जाना चाहिए। – notnoop

+0

@notnoop। मैंने जवाब स्पष्ट किया है। –

11

चेकआउट Lombok Patcher LiveInjector.java से LiveInjector.findPathJar() के स्थान देता है। ध्यान दें कि यह विशेष मामले हैं जहां फ़ाइल वास्तव में एक जार में नहीं रहती है, और आप इसे बदलना चाहेंगे।

/** 
* If the provided class has been loaded from a jar file that is on the local file system, will find the absolute path to that jar file. 
* 
* @param context The jar file that contained the class file that represents this class will be found. Specify {@code null} to let {@code LiveInjector} 
*    find its own jar. 
* @throws IllegalStateException If the specified class was loaded from a directory or in some other way (such as via HTTP, from a database, or some 
*        other custom classloading device). 
*/ 
public static String findPathJar(Class<?> context) throws IllegalStateException { 
    if (context == null) context = LiveInjector.class; 
    String rawName = context.getName(); 
    String classFileName; 
    /* rawName is something like package.name.ContainingClass$ClassName. We need to turn this into ContainingClass$ClassName.class. */ { 
     int idx = rawName.lastIndexOf('.'); 
     classFileName = (idx == -1 ? rawName : rawName.substring(idx+1)) + ".class"; 
    } 

    String uri = context.getResource(classFileName).toString(); 
    if (uri.startsWith("file:")) throw new IllegalStateException("This class has been loaded from a directory and not from a jar file."); 
    if (!uri.startsWith("jar:file:")) { 
     int idx = uri.indexOf(':'); 
     String protocol = idx == -1 ? "(unknown)" : uri.substring(0, idx); 
     throw new IllegalStateException("This class has been loaded remotely via the " + protocol + 
       " protocol. Only loading from a jar on the local file system is supported."); 
    } 

    int idx = uri.indexOf('!'); 
    //As far as I know, the if statement below can't ever trigger, so it's more of a sanity check thing. 
    if (idx == -1) throw new IllegalStateException("You appear to have loaded this class from a local jar file, but I can't make sense of the URL!"); 

    try { 
     String fileName = URLDecoder.decode(uri.substring("jar:file:".length(), idx), Charset.defaultCharset().name()); 
     return new File(fileName).getAbsolutePath(); 
    } catch (UnsupportedEncodingException e) { 
     throw new InternalError("default charset doesn't exist. Your VM is borked."); 
    } 
} 
+2

यह कुछ बहुत आसान पाने के लिए अत्यधिक जटिल लगता है। मैं बस बैठ गया और मैंने जो पाया उसे आजमाया, और ऐसा लगता है कि यह काम करता है। बस कुछ सत्यापन चाहता था। –

+0

ठीक है। आपका कोड बूटक्लास पथ में फ़ाइलों को संभाल नहीं करता है, और चंद्र का समाधान फ़ाइल में यूआरएल को जार फ़ाइल पर नहीं लौटाता है, इसलिए आपको जार फ़ाइल खोजने के लिए पथ को पार्स करना होगा। – notnoop

0
private String resourceLookup(String lookupResourceName) { 



    try { 

     if (lookupResourceName == null || lookupResourceName.length()==0) { 
      return ""; 
     } 
     // "/java/lang/String.class" 

     // Check if entered data was in java class name format 
     if (lookupResourceName.indexOf("/")==-1) { 
      lookupResourceName = lookupResourceName.replaceAll("[.]", "/"); 
      lookupResourceName = "/" + lookupResourceName + ".class"; 
     } 

     URL url = this.getClass().getResource(lookupResourceName); 
     if (url == null) { 
      return("Unable to locate resource "+ lookupResourceName); 

     } 

     String resourceUrl = url.toExternalForm(); 

     Pattern pattern = 
      Pattern.compile("(zip:|jar:file:/)(.*)!/(.*)", Pattern.CASE_INSENSITIVE); 

     String jarFilename = null; 
     String resourceFilename = null; 
     Matcher m = pattern.matcher(resourceUrl); 
     if (m.find()) { 
      jarFilename = m.group(2); 
      resourceFilename = m.group(3); 
     } else { 
      return "Unable to parse URL: "+ resourceUrl; 

     } 

     if (!jarFilename.startsWith("C:")){ 
      jarFilename = "/"+jarFilename; // make absolute path on Linux 
     } 

     File file = new File(jarFilename); 
     Long jarSize=null; 
     Date jarDate=null; 
     Long resourceSize=null; 
     Date resourceDate=null; 
     if (file.exists() && file.isFile()) { 

      jarSize = file.length(); 
      jarDate = new Date(file.lastModified()); 

      try { 
       JarFile jarFile = new JarFile(file, false); 
       ZipEntry entry = jarFile.getEntry(resourceFilename); 
       resourceSize = entry.getSize(); 
       resourceDate = new Date(entry.getTime()); 
      } catch (Throwable e) { 
       return ("Unable to open JAR" + jarFilename + " "+resourceUrl +"\n"+e.getMessage()); 

      } 

      return "\nresource: "+resourceFilename+"\njar: "+jarFilename + " \nJarSize: " +jarSize+" \nJarDate: " +jarDate.toString()+" \nresourceSize: " +resourceSize+" \nresourceDate: " +resourceDate.toString()+"\n"; 


     } else { 
      return("Unable to load jar:" + jarFilename+ " \nUrl: " +resourceUrl); 

     } 
    } catch (Exception e){ 
     return e.getMessage(); 
    } 


} 
+0

उपर्युक्त कोड पथ पर कोई संसाधन पाएगा। यदि एक जार में जार को जार के आकार और दिनांक और जार के भीतर संसाधन के आकार और दिनांक को प्रिंट किया जाएगा – Don

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