Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate Java code to a more recent jdk (e.g. 1.8)

What is the best way to migrate Java code of an older jdk (1.5) to a more recent Java version (1.8) to provide from its new features and improvements. We have a large Java jdk 5 code base and want to migrate to jdk 8.

There are a lot of compiler warnings and hints (e.g. diamond operator, multicatch, unnecessary (un)boxing, etc) which will improve the performance, code readability, etc.

We are using Netbeans IDE. Are there any plugins which we can use or are there migration scripts?

like image 347
R. Oosterholt Avatar asked Nov 10 '15 20:11

R. Oosterholt


2 Answers

The likelihood of your code being incompatible with Java 8 is slim, since Java has taken great strides to ensure backwards compatibility with all previous versions.

The issues that you'll likely run into lie much deeper, likely in implementations of collections or methods who have changed over the years.

If you don't have a test suite that covers the critical paths of your code, start there. You'll need that test suite to ensure that the migration hasn't horribly broken anything.

Next, peruse the compatibility guides for Java 1.7 and Java 1.8 and be sure that nothing that you're using in particular is impacted by those changes.

Lastly, the code cleanup piece can be tackled, but it shouldn't be addressed right now. The only thing you need to concern yourself with is to get the platform running on the new version of Java. As you work in the code base, discipline yourself and the team to use the newer Java idioms, such as the diamond notation, and try-with-resources where applicable.

like image 88
Makoto Avatar answered Nov 11 '22 11:11

Makoto


Unfortunately, there are no magical ways to achieve what you are asking, but here are a few pointers that can make it easier for you to migrate the code to JDK 1.7 (note that JDK 1.8 has been out for some time now, and 1.7 is already out of support officially by Oracle):

  • Use checkstyle or a similar plugin in Eclipse to find the problems

  • Build your project with JDK compiler level 1.7 in Eclipse; warnings given by Eclipse are much more user friendly than the warnings printed on console by command line compiler

  • In theory, JDK 1.7 is backwards compatible with 1.5. The only exceptions are assert and enum keywords. If you used these words as user defined type/method names, you'll get a compilation error. So for most part, you can get right down to warnings. If push comes to shove, you can choose to ignore many of these warnings (of course, only if you must)

like image 1
Bloodysock Avatar answered Nov 11 '22 09:11

Bloodysock