What you want is not possible because there might not be a .java file around (only a .class file), the .class file might reside inside a (nested) .jar file and the JVM does not know which classpath entry was used to locate the .class file.
That said, I have tried to solve the same problem for a project of mine. The code goes like this:
private static List<Class> getClassesForPackage(String packagename)
throws ClassNotFoundException
{
// This will hold a list of directories matching the pckgname.
// There may be more than one if a package is split over multiple
// jars/paths
List<Class> classes = new ArrayList<Class>();
List<File> directories = new ArrayList<File>();
try {
ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
throw new ClassNotFoundException("Can't get class loader.");
}
// Ask for all resources for the path
Enumeration<URL> resources =
classLoader.getResources(packagename.replace('.', '/'));
while (resources.hasMoreElements()) {
URL res = resources.nextElement();
if (res.getProtocol().equalsIgnoreCase("jar")) {
JarURLConnection conn =
(JarURLConnection) res.openConnection();
JarFile jar = conn.getJarFile();
for (JarEntry e : Collections.list(jar.entries())) {
if (e.getName().startsWith(
packagename.replace('.', '/'))
&& e.getName().endsWith(".class")
&& !e.getName().contains("$"))
{
String className =
e.getName().replace("/", ".").substring(
0,
e.getName().length() - 6);
classes.add(Class.forName(className));
}
}
}
else
directories.add(new File(URLDecoder.decode(
res.getPath(),
"UTF-8")));
}
}
catch (NullPointerException x) {
throw new ClassNotFoundException(packagename
+ " does not appear to be "
+ "a valid package (Null pointer exception)");
}
catch (UnsupportedEncodingException encex) {
throw new ClassNotFoundException(packagename
+ " does not appear to be "
+ "a valid package (Unsupported encoding)");
}
catch (IOException ioex) {
throw new ClassNotFoundException(
"IOException was thrown when trying "
+ "to get all resources for " + packagename);
}
List<String> subPackages = new ArrayList<String>();
// For every directory identified capture all the .class files
for (File directory : directories) {
if (directory.exists()) {
// Get the list of the files contained in the package
File[] files = directory.listFiles();
for (File file : files) {
// add .class files to results
String fileName = file.getName();
if (file.isFile() && fileName.endsWith(".class")) {
// removes the .class extension
classes.add(Class.forName(packagename + '.'
+ fileName.substring(0, fileName.length() - 6)));
}
// add directories to subpackages
if (file.isDirectory()) {
subPackages.add(packagename + "." + fileName);
}
}
}
else {
throw new ClassNotFoundException(packagename + " ("
+ directory.getPath()
+ ") does not appear to be a valid package");
}
}
// check all potential subpackages
for (String subPackage : subPackages) {
classes.addAll(getClassesForPackage(subPackage));
}
return classes;
}
To find the directory of the corresponding .java file you need to configure the source directory you are using.