You are on page 1of 1

Stumble! I like it! more!

Share All topics 1085 favorites


1 dharmin007

Java "Get URL" code discussion


Because this Java URL source code is well
documented, I won't add much to the description.
First, I'll point out the obvious - you'll want to put
your own URL in place of the
"http://200.210.220.1:8080/index.html" in this code.
This is just a TCP/IP address we use on our internal
LAN during testing.

Of course, instead of hard-wiring the URL into the


Java code, it would be even better to read the URL
as a command-line argument, creating the URL
object "u" with a statement like this:

u = new URL(args[0]);

(You'll want to do a little error-checking in your code


to make sure args[0] was supplied by the user.)

Another item to point out is that we typically


generated a MalformedURLException whenever
we botched the actual URL, doing things like mis-
typing "http" as "htp", for instance. On the other
hand, we generated an IOException whenever we
properly typed the URL syntax, but mis-typed a
filename.

Compiling and running the program


To compile our Java URL program (after you've
downloaded it), just type this command:

javac JavaGetUrl.java

Assuming you have the JDK installed on your


system, this program should compile without
warnings or errors. Next, run the program like this:

java JavaGetUrl

You might also like