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:
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:
mypackage.mysubpackage.MyClass
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
) ?
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.