views:

43

answers:

3

I have MyClassin package X, Also in package X there are packages Y and Z like this:

X - MyClass
X - Y - Some Files
X - Z - Some Files

How do I get a list of all the files in packages Y and Z from MyClass?

+1  A: 

Java packages mirror directory structure. You can use the File class. In particular, see the listFiles() method.

EDIT

You can dynamically find your executing location. Here is code from a project I've recently worked on; I wanted to be able to find the directory I'm running the JAR from (if I'm running the JAR), or else the directory of the JAR if I'm running from the class files. In my case, my JAR is in <project root>/bin and my classes are in <project root>/classes.

final URL location;
final String classLocation = JavaPlanner.class.getName().replace('.', '/') + ".class";
final ClassLoader loader = JavaPlanner.class.getClassLoader();

if(loader == null)
{
    try { throw new ClassNotFoundException("class loaded with bootstrap loader"); }
    catch (ClassNotFoundException cnfe) { throw new InitializationException(cnfe); }
}
else
{
    location = loader.getResource(classLocation);
}

if(location.toString().startsWith("file:/")) // Line 14
{
    // Running from .class file
    String path;
    try { path = URLDecoder.decode(location.toString().substring(6), "UTF-8"); }
    catch(UnsupportedEncodingException uee) { throw new InitializationException(uee); }

    // Move up package folders to root, add /bin/
    File package_ = new File(path).getParentFile();
    binPath = package_.getParentFile().getParentFile().getParentFile().getParentFile() + File.separator + "bin" + File.separator;
}
else // Line 25
{
    // Running from .jar file
    String jarURL = JavaPlanner.class.getResource("/" + JavaPlanner.class.getName().replaceAll("\\.", "/") + ".class").toString();
    jarURL = jarURL.substring(4).replaceFirst("/[^/]+\\.jar!.*$", "/");

    try
    {
        File dir = new File(new URL(jarURL).toURI());
        jarURL = dir.getAbsolutePath();
    }
    catch(MalformedURLException mue) { throw new InitializationException(mue); }
    catch(URISyntaxException use) { throw new InitializationException(use); }

    binPath = jarURL;
}

At line 14, I've found that I'm running the application from a class file. String path initially is set to the file path of JavaPlanner (the class containing my main method). I know the package structure JavaPlanner is in, so I use getParentFile an appropriate number of times to find the project root, and then append bin/.

At line 25, I've found that I'm running the application from a JAR. The block simply gets the path to the folder containing that executable JAR.

Obviously, this code is not 100% adapted to your purpose (most specifically, I'm calling getParentFile a specific number of times for my package structure), but I think it should help.

The entire purpose of the above code was to be able to find the correct resource files for my application. In the production version, only the JAR would be available to the user, but I didn't want to have to rebuild the JAR every time I needed to test some code, and I didn't want to duplicate my resource files, and I didn't want to pollute my bin/ folder with the class files (because everything in bin/ was meant to be sent to the user).

Brian S
I am trying to avoid hardcoding the path. I would like ot dynamically find the path.
Jon
@Jon: See my edit, I've included code I've previously used to find a specific folder relative to my executing location. It should help.
Brian S
A: 

It depends on the source of the class. If they are from a JAR file, then you can use the JarFile class to get a list of entries.

Maz
If they are not in a JAR file, and they are separate CLASS files, then you should use Brian's idea.
Maz
They are not in a jar and they are data files not java files.
Jon
A: 

If you're only looking for classes in the package, consider

Package.getPackage(String name)

Package.getPackages()

http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/lang/Package.html#getPackage%28java.lang.String%29

http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/lang/Package.html#getPackages%28%29

glowcoder
More background:I have several files I need to loop through in test methods. Some of the files are in package Y and some are in package Z. They are not class files, they are just data files.I want to be able to loop through all the files in package Y and package Z.
Jon