The Java TM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

Trail: To 1.1 -- And Beyond!
Lesson: Migrating to 1.1

## How to Convert Your Program

> This section tells you how to convert programs that use deprecated API. This section does not tell you how to use the JDK's new features unless a new feature's API replaces deprecated API. For example, this section doesn't discuss substituting RMI for your existing networking code, since the original networking code is still valid. However, this section does tell you how to update to the new AWT event system, since the API for the old AWT event system is deprecated.

> This section has four subsections:

> - General Instructions
>   Provides step-by-step instructions on how to convert your programs.
> - Special Coding Techniques
>   Describes a couple of techniques for writing code that works in both 1.0 and 1.1.
> - How to Convert Code that Uses I/O
>   Shows you how to convert code that uses deprecated API in `java.io`.
> - How to Convert Code that Uses the AWT
>   Gives detailed directions on converting programs that have a GUI.

### General Instructions

1. First, save a copy of the original program -- both the Java source code (`.java` files) and the Java bytecodes (`.class` files). You'll need the copy until all the Java runtime systems the program might execute in have been converted to 1.1 or a later release. Here's an example of saving a copy of a program on a UNIX system:

   >     % cp MyClass.java MyClass.java.orig
   >     % cp MyClass.class MyClass.class.orig

2. Get a list of the deprecated API the program uses by compiling the program with deprecation warnings turned on. Make sure you're using a 1.1 compiler. Here's an example of compiling on a UNIX or Windows system:

   >     % javac -deprecation MyClass.java

   If your program calls or overrides any deprecated methods, the compiler displays a warning. For example:

   >     MyClass.java:18: Note: The method boolean handleEvent(
   >     java.awt.Event) in class java.awt.Component has been deprecated,
   >     and class MyClass overrides it.
   >         public boolean handleEvent(Event event) {
   >                        ^
   >     MyClass.java:26: Note: The method boolean handleEvent(
   >     java.awt.Event) in class java.awt.Component has been deprecated.
   >             return super.handleEvent(event);
   >                                     ^
   >     Note: MyClass.java uses a deprecated API.  Please consult the
   >     documentation for a better alternative.
   >     2 warnings

   Note: The original JDK 1.1 compiler warns you only when a program calls a deprecated method, not when it overrides it. Starting with 1.1.1, the Java compiler warns you of both overridden and called deprecated methods.

3. Now that you have identified where your program uses deprecated API, modify the program to use new alternative API instead. The following sections can help you find the best alternative API:
   - How to Convert Code that Uses I/O, for programs that use deprecated `java.io` API.
   - How to Convert Code that Uses the AWT, for programs with GUIs.
   - Alternatives to Deprecated Methods in `java.lang`, `java.net`, and `java.util`, for programs that use deprecated API in the remaining 1.0 packages. (Note that `java.applet` contains no deprecated methods or classes.)
   - Lists and Tables, for reference material that lists all new packages and classes and lists all of the deprecated API and their alternatives.

4. Once your program compiles without warnings, test it by executing it. It should work as well as or better than it worked before.

   If your program doesn't work the way you expected it to, then you have probably either implemented a new feature incorrectly or encountered an incompatible change. See the relevant section in this tutorial for help implementing any new features. See JDK 1.1 Compatibility for a list of incompatible changes between 1.0 and 1.1.

| Start of Tutorial > Start of Trail > Start of Lesson | Search