Java Class Find findClassOriginFile(Class cls)

Here you can find the source of findClassOriginFile(Class cls)

Description

find Class Origin File

License

Open Source License

Declaration

public static File findClassOriginFile(Class cls) 

Method Source Code


//package com.java2s;
import java.io.File;

import java.net.URL;

public class Main {
    public static File findClassOriginFile(Class cls) {
        // Try to find the class file.
        try {//from  www .ja  v  a2  s  .co  m
            final URL url = cls.getClassLoader().getResource(cls.getName().replace('.', '/') + ".class");
            final File file = new File(url.getFile()); // toString()
            if (file.exists())
                return file;
        } catch (Exception ex) {
        }

        // Method 2
        try {
            URL url = cls.getProtectionDomain().getCodeSource().getLocation();
            final File file = new File(url.getFile()); // toString()
            if (file.exists())
                return file;
        } catch (Exception ex) {
        }

        return null;
    }
}

Related

  1. findClasses(String directory, String packageName)
  2. findClasses(String path, String packageName)
  3. findClassesAnnotatedWith(String packageName, Class a)
  4. findClassesAssignableFrom(String packageName, Class assignableFrom)
  5. findClassLocation(Class context)
  6. getAllAssignedClass(Class cls)
  7. getAllClasses(ClassLoader cl, String packageName)
  8. getAllClasses(String packageName)
  9. getAllClassesFromPackage(final String packageName)