Java Coffee Break Newsletter Volume 3, Issue 3 http://www.javacoffeebreak.com/ ISSN 1442-3790 ================================================================= In this issue * Featured Java Websites * Book Review - Java Network Programming 2 Ed. * Book Review - Thinking in Java * Article : "Why is my applet gray?" or What causes applets to fail * Q&A : Who created Java? * Q&A : How do I get the current time? * Q&A : My classpath statement is too big, and causes an "Out of environment space" error. How do I fix it? ================================================================= /* * Need a Java course to bring your team up to speed? * Artima Software offers in-house and public seminars on * Java programming and OO design with Java, taught by * Bill Venners, a recognized expert on the Java platform. * --> http://www.artima.com/javaseminars */ ================================================================= Featured Java Websites Here are a selection of websites that may be of interest to readers. This is a new section, profiling useful resources for developers. SERIOUS GAMING WITH JAVA Looking to save a few bucks, and play some games online, rather than buying shrinkwraped software? JavaGame.net has one of the largest collections of Java games on the Net, grouped together by category. Whether your interest is strategy or arcade, board game or adventure games, kids fun or card games, there's a huge range of Java applets just waiting for you, and all for free. Who said Java wasn't designed for fun! http://www.javagame.net/ IBM RELEASES C++ STYLE PRE-PROCESSOR FOR JAVA Pre-processors allow developers to write conditional statements, such as including debug code for an internal test release, and stripping debug code out for a release edition. Now, big blue has released a pre-processor class for Java, which makes it possible to add conditional statements to your Java applications. Very cool, and available from IBM, with a good tutorial article on the topic. http://www-4.ibm.com/software/developer/library/cprep/index.html ================================================================= /* * Looking for anti-virus software from leading vendors like * Symantec or McAffee? If you want instant protection, purchase * your anti-virus products from Beyond, the Internet's leading * software superstore. McAffee Anti-Virus and Symantec's * Anti-Virus 2000 is also electronically downloadable, for fast * protection Best of all, you make the savings! * * --> http://www.beyond.com/AF10445/support/antivirus.htm */ ================================================================= Book Review - Java Network Programming 2 Ed. Author : Merlin Hughes, Michael Shoffner, Derek Hamner Publisher : Manning ISBN : 188477749X Experience: Beginner - Intermediate Java Network Programming, Second Edition aims is the sequel to the highly popular Java Network Programming title, and picks up where the original left off. It offers considerably expanded coverage of the Java networking API, for the Java 2 platform. The authors build on chapters from the previous book, adding Java 2 specific methods and classes, and covering newer topics such as servlets, and CORBA (support for which was introduced in the Java 2 platform). Readers unfamiliar with network programming, and the intricacies of sockets, are guided fairly gently through the process, with a thorough coverage of I/O streams (including files), UDP and TCP sockets, from both client and server perspective. This gives a good grounding for later development, with plenty of example clients and servers. There's also coverage of Java HTTP support, which is quite simple to work through. That said, readers familiar with the original title may be in for some disappointment. The strong cryptography coverage of the first book has vanished, mentioned only in the preface as the subject of a future book. Not being very cryptographically minded myself, I really enjoyed reading about this topic in the first title. Nonetheless, with the number of pages in this thick reference, something probably had to go, to save room for other topics. Newer topics covered in the second edition (or greatly enhanced from coverage in the first edition) include servlets, CORBA, and remote method invocation (RMI). There's even more examples than in the first edition, but there are a few gaps where coverage could have been improved (for example, the new RMI activation features are barely mentioned, and the reader is referred instead to the RMI documentation of Sun). Servlet coverage could really be improved as well - there's some great books out on the market though that can be used in companion with this book Perhaps these, and other topics will be covered in a future addition. As networking books go, Java Network Programming 2Ed is close to the top, but has room for improvement. Currently, however, you won't find a book that can beat it, but for advanced topics you'll probably need a second title for topics like servlets or distributed computing. For more information about this title, or to order it, visit http://www.davidreilly.com/goto.cgi?isbn=188477749X ================================================================= Book Review - Thinking in Java Author : Bruce Eckel Publisher : Thinking in Java ISBN : 0136597238 Experience: Beginner-Intermediate Thinking in Java is a comprehensive guide to the Java programming language, written by the Bruce Eckel (author of the hugely successful Thinking in C++ series). This is an amazing book, not only for its content but also its distribution. Bruce Eckel gives the entire text of his book in Adobe PDF format away from his website, without charging. The 'electronic' edition of this book is a companion to the print edition, which retails for a modest price considering the size of the book and the value of the content within. There's a real sense, throughout the book, of the author's personality and programming prowess. Some introductory Java books will show you how to program applets, a little GUI design, a little networking, and if you're lucky something about Swing. The focus is on the base Java APIs, and little more. Thinking in Java goes much further, tackling advanced issues like design patterns, I/O and networking, and class design. It teaches you how to program in Java, and to think like a Java programmer. The wealth of experience of the author is contained within these pages, and is an invaluable learning tool. Many readers will be tempted to download the book, rather than purchasing it. I'd highly recommend doing so - but if you're like me you'll quickly find reading such a long book on a screen tiresome. However, to whet your appetite, visit Bruce Eckel's website at http://www.bruceeckel.com/ and try it for yourself. This comprehensive guide to the Java programming language is excellent value, and a long lasting reference when working with Java. For more information about this title, or to order it, visit http://www.davidreilly.com/goto.cgi?isbn=0136597238 ================================================================= Article : "Why is my applet gray?" or What causes applets to fail Applets are both a revolutionary way to produce dynamic web-based content and a major pain in the neck for developers. In an ideal world pitched by Sun, applets are written once, and run on any computer, any browser, any operating system. This vision has been coined Write Once, Run Anywhere (WORA). What a wonderful dream! In the real world, experienced by thousands of developers, and hundreds of thousands of users, Java applets are a frustrating headache. When they work, they work great. But when they don't, the problem is hard to diagnose, and even harder to repair. Inconsistencies between browser versions and vendors, operating systems, and user configurations make for plenty of problems. Sure there is the occasional glitch in the GUI, or the slow performance on certain machine/browser configurations. But the problem goes further, to what Java developers and web users dread the most. The Ugly Gray Box of Java. What doesn't kill us makes us stronger Provided that your browser installation is stable, it is unlikely that your browser will crash or freeze (if it does repeatedly, reinstalling the browser often solves this problem). More likely is that you'll see the Ugly Gray Box of Java (almost as frustrating as the Blue Screen of Death in Win 9x). Here's an example of an applet that is guaranteed to give a UGBoJ. Depending on your browser, you may also see a cryptic message in the browser status bar. import java.awt.*; import java.applet.*; public class NaughtyApplet extends Applet { private String message = null; public void init() { // Check the length of message // It will always be null System.out.println (message.length()); } } What causes Ugly Gray Boxes? They occur when an error in the loading or execution of an applet has occurred, causing an applet crash. In Java, the technical term for these errors is an Exception. Good Java applets will "catch" these exceptions, or at least attempt to catch the most likely of errors. Many though are unforeseen, and some not even the fault of the applet. Let's look at some of the most obvious causes. Causes for Applet Exceptions The causes for applet exceptions are numerous, but some of the most common are: - * failure to locate or load a Java class * failure to access a network resource * null-pointers Failure to locate or load a Java class Some applets are composed of a single class, while others are made up of a large number of classes. Each class must be fetched individually, and if a network fault occurs during the loading of any of these classes, an exception will be thrown. These exceptions occur before the applet even fully loads, and so are hard to guard against. The easiest way though to avoid these errors is to package your applet into a .ZIP, .JAR, or .CAB archive. Netscape supports .ZIP and .JAR archives, while .CAB is specific to Internet Explorer. Failure to access a network resource Networking problems are very frustrating. Sometimes a networking host is down, and so applets can't establish network connections to it. Other times, the user is behind a firewall, and so Socket or DatagramSocket requests will not work. Please, if you write networking applets, consider using HTTP for communication rather than TCP/UDP, as the number of people behind firewalls is growing. Another cause of problems are networking applets that violate browser security restrictions. They run fine in Appletviewer, and then fail in Netscape / Internet Explorer. An applet can only connect to the host from which it is loaded. Many applets also have problems when looking up IP addresses, so specify the CODEBASE of the applet and the host it will connect to as an IP address, rather than a hostname. Null pointer exceptions Null pointer exceptions are another of the most common types of exceptions that crash applets. Unfortunately, this one is usually a bug in the applet code. When assigning an object reference to a variable, be sure to check that it is not null. For example, consider the following code: - // Get an applet parameter String count = getParameter ("count"); if (count == null) count = "1"; Notice the guard statement, which assigns a default value of no parameter was specified. There are plenty of other places where null pointers can creep in - be careful to check for them. Summary It can be frustrating for users to see the Ugly Gray Box of Java - they think that they're somehow at fault. The UGBoJ is equally annoying for developers. With a little care though, you can guard against the more common causes, and help reduce the number of gray applets on the web! ================================================================= Q&A : Who created Java? Java is still a relatively new language, so it is quite amusing to some to think that Java has a "history" behind it. One of the most frequent questions I've been getting lately though is about the origins of Java. Java was created by engineers working at Sun Microsystems. The figure that stands out most of all is James Gosling, widely regarded as the "father" of Java. James and his team were working on a language whose original name was Oak. Oak was designed for embedded devices, such as mobile phones. The first publicly available version of Java, however, was as Java applets, in the original HotJava browser. From there, Java grew to what it is today. ================================================================= Q&A: How do I get the current time? The easiest way is to declare a new instance of java.util.Date. When the default constructor is called, the date stores the current time. // Get the current time/date Date d = new Date(); System.out.println (d); If you're interested into a greater degree of precision, you can also get the current time in milliseconds. The System class (part of the java.lang package) provides a static method, currentTimeMillis(). This returns the number of milliseconds since midnight January 1, 1970. // Get the current time in milliseconds long milliseconds = System.currentTimeMillis(); ================================================================= Q&A: My classpath statement is too big, and causes an "Out of environment space" error. How do I fix it? This occurs only on Windows systems, and is a throwback to the good old days of MS-DOS. You need to expand your environment space, by adding the following line to your config.sys file: - shell=c:\command.com /p /e:32000 This specifies almost 32kb for environmental variables, which should be more than enough. If not, just increase the amount. ================================================================= The Java Coffee Break Newsletter is only sent out to email subscribers who have requested it, and to readers of the comp.lang.java.programmer and comp.lang.java.help newsgroups. If you'd like to receive our newsletter, and get the latest Java news, tips and articles from our site, then get your FREE subscription & back issues from http://www.javacoffeebreak.com/newsletter/ If you are an email subscriber and no longer wish to receive the JCB Newsletter, please unsubscribe by emailing javacoffeebreak-unsubscribe@listbot.com