比如有个类叫
a.b.c.MyClass输入1个字符串"MyClass" 返回a.b.c.MyClass当然有重名的情况,全部返回
比如有两个类
a.b.c.MyClass
x.y.z.MyClass
输入1个字符串"MyClass" 
返回
a.b.c.MyClass

x.y.z.MyClass

解决方案 »

  1.   

    其实Eclipse插件里有类似功能的,就是
    ctrl+shift+o 和 ctrl+shift+m有人知道怎么实现吗?
      

  2.   

    MyClass.getClass()可以得到这个类的类名,至于所有包下的我就不知道了。
      

  3.   

    JDK没有现成的API来做这个。
    以前看到有人把classpath里面jar文件当作IO文件系统,然后search这个class,得到全类名。
      

  4.   

    不太明白,是要在整个项目中搜索所有的a.b.c.MyClass吗?
      

  5.   

    在要查的jar里面search: MyClass.class, 然后得到全路径。
      

  6.   

    遍历整个文件目录,
    找到以.java结尾的文件名为“MyClass”的文件,
    然后依次取到此文件上层的文件夹名(包名),
    直到项目的源码根目录,
    拼成一个完整的目录路径,
    装到一个ArrayList里。
    然后接着继续遍历,
    如果再次找到以.java结尾的文件名为“MyClass”的文件,
    再依次取到此文件上层的文件夹名(包名),装到ArrayList里,重复
    直到遍历完毕,
    最终返回结果集ArrayList。
    output。。
    你可以在遍历的时候,设置一个变量location,每次遍历不一样的文件夹时,随时改变location的值,找到
    “MyClass.java”时候,就直接返回location+“MyClass”;有点罗嗦,遍历就行
      

  7.   

    已经用Lucene索引了的,所以非常快
      

  8.   

    谢谢,以上的同学回答。我会参考的。

    其实我在做的项目是一个Eclipse插件,我在考虑和希望直接调用ctrl+shift+m这个功能。

    如果不行的话,只能从classpath里读class文件和jar里的class文件了。
      

  9.   

    关于Java编辑器快捷键设置操作可以看看这个: http://wenku.baidu.com/view/e20c9aa1284ac850ad0242ce.html
      

  10.   

    Java API 做不到,能做到的类名必须是完全限定的名称,也就是带有全部包名的类名
      

  11.   

    自己按
    遍历文件目录和JAR文件的方法写好了。 没有注释,先共享给大家。调用时FullClassNameGetter.getInstance().getFullClassName(不含包的类名);
    具体代码
    FullClassNameGetter.java
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.jar.JarEntry;
    import java.util.jar.JarFile;
    public class FullClassNameGetter { private static final String JAVA_CLASS_PATH = "java.class.path"; private static final String CLASSPATH_SEPARATOR = System.getProperty("path.separator"); private static final String PACKAGE_SEPARATOR = "."; private static final String JAR_DIR_SEPARATOR = "/"; private static final String CLASS_SUFFIX = ".class"; private HashMap<String, ArrayList<String>> nameingMap;
    private static class FullClassNameGetterSingletonHolder {
    private static FullClassNameGetter instance = new FullClassNameGetter();
    } private FullClassNameGetter() {
    final String initialClasspath = System.getProperty(JAVA_CLASS_PATH);
    setClassPath(initialClasspath);
    } public static FullClassNameGetter getInstance() {
    return FullClassNameGetterSingletonHolder.instance;
    } public ArrayList<String> getFullClassName(String className) {
    if (className.indexOf(PACKAGE_SEPARATOR) != -1) {
    ArrayList<String> ret = new ArrayList<String>();
    ret.add(className);
    return ret;
    } return nameingMap.get(className);
    } public void setClassPath(String classPath) {
    nameingMap = new HashMap<String, ArrayList<String>>();
    String[] classPathArray = classPath.split(CLASSPATH_SEPARATOR); File file;
    for (String onePath : classPathArray) {
    file = new File(onePath);
    if (file.exists()) {
    if (file.isDirectory()) {
    // dir
    setDirClasses(file, "");
    } else {
    // jar file
    setJarClasses(file);
    }
    }
    } } private void setDirClasses(File classDir, String path) {
            File[] files = classDir.listFiles();
            String className;
            for (File file : files) {
                if (file.isDirectory()) {
                 setDirClasses(file, path + file.getName() + PACKAGE_SEPARATOR);
                } else {
                 if (file.getName().endsWith(CLASS_SUFFIX)) {
                 className  = file.getName().substring(0, file.getName().length() - CLASS_SUFFIX.length());
                 if (!"".equals(path)) {
                 setNamingMap(className, path + className);
                 } else {
                 //no package
                 setNamingMap(className, className);
                 }
                 }
                }
            }
    } private void setJarClasses(File jarFile) {
    JarFile jf = null;
            try {
                jf = new JarFile(jarFile);            Enumeration<JarEntry> es = jf.entries();
                JarEntry entry;
                String simpleName;
                while (es.hasMoreElements()) {
                 entry = es.nextElement();
                 if (!entry.isDirectory()) {
                 String fullName = entry.getName();
                 if (fullName.endsWith(CLASS_SUFFIX)) {
                 fullName = fullName.substring(0, fullName.length() - CLASS_SUFFIX.length());
                            int index = fullName.lastIndexOf(JAR_DIR_SEPARATOR);
                            if (index != -1) {
                             simpleName = fullName.substring(index + 1, fullName.length());
                             fullName = fullName.replace(JAR_DIR_SEPARATOR, PACKAGE_SEPARATOR);
                             setNamingMap(simpleName, fullName);
                            } else {
                             //no package
                             simpleName = fullName;
                             setNamingMap(simpleName, fullName);
                            }
                 }
                 }
                }
            } catch (IOException e) {
             //do nothing
            } finally {
                if (jf != null) {
                    try {
                        jf.close();
                    } catch (IOException e) {
                     //do nothing
                    }
                }
            }
    } private void setNamingMap(String simpleName, String fullName) {
    ArrayList<String> value = nameingMap.get(simpleName);
    if (value == null) {
    value = new ArrayList<String>();
    nameingMap.put(simpleName, value);
    }
    if (!value.contains(fullName)) {
    value.add(fullName);
    }
    }
    }
      

  12.   

    sorry,以上代码还不支持JDK自己的类再改ing.
      

  13.   

    调用java.lang包下的Class类的getClass方法如:
    MyClass.getClass().getName()
      

  14.   

    修改版,支持标准jdk的那些class名转换带package的类名
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    //import java.util.Date;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.jar.JarEntry;
    import java.util.jar.JarFile;
    public class FullClassNameGetter { private static final String JAVA_CLASS_PATH = "java.class.path"; private static final String CLASSPATH_SEPARATOR = System.getProperty("path.separator"); private static final String PACKAGE_SEPARATOR = "."; private static final String JAR_DIR_SEPARATOR = "/"; private static final String CLASS_SUFFIX = ".class"; private HashMap<String, ArrayList<String>> nameingMap; private String[] jdkJars = {"lib/rt.jar",
    "lib/jsse.jar",
    "lib/jce.jar",
    "lib/charsets.jar",
    "lib/ext/dnsns.jar",
    "lib/ext/localedata.jar",
    "lib/ext/sunjce_provider.jar",
    "lib/ext/sunmscapi.jar",
    "lib/ext/sunpkcs11.jar"}; private static class FullClassNameGetterSingletonHolder {
    private static FullClassNameGetter instance = new FullClassNameGetter();
    } private FullClassNameGetter() {
    final String initialClasspath = System.getProperty(JAVA_CLASS_PATH);
    //Date now = new Date();
    setClassPath(initialClasspath);
    //System.out.println("initialize done.  " + (new Date().getTime() - now.getTime()));
    } public static FullClassNameGetter getInstance() {
    return FullClassNameGetterSingletonHolder.instance;
    } public ArrayList<String> getFullClassName(String className) {
    ArrayList<String> ret = null;
    if (className.indexOf(PACKAGE_SEPARATOR) != -1) {
    ret = new ArrayList<String>();
    ret.add(className);
    return ret;
    }
    ret = nameingMap.get(className);
    return ret;
    } public void setClassPath(String classPath) {
    nameingMap = new HashMap<String, ArrayList<String>>();
    String[] classPathArray = classPath.split(CLASSPATH_SEPARATOR); File file;
    for (String onePath : classPathArray) {
    file = new File(onePath);
    if (file.exists()) {
    if (file.isDirectory()) {
    // dir
    setDirClasses(file, "");
    } else {
    // jar file
    setJarClasses(file);
    }
    }
    }
    String jreDir = System.getProperty("java.home");
    for (String jreJar : jdkJars) {
    file = new File(jreDir + JAR_DIR_SEPARATOR + jreJar);
    if (file.exists()) {
    // jar file
    setJarClasses(file);
    }
    } } private void setDirClasses(File classDir, String path) {
            File[] files = classDir.listFiles();
            String className;
            for (File file : files) {
                if (file.isDirectory()) {
                 setDirClasses(file, path + file.getName() + PACKAGE_SEPARATOR);
                } else {
                 if (file.getName().endsWith(CLASS_SUFFIX)) {
                 className  = file.getName().substring(0, file.getName().length() - CLASS_SUFFIX.length());
                 if (!"".equals(path)) {
                 setNamingMap(className, path + className);
                 } else {
                 //no package
                 setNamingMap(className, className);
                 }
                 }
                }
            }
    } private void setJarClasses(File jarFile) {
    JarFile jf = null;
            try {
                jf = new JarFile(jarFile);            Enumeration<JarEntry> es = jf.entries();
                JarEntry entry;
                String simpleName;
                while (es.hasMoreElements()) {
                 entry = es.nextElement();
                 if (!entry.isDirectory()) {
                 String fullName = entry.getName();
                 if (fullName.endsWith(CLASS_SUFFIX)) {
                 fullName = fullName.substring(0, fullName.length() - CLASS_SUFFIX.length());
                            int index = fullName.lastIndexOf(JAR_DIR_SEPARATOR);
                            if (index != -1) {
                             simpleName = fullName.substring(index + 1, fullName.length());
                             fullName = fullName.replace(JAR_DIR_SEPARATOR, PACKAGE_SEPARATOR);
                             setNamingMap(simpleName, fullName);
                            } else {
                             //no package
                             simpleName = fullName;
                             setNamingMap(simpleName, fullName);
                            }
                 }
                 }
                }
            } catch (IOException e) {
             //do nothing
            } finally {
                if (jf != null) {
                    try {
                        jf.close();
                    } catch (IOException e) {
                     //do nothing
                    }
                }
            }
    } private void setNamingMap(String simpleName, String fullName) {
    ArrayList<String> value = nameingMap.get(simpleName);
    if (value == null) {
    value = new ArrayList<String>();
    nameingMap.put(simpleName, value);
    }
    if (!value.contains(fullName)) {
    value.add(fullName);
    }
    }
    }
      

  15.   

    有一个办法, 但必须先拿到所有已知的类加载器,   在把每个类加载器加载过的类拿出来分析,(缺点就是不能查找未加载的类如a.jar中有a.a.a.MyClass但没人用他,你就找不到了)
      

  16.   

    用正则表达式遍历所有.class文件[a-zA-Z.]*文件名.class这样就可以了滤出你想要的文件名的