Onward and Upward: Porting Apps to Higher JDK Versions

he IT industry is synonymous with change. Every day sees some new software version or specification released, which necessitates constant upgrades. Programming professionals often must upgrade business applications to the new versions of the software upon which they are built. To accommodate these rapidly changing business requirements, Sun Microsystems releases a JDK version with some new capabilities, enhancements, and improvements nearly every year.

This article describes the process of porting an existing Java-based application to a new JDK version and prescribes a porting process that ensures the functionality of the ported application will remain unchanged (see Figure 1).

 
Figure 1. Porting Process Diagram

Porting is the process of making software that was written for one operating environment work in another operating environment that offers new value-added features and improved performance. Porting requires changing the programming details, which can be done at the binary (application) level or the source code level. The target configuration may include a new operating system, compiler, database, and/or other third-party software that will be integrated with the base product.

Before making the decision to port, one must determine the why, what, and how of the task. Answering the following questions in the given order will help:

  1. Why migrate the existing application and/or product?
  2. What in the existing application and/or product has to be migrated?
  3. How do I migrate the application and/or product?

Why Port in the First Place?
What are the reasons or external events that necessitate the porting process? One good reason for porting is to take advantage of the new features and improved functionality of a newer JDK, but you still must determine whether that’s a real business requirement. If none of your business applications require the new features the newer JDK version introduces, then spending the time and money to port your existing application may not be a good idea. On the other hand, if you are in the business of developing software products, then it is mandatory to constantly upgrade and support the latest functionality on the market in order to give customers value for their money.

The following are some of the scenarios in which one should port an application or product:

  1. When an existing application/product stops running on the targeted JDK [The changes introduced in the JDK, which are mandatory for implementation, can cause this (e.g., changes in the existing interfaces, changes in exception handling, introduction of new keywords such as assert in JDK 1.4, etc.)]
  2. When you want to improve the performance of your existing application/product with the features introduced in the targeted JDK
  3. When you want to remove the application’s dependency on third-party products/APIs, which are now introduced as features of the targeted JDK (e.g., using of logging API instead of Log4j)
  4. When a new feature replaces an old one and the new feature will be used in all forthcoming releases
  5. When you want to use the new JDK compiler, and you want the program to compile cleanly
  6. When you are in the business of product development or producing APIs, then upgrading is a business need for client requirements as well as for competitive edge

You may be thinking: if Java truly delivers “Write Once, Run Anywhere” (WORA) functionality, porting applications between JDKs should be so trivial as to make this article moot. True, but WORA is not always the reality. Sure, when you move from a Windows to a Unix platform or vice-versa, your JDK version remains the same. However, you have to keep some key points in mind while writing the code, including:

  1. Naming conventions—Unix is case sensitive.
  2. Use of appropriate separators (File.pathSeparator and File.separator)—Unix uses ‘/’ and ‘:’ as file and path separators, while Windows uses “” and ‘;’, respectively.
  3. AWT GUI components—They need special attention. Nowadays, it is advised to use swings.
  4. Threading model—In this arena, Java’s WORA falls flat on its face. Making threading truly platform independent is a nightmare for programmers. For example, programmers are advised to break up thread-processing code written in the run method into smaller chunks and, if necessary, to call yield() at the end of a loop iteration. In a nutshell, they must keep the pre-emptive and co-operative models in mind while writing this code (Read this article for more information: An Introduction to Java Thread Programming)
  5. Internationalization—Character sets are different in Unix and Windows.
  6. Proper path usage—Avoid using absolute paths, try to use relative paths instead. If that’s not possible, try to fetch from properties files.

So, with all these factors to consider, you very well may run into problems when you try to run or compile your existing application on a newer JDK version.

What Exactly Do I Need to Port?
Once you decide to go ahead with porting, your next step is to finalize what all needs to be migrated and determine their priorities. In order to ensure a smooth porting process, you must decide this and finalize your approach during the early stages of the project.

