【发布时间】:2014-11-20 05:13:53
【问题描述】:
我有两节课。一个是公共库,另一个是使用该库的客户端。
该库的结构如下:
public class Library
{
public Library()
{
String pathToResourceA="src/main/resources/A.xls";// A.xls is present within resources
String key="apples";
Resource res= loadResourceBasedOnDoc(pathToResource,key);
...//process resource
}
}
对应的测试类
public class LibraryTest
{
@Test
public void testLibrary()
{
new Library();// works as expected- the test passes and the resource is loaded.//ie. A.xls is found in the right place
}
}
但是,当我尝试通过以下方式从客户端访问库时
import packagename.Library
public class Client{
Library lib;
public Client()
{
lib= new Library();// throws a FileNotFoundException!
}
}
我得到一个 FileNotFoundException。我猜这与在 A 类中为 pathToResourceA 定义正确的值有关,但无法弄清楚它是什么。任何想法将不胜感激。谢谢!
loadResourcesBasedonDoc 的代码
protected Resource loadResourceBasedOnDoc(String filename,String password)
{
InputStream in = null;
try {
in = new FileInputStream(filename);
//further in is processed...
【问题讨论】:
-
能否贴出 loadResourceBasedOnDoc 方法的代码?
-
@EJK 我已经添加了代码在高层次上的作用。
-
您的评论“A.xls 存在于资源中”。你能详细说明一下吗? A.xls 是文件系统上的文件,还是包含在库的 JAR 文件中的文件?
-
A.xls是文件系统上的一个文件。src/main/resources/A.xls是相对于Library的文件路径。整个想法是在多个项目中没有A.xls,并且只在库中。