问题描述
我有一个代码以相同的包名称( second
)加载文件夹中的所有类。
I have a code that load all classes in folder with same package name ( second
).
如何我可以更改它以将带有引用包的文件夹中的所有类加载(不在 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++;
}
推荐答案
我遇到了两件事注意:
- 如果要加载类,则相应的文件夹必须位于类路径中(您可以确保吗?不知道文件夹的名称吗?)
- 如果要使用包
abClassName
加载类,则文件必须位于文件夹a / b / ClassName.class
(否则,加载将失败)
- 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.ClassName
the file must be located in foldera/b/ClassName.class
(otherwise, loading will fail)
更新
如果您知道文件夹名称,则也知道程序包名称(这就是窍门)。在Java中,它必须彼此对应。如果某个类在软件包 a.b
中,则必须在文件夹 a / b /
中。如果只知道根文件夹,即 pathToRootFolder / a / b /
,则可以使用以下方式搜索文件夹
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.
结束更新
关于代码的一些旁注:
- 为什么在
新文件( ..// 940424 // second)
中输入//
(这是拼写错误,应该是\\
)[btw:最好使用File.separator
] - 为什么有变量
i
在您的for循环中,但实际上使用classCounter
?? - 在
addedClassName [classCounter] = classFileAdded [classCounter] .getAbsolutePath()。substring(classFileAdded [classCounter] .getAbsolutePath()。lastIndexOf( \\)+ 1);
您提取文件名,对吗?为什么不只使用addedClassName [classCounter] = classFileAdded [classCounter] .getName()
?
- Why
//
innew File("..//940424//second")
(is this a typo and should be\\
) [btw: it is better to useFile.separator
] - Why do you have variable
i
in 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()
?
这篇关于在不知道包名称的情况下将所有类加载到特定文件夹中-Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!