I'm trying to refer to a XML file into my project so i can parse the data from it.
It is being exported as a .jar
file which means i can't refer to it using
BufferedInputStream in = new BufferedInputStream(new FileInputStream("PhillPlugin.xml"));
which works perfectly when i run it from the JDeveloper IDE
So i replace the line above with the following line:
BufferedInputStream in = new BufferedInputStream(Class1.class.getResourceAsStream("Resources/PhillPlugin.xml"));
But when i'm executing the .jar
file
java -jar test.jar
in terminal
the java.io.IOException: Stream closed
is being triggered.
It's mandatory to do it using Streams and not with any Parsing XML libraries
This is where my files are located:
This is how i'm trying to read it:
Based on this post here
Here is the code if you want to try it:
public static void main(String[] args) throws Exception {
/*This is line 21*/ BufferedInputStream in = new BufferedInputStream(Class1.class.getResourceAsStream("Resources/PhillPlugin.xml"));
StringBuilder sb = new StringBuilder();
String genurl=null;
int cp;
while ((cp = in.read()) != -1) {
sb.append((char) cp);
String t = sb.toString();
if(t.contains("</AttributeValuePair>"))
{
String test = sb.toString();
String test1p[]=test.split("<value>|</value>");
genurl=test1p[1];
break;
}
}
System.out.println(genurl);
sb=new StringBuilder();
while ((cp = in.read()) != -1) {
sb.append((char) cp);
String t = sb.toString();
if(t.contains("</AttributeValuePair>"))
{
String test = sb.toString();
String test1p[]=test.split("<value>|</value>");
genurl=test1p[1];
System.out.println(genurl);
break;
}
}
in.close();
}
This is the stacktrace i'm getting from the terminal
after the correct reference to the path
Exception in thread "main" java.io.FileNotFoundException: PhillPlugin.xml (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at client.Class1.main(Class1.java:21)