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.
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.
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.
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: