Question

What's the most efficient way of getting the class(es) created on a .java file? I have the .java file path and I need to get the class full name.

I can only remember:

  1. Compile it with JavaCompiler
  2. Using the file text to parse it with Eclipse ASTParser (or another)
  3. Infer the class name through part of the file path, but I don't know if this works for every cases
  4. Somehow, use a tool like javap (dind't really thought about this one)

EDIT

I have this file, located at C:\myfolder\MyClass.java (let's ignore package and folder association conventions):

package mypackage.mysubpackage;

public class MyClass 
{
    // my class implementation here

    public class MyInnerClass 
    {
        // my inner class implementation here
    } 
}

The full name of the classes declared in this file are:

  1. mypackage.mysubpackage.MyClass
  2. mypackage.mysubpackage.MyClass.MyInnerClass (I don't know if this one it's correct, but let's pretend it is)

How can I get those class when I only have the .java file path (C:\myfolder\MyClass.java) ?

Was it helpful?

Solution

The only way to reliably obtain the names of the classes (mind that it may also define interfaces) files a .java file declares would be to really parse the java language contained in that file.

And even then you will need to know which compiler will be/has been used to compile the .java file, as a java compiler could use any naming convention it likes for anonymous classes (the Oracle compiler uses $1, $2..., but there is no strict need to mimic that behavior).

Considering these obstacles I believe its very hard to do from the .java files contents and simply impossible with the .java files path alone.

OTHER TIPS

The most effective way is Class.forName().getName()

I have the .java file path and I need to get the class full name.

Which means, you know the path of .java file and you want the class name of each class file.

class Filter {

public static void main(String[] a) {
    Filter f = new Filter();
    String dirName = "D:\\Yourfolder\\";  // assuming your java file are located in D:\Yourfolder\
    f.finder(dirName);  // call the method for listing all the class file
}

public File[] finder(String dirName) {

    File dir = new File(dirName);

    return dir.listFiles(new FilenameFilter() {

        public boolean accept(File dir, String filename) {
            if(filename.endsWith(".class"))
            {
            System.out.println(filename);
            }
            return filename.endsWith(".class");

        }
    });

}

}

Replace dirName with your .java directory path.

One approach is to scan the directory tree where your Java source files are located, and for each file ending in ".java", you take its full folder path as a String and convert each dir separator to a '.' character. This will give you the fully qualified class name (FQCN). For example, if the path is: com\foo\fee\Foo.java, that becomes com.foo.fee.Foo.

Of course, this does not give you inner or nested classes and other advanced things, but these are created when you compile.

I have seen this kind of directory scanning in many frameworks, even Spring.

I am working on this in Groovy, so far I have:

File file = new File(rootSourcePath)

file.eachFileRecurse(FILES){
    def path =it.getAbsolutePath()
    println path

    if(path.endsWith(".java")){
           // to do the conversion here
    }
}

Hope this interpreted your question correctly.

To get the name of the class file Try this

      void printClassName(String classname) 


        {

          System.out.println("The class name " + classname +" is " + classname.getClass().getName());

        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top