this.sourceFileName = TestClass.class.getResource("Book1.xls").toURI().getPath();
System.out.println("URI:" + this.sourceFileName );
这是一段客户端代码,Book.xls和TestClass在同一个目录下。
如果这段代码在本地跑的话是没有问题,如果打成jar包,用户在下载自己客户端,这里获取的sourceFileName 会是一个null。
请高手帮看一下。
谢谢。
已经找到解决方法了,谢谢各位的踊跃回答。
具体,请看笔记:http://blog..com/lax/1190000000423350
出问题在于你getResource()得到URL后, 再使用.toURI().getPath().
代码:
System.out.println(EsrQuery.class.getResource("MyRB.properties") );
System.out.println(EsrQuery.class.getResource("MyRB.properties").toURI().getPath());
打成jar包, 运行结果:
d:\>java -jar test.jar
rsrc:MyRB.properties
null
再试了下, 应该是在getPath()时返回了null.
确认下jar中xls文件的位置
同楼上,确认jar包中xls文件的位置。如果你打成jar包后在自己本机上测试,是否会出现问题
可以使用下面的代码试试
this.sourceFileName = TestClass.class.getClassLoader().getResource("Book1.xls").toURI().getPath();