To help sort out your priorities, JDK changes can be broadly classified into four categories:

  • Incompatibilities
  • Improvements
  • Deprecated API
  • New features in JDK

These changes can be introduced in Sun APIs as well as JDK APIs. Ideally, you should not use sun.* packages because Sun intends them only for their own usage. If you are using or extending a sun.* API in your application, be ready to port the changes or incompatibles introduced in it.

Incompatibilities
Sun technically classifies the compatibility between two JDK release versions as either binary compatibilities or source compatibilities. Binary compatibilities exist when the compiled code can run with the other version of the JDK. Source compatibility means the source code can comply and run with the other version of the JDK.

Binary compatibility has two types:

  • Upward—when compiled code can run with a higher JDK version
  • Downward—when compiled code can run with a lower JDK version

Similarly, source compatibility also has two types:

  • Upward—when source code can comply and run with a higher version the JDK
  • Downward—when source code can comply and run with a lower version of the JDK

In general, maintenance releases (e.g., JDK 1.4.1 and 1.4.2) support both upward and downward compatibility at the binary and source levels. The functional releases (e.g., JDK 1.3 and 1.4) support upward compatibility at both the binary and source levels, except for Sun’s stated incompatibilities. However, they do not guarantee downward compatibility at either level.

Removing the incompatibilities in the targeted JDK release is the top priority when porting your application, and it qualifies as the only MUST DO activity. Some of the incompatibilities you’ll find are changes in the existing interfaces, constants, exception handling, introduction of keywords (e.g., JDK 1.4 introduced assert), and removal of some methods and constants (mainly in sun.* packages). In most cases, you can compile and run code written in JDK 1.1 with JDK 1.2, 1.3, 1.4, and 1.5, as long as you don’t use any statements/APIs listed as part of the incompatibilities.

For more details about incompatibilities visit java.sun.com. The following are some other helpful links:

Improvements
JDK improvements could be lumped in the new features category, but for a clear distinction and a better understanding of tasks, it’s better to view them as a separate task. Although these activities aren’t mandatory for making your application portable, from the performance, optimization, and best practices points of view, all the changes in this category are required. Before performing all the improvement changes, however, you must weigh the trade-off between the time and effort they require and the improvement you expect to get out of them.

A few of the suggested improvements for different JDK versions are:

  • With JDK 1.1, use GregorianCalendar instead of java.util.Date for wider acceptability.
  • With JDK 1.2, use Arraylist instead of Vectors, if you are not required to synchronize.
  • With JDK 1.3, use the Timer class for scheduling future execution in a background thread instead of your own implementation.
  • With JDK 1.4, the Preference API is better than the properties file for managing user preferences and configuration data.

Deprecated API
Deprecation APIs are those that have been restructured and modified with new classes and methods that provide similar functionality. In general, whenever an API is deprecated, an alternative implementation is provided and information about it is offered in the API’s javadoc. Sun warns its users to withdraw support for deprecated APIs in future releases. Whenever possible, you should modify your application to remove references to deprecated methods/classes and to use the new alternatives. Deprecated APIs support methods and classes only for backward compatibility, and your Java compiler will generate a warning whenever you use them.

Marking something as deprecated is only one aspect of documentation and is not part of the OO paradigm. Deprecation also is not inherited, so you can still override a deprecated method without treating the subclass methods as deprecated methods. For example, say Class X has a deprecated method called getStringValue(), and Class Y extends Class X, overriding the getStringValue () method. While compiling test client TestX, which creates an instance of Class X and calls getStringValue(), the compiler generates a warning. However, while compiling test client TestY, which creates an instance of Class Y and calls getStringValue (), it doesn’t generate a warning.

You could treat a deprecated API as part of source compatibilities, but you should consider it a separate activity. Currently, you don’t have to eliminate the usage of deprecated APIs in order to port applications. In fact, the deprecated APIs from JDK 1.1 are still available in JDK 1.5 (J2SE 5). However, Sun still may withdraw their support in future releases.

