1

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:

enter image description here

This is how i'm trying to read it:

enter image description here

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)

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Phill Alexakis
  • 1,449
  • 1
  • 12
  • 31

2 Answers2

1

Assuming that the JAR file is created from the project in the normal way, the correct resource path for that XML file will be "/PhillPlugin.xml" not "Resource/PhillPlugin.xml".

Assuming that Class1.class is in the default package, and you use Class::getResourceAsStream, the relative path "PhillPlugin.xml" should work as well.

Use jar tvf your.jar to check that the XML file is in the JAR, and what the resource path for the XML file actually is.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

(Posted on behalf of the question author; copied from the solution they placed in the question).

I forgot to re-deploy he .JAR file after the change. It works perfectly now

Makyen
  • 31,849
  • 12
  • 86
  • 121
halfer
  • 19,824
  • 17
  • 99
  • 186