问题
I have a code that load all classes in folder with same package name ( second ).
how can i change it to load all classes in a folder with deference package (not in second) (and we don't know the package name)
int classCounter = 0;
File folderAdded = new File("..//940424//second");
File [] classFileAdded = folderAdded.listFiles();
String [] addedClassName = new String [classFileAdded.length];
List<Operations> newOp = new ArrayList<Operations>();
Operations newOpTemp = null;
for(int i = 0; classCounter < classFileAdded.length; classCounter++){
addedClassName [classCounter] = classFileAdded[classCounter].getAbsolutePath().substring(classFileAdded[classCounter].getAbsolutePath().lastIndexOf("\\")+1);
addedClassName [classCounter] = addedClassName[classCounter].substring(0,(addedClassName[classCounter].lastIndexOf(".")));
addedClassName [classCounter] = "second." + addedClassName[classCounter];
Class addedClass = Class.forName(addedClassName[classCounter]);
newOpTemp = (Operations)addedClass.newInstance();
if (newOpTemp instanceof Operations){
newOp.add( i, newOpTemp);
i++;
}
回答1:
Two things that come to my mind:
- If you want to load classes, the corresponding folder must be in your classpath (can you ensure this -- as you don't know the folder's name?)
- If you want to load a class with package
a.b.ClassNamethe file must be located in foldera/b/ClassName.class(otherwise, loading will fail)
update
If you know the folder name, you also know the package name (that's the trick). It must correspond to each other in Java. If a class is in package a.b it must be in folder a/b/. If you only know the root-folder, ie, pathToRootFolder/a/b/ you can search for folders using
File root = new File("<rootFolder>");
File[] files = root.listFiles()
and check each file if it is a directory:
for(File f : files) {
if(f.isDirectory()) {
// do processing
}
}
If there is only one file in a single package structure, there should be only one directory and the name must corresponds to the package name (of course you need to "step into the directory" until there is no further nesting, ie, no sub-folder, and this last folder contains the actual class you want to load. This way to can extract the directory structure and thus the package name.
end update
Some side remarks about your code:
- Why
//innew File("..//940424//second")(is this a typo and should be\\) [btw: it is better to useFile.separator] - Why do you have variable
iin your for-loop but actually useclassCounter?? - In
addedClassName[classCounter] = classFileAdded[classCounter].getAbsolutePath().substring(classFileAdded[classCounter].getAbsolutePath().lastIndexOf("\\")+1);you extract the file name, right? Why you not just useaddedClassName[classCounter] = classFileAdded[classCounter].getName()?
回答2:
you can try this:
Reflections reflections = new Reflections("mysh.im.monitor");
reflections.getSubTypesOf(Monitor.class);
before that, make sure your folder is in your classpath.
maven dependency
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.9</version>
</dependency>
来源:https://stackoverflow.com/questions/31510686/load-all-classes-in-a-specific-folder-without-knowing-package-name-java