-2

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) ?

rnunes
  • 2,785
  • 7
  • 28
  • 56
  • when you say you have the path, i assume you don't have the file name, right? – Stephan Dec 13 '12 at 14:03
  • @Stephan: I have the .java full path. I'm going to edit the question now to make it more clear. – rnunes Dec 13 '12 at 14:09
  • did you tried my post ?? Because, this is what i got from your previous post. I must say, my post will helpful for your scenario – Ravi Dec 13 '12 at 14:23
  • 1
    You already have made a wrong assumption: a single .java file can contain *more than one* class (it can contain a maximum of one *public* class, but many more classes in general, like inner classes or package access classes for example). The only reliable way to obtain all these class names is to *parse* the .java file properly and completely. – Durandal Dec 13 '12 at 15:35
  • @Durandal I tried to make it clear that I need all of the classes full name, sorry about that. Could you put that as an answer so that I can accept it? – rnunes Dec 13 '12 at 15:43
  • @munes Sorry I must somehow missed your edit (with regards to multiple classes in one file). I added an answer outlining the general obstacles to overcome when trying to get the class names from a .java file. – Durandal Dec 13 '12 at 15:56
  • @rnunes i really don't understand till now, what you exactly wants to do. Nevertheless, i believe, your problem would be resolved now. – Ravi Dec 13 '12 at 16:27

5 Answers5

1

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

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • But Class.forname(string) needs the "the fully qualified name of the desired class", and I only have the .java file path. See http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#forName(java.lang.String) – rnunes Dec 13 '12 at 13:17
1

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.

Ravi
  • 30,829
  • 42
  • 119
  • 173
  • No, all I have is a .java file path, and you're using a reference to a Class object (java.lang.String.class) – rnunes Dec 13 '12 at 13:49
  • Even after the edit, it's not that, that we'll only give me the path of .class files. I'll edit the question, to make it more clear. – rnunes Dec 13 '12 at 14:09
  • This should work, although only if the .class are in the same directory as the .java files (-d option, javac) – Stephan Dec 13 '12 at 14:13
  • @coders, to get what he wants you should modify the return condition, i think: `return (filename.endsWith(".class") && filename.startsWith("MyClass"));` having it passed to `finder(String dirName,String className)` – Stephan Dec 13 '12 at 14:33
  • I have tested and it is showing what you exactly want. If this is what you want, then accept this answer otherwise put some comment here – Ravi Dec 13 '12 at 14:48
  • @coders All I have it's a .java file path, I don't even have a .class file, it's not mandatory and I'm avoiding have to compile the file in order to find the classes that the .java have – rnunes Dec 13 '12 at 14:59
  • 1
    so basically what you're saying is that the .class file doesn't exist. Then why all the fuss? "MyClass" should be enough! If this is not the case then you really need to rephrase the question better. – Stephan Dec 13 '12 at 15:02
  • @rnunes you should mention this thing. I mean about your different assumption. You should tell, you don't know about the class directory and you want class name. I would recommend you, better to ask your question by putting some example and include your all assumption there. – Ravi Dec 13 '12 at 15:05
  • @Stephan "MyClass" it's not the full class name, you can't identify a class just using "MyClass", you can have several classes named "MyClass" if they have different packages. – rnunes Dec 13 '12 at 15:32
  • 1
    @munes if you have the full path (taken that in compilation you do not specify a different path for the compiled files), then you also have the full class name. i still don't get what you're banging about – Stephan Dec 13 '12 at 15:36
  • @Stephan If you look in my example, the .java file path "C:\myfolder\MyClass.java" doesn't have anything to do with the class full name, which is "mypackage.mysubpackage.MyClass" – rnunes Dec 13 '12 at 15:42
  • @rnunes be clear with your question, you must include, your class file and java file are located in different directory. But, i think, ost of the case, java and class file are in the same directory. But, as you have mentioned `mypackage.mysubpackage.MyClass` which means `mypackage.mysubpackage` is the directory of class file, isn't ?? – Ravi Dec 13 '12 at 15:44
  • @rnunes ok leave it. Just tell me whether you know the directory of your class file, yes or no ?? If no, then, whether class path is variable. i mean, it will change for different java program ?? If yes and your not changing your class path. Then, i really don't know, what is your problem. Simply, you just put the exact directory path, where i mentioned in my code. – Ravi Dec 13 '12 at 15:51
  • @coders "let's ignore package and folder association conventions". I wrote that because the .java files doesn't need to be on a conventional folder structure (even though Eclipse will give you an error, you can use javac to do it if you have the files on a "non-conventional" directory). A little bit off-topic: .class files are whatever you want them to be, you just need to configure your build – rnunes Dec 13 '12 at 15:51
  • i believe, you are really going off-topic. – Ravi Dec 13 '12 at 15:53
  • @coders I'm just explaining to you why your solution doesn't solve my problems: A class full name it's not "MyClass" or even related to the folder structure where the file is. Even if that's the usual way, in this problem I can't just think about the usual way, but also on the others, even if they'll appear 1% of the time – rnunes Dec 13 '12 at 15:59
1

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.

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • i belive, whatsoever would be class/interface, all will end with class extension. AFAIC, he wanted to list all class name. I really don't know, whether he was asking, whether is it possible or not or himself consfused about his question. Nevertheless, he got the solution. We got relief. – Ravi Dec 13 '12 at 16:31
0

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.

Josef.B
  • 942
  • 8
  • 16
-1

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

        }
Murali
  • 774
  • 6
  • 12