Click here for a complete list of deprecated APIs still available in JDK 1.5.

New Features in JDK
Every major JDK version release provides new features in the form of new APIs and/or extensions to existing APIs. The following are some of the major features introduced in respective JDK versions:

  1. JDK 1.1 introduced inner classes.
  2. JDK 1.2 introduced swings, JDBC 2.0, and changes in Java security.
  3. JDK 1.3 introduced JNDI, RMI-IIOP, Java Sound, enhanced the Collection framework, and completed swings.
  4. JDK 1.4 introduced the JAAS Preferance API, the Logging API, JDBC 3.0, the assertion facility, and Regular expression.
  5. JDK 1.5 introduces major changes at the language level including autoboxing/unboxing, enhanced for loop, static import, typesafe enums, and varargs.
 
Figure 2. JDK Releases and the Major Feature They Introduced

For a list of new features in your targeted JDK, refer to its javadoc.

Effectively using the targeted JDK’s new features, as well as its enhanced existing features, in your existing application is part of the porting process. Sometimes, using newly added features ends your dependency on third-party software/APIs (e.g., companies that previously used log4j for logging can now use the logging facility in JDK 1.4 instead). However, if you use new features just to enhance your existing application, then it’s an enhancement activity rather than a porting activity. Enhancement activities should not be included as part of the porting process because they may lead to some regressions in existing functionality, and then determining whether these problems are because of the porting activity or because of the enhancement becomes very difficult. Still, whether or not to go for the new features is purely dependent on your business requirements. It is not a mandatory part of the porting task.

How Do I Port My Application?
Once you have identified the porting tasks that need to be performed, as well as their priorities, make an action plan to execute the porting process. Testing plays a very important role in this execution. Create base line results or use existing base line results for unit, system, performance, and acceptance testing of your current system for each platform it supports. Figure 3 shows a porting process flow diagram.

Click to enlarge  
Figure 3. Porting Process Flow Diagram

Before starting the porting process, take the following steps:

  1. Prepare a list of the following changes:
    • Incompatibilities introduced in Java API and Sun API for targeted JDK version
    • Deprecated APIs
    • Suggested improvements
    • Newly added features

    Try to estimate the number of occurrences of each identified change in the entire application source code (i.e., what is the approximate number of places you have to make changes corresponding to each JDK change introduced). Also estimate the time required for each change. Table 1 offers a sample template for capturing these details.

    Click to enlarge
    Table 1. Template for Capturing Change Details
  2. Prepare baseline results.

    Consider a scenario in which a CRM product supports the following:

    • ATG and BEA WebLogic application servers
    • Windows, Linux, and Solaris operating systems (OSs)
    • JDK 1.3

    The product needs to be ported to JDK 1.4. As discussed previously, comparing the test results on the ported platform with the original (baseline) test results is crucial. So the baseline results and actual results must be captured. Table 2 offers a template for capturing test results.

    Click to enlarge
    Table 2. Template for Capturing Test Results
  3. Capture the test results for each of the supported OSs (Windows, Linux, and Solaris) separately. If all the baseline results match the new test results of the ported application, then your porting process is almost complete.

    Another advisable practice is performing Java compatibility tests as per your requirements [e.g., Pure Java Check and J2EE CTS (Compatibility Test Suite)]. Such compatibility ensures that implementations of Java technology meet Java specifications. Table 3 presents some of the changes that you might have to perform while porting an application from JDK 1.3 to JDK 1.4.

    The areas where you’ll notice the majority of changes across different JDK versions are security, AWT, swings, I/O, exception handling, RMI/CORBA, and the collection API.

A Systematic Approach Covers All the Bases
The process and strategy this article describes may not be the only way to handle JDK porting activities, but from my experience with these types of project, it provides a comprehensive, systematic approach that can ensure a smooth process.

Credits: The author wishes to thank his project manager Atul Jain and his colleague Saurabh Bhatnagar for their valuable contributions as reviewers for this article.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job