简体   繁体   中英

Read an external file from a Jar

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?

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:

this.getClass().getClassLoader().getResourceAsStream("foo.cer");

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.

try {
    BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME_REL_OR_ABSOLUTE));
    reader.readLine();
} catch (IOException e) { 

} catch (FileNotFoundException ex) {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM