निर्धारित करें कि किस JAR फ़ाइल से एक वर्ग है


154

मैं अभी एक आईडीई के सामने नहीं हूं, बस एपीआई स्पेक्स को देख रहा हूं।

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

मैं यह निर्धारित करना चाहता हूं कि JAR फाइल किस क्लास से है। क्या ऐसा करने का तरीका है?


2
क्या कंसोल से ऐसा करने का कोई तरीका है? कुछ इस तरह java -findjar -cp /some/path/with/libs/*.jar my.java.Class-> my.jar
kub1x

जवाबों:


191

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

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

जैसा कि बताया गया है कि klass.getResource()विधि ने वर्ग फ़ाइल के स्थान को ही लौटा दिया है। उदाहरण के लिए:

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

getProtectionDomain().getCodeSource().getLocation()विधि जार फ़ाइल या CLASSPATH के स्थान देता है

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

यह मान्यताओं को क्लास के नाम से क्लास फ़ाइल में मैपिंग को समाप्त कर देता है। क्या यह अनाम कक्षाओं के लिए ठीक से काम करेगा? नेस्टेड क्लासेस?
थोरबजोरन रावन एंडरसन

1
यह वर्ग के उर की ओर इशारा करता है न कि स्वयं जार। जार को जार फ़ाइल खोजने के लिए पार्स करने की आवश्यकता है।
notnoop

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

19
यह ठीक उसी तरह से दूसरा तरीका है जैसा कि उत्तर में लिखा गया है, getProtectionDomain().getCodeSource().getLocation()अगर आप जार-फाइल लोकेशन पाना चाहते हैं तो उपयोग करें
peter

इस उत्तर के लिए धन्यवाद, इसने मुझे इस प्रश्न का उत्तर देने के लिए प्रेरित किया ।
क्रिएगाक्स

12

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

/**
 * 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
यह अति सरल कुछ पाने के लिए जटिल लगता है। मैं बस बैठ गया और कोशिश की कि मुझे पहले क्या मिला, और यह काम करने लगता है। बस कुछ मान्यता चाहिए थी।

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

2

उपयोग

String path = <Any of your class within the jar>.class.getProtectionDomain().getCodeSource().getLocation().getPath(); 

यदि इसमें एक से अधिक प्रविष्टियाँ हैं, तो कुछ प्रतिस्थापन क्रिया करें।


1
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();
    }


}

उपरोक्त कोड पथ पर किसी भी संसाधन को ढूंढेगा। अगर जार में जार को जार का आकार और दिनांक प्रिंट करने और जार के भीतर संसाधन का आकार और दिनांक प्रिंट होगा
डॉन
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.