从 Jar 中读取外部文件
我正在使用下面提到的代码从 java 类读取文件(.cer
文件)。 java 类将捆绑在 Jar 中,然后包含在 EAR 中。
FileInputStream inStream = new FileInputStream ("/home/test/foo.cer");
我也尝试过
FileInputStream inStream = new FileInputStream ("home/test/foo.cer");
,但它们都抛出 FileNotFoundException。 EAR部署在Unix系统的Weblogic中。我已经验证了路径和文件名。
如果我遗漏了什么,有什么想法吗?
I am using below mentioned code to read a file (.cer
file) from a java class. The java class will be bundled inside a Jar and then be included into a EAR.
FileInputStream inStream = new FileInputStream ("/home/test/foo.cer");
I also tried
FileInputStream inStream = new FileInputStream ("home/test/foo.cer");
But both of them throw FileNotFoundException. The EAR is deployed in Weblogic in a Unix system. I have verified the path and file name.
Any ideas if I have missing anything?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议将 .cer 文件的位置包含在应用程序服务器的类路径中,然后使用 ClassLoader 的 getResourceAsStream(String location) 方法。
如果您将目录
/home/test
添加到类路径中,则适当的代码应该是:我发现这是在应用程序服务器中运行时加载外部资源的一种更可靠的方法,而且它使得测试更容易。
I suggest including the location of the .cer file on the application server's classpath, and then loading it with ClassLoader's getResourceAsStream(String location) method.
If you added the directory
/home/test
to the classpath, appropriate code should be:I've found this to be a far more reliable method of loading external resources when running in an appserver, plus it makes testing much easier.