Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file not found exception in jar

Tags:

java

public class ABC {
    public ABC() {
        File file = new File("xyz.xml");

but when I run my jar as follows:

java -jar filename.jar arguments....

then it is showing error:

java.lang.IllegalArgumentException: InputStream cannot be null
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:120)
at com.ensarm.niidle.web.proxy.ABC.<init>(ABC.java:47)

How can I fix it?

like image 505
swarup7m Avatar asked Jan 21 '11 05:01

swarup7m


1 Answers

If you need to read file content in JARs, you can not use File class directly. Using ClassLoader to load it:

// for example read the SeleniumConfiguration.xml in the default package

InputStream input = SeleniumConfiguration.class.getResourceAsStream("/SeleniumConfiguration.xml");
like image 135
qrtt1 Avatar answered Nov 08 '22 19:11

qrtt1