Exclude file in the bank

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

but when I start my jar as follows:

 java -jar filename.jar arguments.... 

then it shows an 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 this?

+7
source share
4 answers

If you need to read the contents of a file in a JAR, you cannot directly use the File class. Using ClassLoader to download it:

// e.g. read SeleniumConfiguration.xml in the default package

 InputStream input = SeleniumConfiguration.class.getResourceAsStream("/SeleniumConfiguration.xml"); 
+10
source

The NullPointerException is a clear indication that the file was not found.

Input InputStream = ABC.class.getResourceAsStream ("/Element.xml");

Where is your xml file? If you put it in the same package (the directory inside the jar file) as ABC.class, then it should be Element.xml without a slash.

+4
source

You can usually use an InputStream as suggested, but if you want to perform further operations without java in the file, for example, decrypting it with an external application, etc., you can use FileOutputStream to write this stream to the file, and then use its path as the correct path to the file. In simple words, you can undo this file on your file system.

+1
source

Did you put your xml file in the root of the jar file? If you use a path like "/Element.xml", the structure of the jar file should be like this:

jar file

  • com
  • META-INF
  • element.xml
0
source

All Articles