1
     SAXBuilder builder = new SAXBuilder();
     try {
        File f = new File("\\\\bady\\SShare\\mart.xml");
        System.out.println(f.exists());   // Returns False
        System.out.println(f.length());   // Returns 0

        Document document = builder.build(f);  //IOException at this point
        Element root = document.getRootElement();
        Element paragraph = root.getChild("mart_element");
        String content = paragraph.getText();
        System.out.println("content = " + content);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Here mart.xml is a shortcut present in C:\Param\Bin on a windows box. I get the following IOException:

Exception in thread "main" java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
        at java.net.Socket.connect(Socket.java:524)
        at java.net.Socket.connect(Socket.java:474)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
        at sun.net.NetworkClient.openServer(NetworkClient.java:118)
        at sun.net.ftp.FtpClient.openServer(FtpClient.java:488)
        at sun.net.ftp.FtpClient.openServer(FtpClient.java:475)
        at sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:270)
        at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(FtpURLConnection.java:352)
        at JDOMElementTextContent.parseXml(JDOMElementTextContent.java:36)
        at JDOMElementTextContent.main(JDOMElementTextContent.java:47)

I tried to open Stream from URL using file: protocol but URLConnection.getInputStream throws the same connection refused exception.

Any recommendations would be appreciated?

Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56

3 Answers3

2

The code in question can't really produce that error message.

First of all, simply creating a File object does not do any checks, so please tell us what you acutally do with that File.

Second, you specify the path with the string literal "\\\\bady\\SShare\\mart.xml". Due to the way string literals work in Java, this boils down to the string \\bady\SShare\mart.xml, however your exception message mentions \\\\bady\\SShare\\mart.xml, which is obviously a wrong path.

So please tell us how you really get that File object and what you do to it.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • First i make the check whether the file exists using f.exists() and it returns false. Is there a better way of checking file existence present on network drive? Then i try to parse the input xml file using JDOM, so i create a SAXBuilder object and call build on that passing the file instance and now i see the following error - java.net.ConnectException: Connection refused: connect – Piyush Mattoo Mar 09 '11 at 17:34
0

It may be a permissions-related issue: I had this problem in the past. Check that the user from which you run your java code has permissions to access the shared network drive.

MarcoS
  • 13,386
  • 7
  • 42
  • 63
0

I was doing documentBuilder.parse(anExistingFile), the network error looks strange indeed because you read a file and do nothing network related, but the issue with the network is validation xml using external dtd (proxy issue in my case). Somewhere inside the parse method it will try to download the external dtd and it causes an network related error message.

So in case you do not care about the dtd validation and only want to read some xml values: Make DocumentBuilder.parse ignore DTD references

Or fix your proxy / network issues.

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85