0% found this document useful (0 votes)
59 views1 page

Java Get An Url Content

This document discusses Java code for getting a URL and includes the code. It explains that the code takes a URL as a command line argument, and that errors like a malformed URL or incorrect file name would result in different exceptions being generated. It also provides the commands for compiling and running the Java program.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views1 page

Java Get An Url Content

This document discusses Java code for getting a URL and includes the code. It explains that the code takes a URL as a command line argument, and that errors like a malformed URL or incorrect file name would result in different exceptions being generated. It also provides the commands for compiling and running the Java program.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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