www.webdeveloper.com
Results 1 to 10 of 10

Thread: "java.io.FileNotFound " Exception while accessing a remote Server

  1. #1
    Join Date
    Nov 2004
    Location
    Nagpur(India)
    Posts
    9

    Unhappy "java.io.FileNotFound " Exception while accessing a remote Server

    Hi All

    I'm facing a very Strange Exception in my java code.

    The java file is making an HTTP connection with a remote server running on the Tomcat (server has same configuration as that on my machine...). My application sends a request in XML file to this server and receives the response in the XML file .

    Now the problem is that the first two request are processed by the server correctly....but on the third request it gives the Exception:-

    java.io.FileNotFoundException: <server-url>

    The source of the errror is from this piece of code ==>
    Code:
    
    
                path =   Config.getServerLocation(serviceId); // This gets the URL of the server
                URL url = new java.net.URL(path);
                
                con = (HttpURLConnection) url.openConnection();
                con.setUseCaches(false);
                
                con.setDoOutput(true);
                con.setDoInput(true);
                
                con.setRequestMethod("POST");
                con.setRequestProperty("Content-Type", "text/xml");
                
                OutputStream os = con.getOutputStream();
                os.write(xml.getBytes());
                
                con.connect();
                 int code = con.getResponseCode(); 
    			            
                if (code == HttpURLConnection.HTTP_OK) {
                    InputStream is = con.getInputStream(); //This InputStream 'is' is the response from the server 
    
                    return XMLHelper.loadXML(is); 
                }
    


    The stackTrace says that the Exception is thrown from the getResponseCode() which is due to the exception thrown by the HttpURLConnection.java class's method getInputStream()

    Is it a BUG in the HttpURLConnection.java for JDK1.3 ???? OR
    Am I making some mistake ???



    The environment I'm using to comile the code is JDK1.3
    & the server is deployed on Tomcat 4.0.6 .

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    did you try to compile your code using jdk 1.4.x? just for testing sake, if it works then you know that there is a problem with the jdk support for that part of the code otherwise problem with your logic.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Nov 2004
    Location
    Nagpur(India)
    Posts
    9

    Reply ::::

    Hi Khalid,

    There is no problem in my logic since the same code works through the NetBeans3.6 in which case the tomcat is running on my local machne.

    Yes I've tried compiling the code with JDK1.4.x also .....
    But that too doesn't solve the problem.....

    What I'm not getting is that How the first 2 request are fulfilled and for the next one a FNF Exception ???????

    Thanks for looking into the problem...but I need the solution desperately since It has halted my work.

    Anybody having the same or similar problem ..please share your experience...

    Thanking You all...

    Rahul

  4. #4
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    I assume that code is in a method called from somewhere else since there is only one request/response cycle there. You're not closing the connection when you're done so maybe you're hitting some arbitrary two connection limit on one end or the other.
    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Brian W. Kernighan

  5. #5
    Join Date
    Nov 2004
    Location
    Nagpur(India)
    Posts
    9
    Hi ray,

    U r correct that the method containing the code is called by other method which creates the Request XML and provide it to this function for sending this request to the Server.

    But I'm using HTTP protocol for connecting to the server and according to my knowledge HTTP is Stateless protocol. It terminates the connection after the server responds to the request.So there is no possibility of having two connections at server.So this is not the problem...

    I think the problem may be in HttpURLConnection.getResponseCode() method.... Since the exception's root cause is the statement for getting the response code from the server.

    But how the hell first 2 request are getting served by the server ???
    Rahul...
    Last edited by rahu_webdev; 01-19-2005 at 11:13 PM.

  6. #6
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    You're probably right about the connection handling. I've always done it at the socket level so I'm not used to the higher level workings of the URLConnection. Are each of these three requests exactly the same? If not, what happens if you actually repeat the first request three times? Does it still fail consistently on the third try? You've definitely got a weird one there.
    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Brian W. Kernighan

  7. #7
    Join Date
    Nov 2004
    Location
    Nagpur(India)
    Posts
    9

    Reply:::

    Hi Ray...

    I've tried with sending the same request for multiple time.But it works.

    I dont think there is any problem with the request since the same request works fine for the server when I debug the code through NetBeans . Also the requests aew well tested upon the server before implementing them in the code.

    For my ammusement how the same code & requests works in the Netbeans ????

    Note: Ray I'm closing the connection after the server responds to Request XML. I forgot to mention it earlier....

    Is it due to the different JDK used by the server & on my local machine ???

    Rahul
    Last edited by rahu_webdev; 01-20-2005 at 01:29 AM.

  8. #8
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    there is one is one options here in this scenario.
    I have noticed one thing missing and that is add a user agent type before you get the connection so that your code may look like this
    [b]con.setDoOutput(true);
    con.setDoInput(true);
    connection.setRequestProperty( Gecko/20030312","User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3);

    In my opinion it will be last thing to suspect
    con.getResponseCode();

    second you can put a delay between any 2 connections that will give your web browser a breather(though I am not sure if this is a problem).
    Try the suggestion above...and if you still have problem then you may wanna show us more of your code then this snippet
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  9. #9
    Join Date
    Nov 2004
    Location
    Nagpur(India)
    Posts
    9

    Reply::: I solved it just by changing the JDK

    Hi khalid,

    Thanks for your valuable suggestions. But I solved the problem by using a differrent JDK than jdk1.3 (which i was using previously). I tried the JDK1.3.1_13 and to my surprise it makes it working.

    Thanks for your addings..those are eally valuable one.

    Rahul

  10. #10
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    glad that changing JDK has solved it..and you are most welcome....
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles