Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii to all

I m trying to create a url using java. Address is http://www.merchant.com/pg/index.html

When i m going to run this address on browser i got error 404 page not found. There is a

index.html file in my project.

My code is

public class Url {

	/**
	 * @param args
	 */
	
		public static void main(String[] args) {
	        try {
	            //
	            // Creating a url object by specifing each parameter separately, including
	            // the protocol, hostname, port number, and the page name
	            //
	            URL url = new URL("http", "www.Merchant.com" , 80, "/pg/index.html");
	            
	            //URL url = new URL("http://Merchant.com/index.html");
	            // We can also specify the address in a single line.
	            //www.merchant.com/pg/index.html
	           
	            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
	 
	            String line;
	            while ((line = reader.readLine()) != null) {
	                System.out.println(line);
	            }
	 
	            reader.close();
	        } catch (MalformedURLException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }

	}

}

How to solve this issue? Plz help.
Posted
Updated 26-Nov-13 19:07pm
v3

1 solution

I don't think the problem is in your URL definition. After I checked the url you said, it in fact has a customized page for the HTTP Error 404, which means resource not found. However, the url http://www.merchant.com/index.html is valid and should give you the expected result.

[Explanation]
What it means is; You are trying to retrieve content from a file that does not exist in the destination URL. In order to read the URL successfully, you must first create a file in the directory named pg of your hosting server. Then in this server, copy your file or create a file that you want to be accessible. Otherwise, you will have to use a valid location to retrieve the file content.

N.B. If you the url does not point to an existing file, you will get a file not found exception...which I suppose is what you're getting in your stack trace.
 
Share this answer
 
v2
Comments
suvi from mumbai 27-Nov-13 1:14am    
Thanks for your quick reply.

If I have to create url "http://Merchant.com/pg/index.html" what are the steps should be followed

by me so that I can excess this page. . .
suvi from mumbai 27-Nov-13 5:19am    
I m getting these errors when run as a java application...

java.io.FileNotFoundException: http://www.authorize.net/pg/index.html
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1623)
at java.net.URL.openStream(URL.java:1037)
at com.kiosk.Url.main(Url.java:28)
Patrick Wanjau 27-Nov-13 5:35am    
Trying using this URL to test whether it's your code, or the destination:

URL url = new URL("http", "docs.oracle.com", "/javase/tutorial/networking/urls/creatingUrls.html");

If you get an error, then the issue is somewhere else in your code, and not the Url. But using the http://www.merchant.com/pg/index.html will not work, because that Url does not have that index file. That's why you're getting the java.io.FileNotFoundException, which is a more specific Exception of the java.io.IOException.
suvi from mumbai 27-Nov-13 7:54am    
i have this url in my code. run my program as a java application it gives me code. when run on

browser 404 error found. what does it mean....
Patrick Wanjau 27-Nov-13 8:04am    
They mean the same thing. Error 404 in browser means the page was not found, which means the file was not found, and the browser is made to give you a well formatted response for that error message. However, in Java, you have to catch the error yourself or your code will break. In short, what it means is tht your page was not found both by the browser, and your java code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900