[英]Accessing a different folder using classloader
URL root = Thread.currentThread().getContextClassLoader().getResource(packageName.replace(".", "/"));
i use the above statement to access .class files given a package name. 我使用上面的语句访问给定包名称的.class文件。 But in some cases i need to access the files from \\build\\test\\classes instead of \\build\\classes . 但是在某些情况下,我需要从\\ build \\ test \\ classes而不是\\ build \\ classes访问文件。 How do i modify the above statement? 我如何修改以上声明?
You are having problems with your classpath. 您的类路径有问题。 ${project}/build/test/classes
and ${project}/build/classes
folders suggest that you have either used Ant to compile your code, or checked "Allow output folders for source folders" option in Eclipse's "New Java Project" dialog. ${project}/build/test/classes
和${project}/build/classes
文件夹表明您已经使用过Ant来编译代码,或者在Eclipse的“ New Java Project”中选中了“允许源文件夹的输出文件夹”选项对话。
You should configure you project to compile all source files into the same output folder, in your case /build/classes
. 您应该将项目配置为将所有源文件编译到同一输出文件夹(在您的情况下为/build/classes
。
If you use classloader to load a resource the path you give it is not absolute to entire filesystem even if it starts with /
. 如果您使用类加载器加载资源, 则即使它以/
开头,您给出的路径也不是整个文件系统的绝对路径。
InputStream is = this.getClass().getResourceAsStream("/some/resource");
This actually means find this file ${project}/build/classes/some/resource
. 这实际上意味着找到此文件${project}/build/classes/some/resource
。 And in order to get that resource at the target location the easiest way is to place it in ${project}/src/some/resource
. 为了使该资源位于目标位置,最简单的方法是将其放置在${project}/src/some/resource
。
Although you should consider using Maven and m2eclipse if your using Eclipse. 尽管如果使用Eclipse,则应考虑使用Maven和m2eclipse。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.