How to set up a Java Project built in an earlier JDK version?
This is how you can setup your Java project when it was built in an earlier version.
Why this article?
After working on .NET projects for a few months, I’m revisiting one written in Java for App Automation. Moreover, I recently had my laptop reformatted at work because there was some inexplicable issue with the sound.
So this brings an opportunity to reinstall the project and make it work in a completely new system. Documenting this process can help anyone who’s going to start working on it.
My first realisation is that a readme file is missing.
The last time I worked on this automation project, I had issues with the SSL certificates java uses. Therefore, I couldn’t connect to most sites and more importantly, the maven repository. I needed to install an efficient REST HTTP library but was blocked. This has now been cleared with the new install.
During my University years, Eclipse and Netbeans were the defacto IDEs for developing Java projects. From what I remember, since the introduction of Android Studio, IntelliJ Idea has slowly become the industry standard.
Once you install the IDE, you can simply clone or open your Java Project in it. What I like about IntelliJ Idea, is it will automatically index the project, detect the build system and install dependencies as required. In my case, it’s Maven.
Can we use the latest JDK?
I know with IntelliJ idea you don’t need to install Java and Maven separately. If you already have it in your machine and in the OS path, it will detect it once you open the project, otherwise, you can manually choose to download a separate version e.g. the latest one.
If you’ve not installed it, you can choose a version from the Edit button.
For example, you can choose the latest version as of this writing (11/02/2022).
Now if you’re wondering what’s the difference between JDK and SDK, here is an explanation:
> The main difference between SDK and JDK is that **SDK is** a set of development tools that help developers create an application or a program on any platform. On the other hand, JDK is a set of development tools that allows a programmer to write a program using only java language.
Manage dependencies with Maven
Right-click on the pom.xml file, choose Maven and click on Download sources.
A better way though is to use Maven goals.
For example, you can reload the project dependencies using:
If you’re having any errors on your dependencies, you can visit the Maven repository and look for the appropriate libraries. For example, if I want the latest version for Appium, this is where I will go:
I’ll then choose the latest stable version I want to work with:
Furthermore, I can copy and paste the Maven dependency in my POM file.
IntelliJ idea will normally detect the change and install it.
Alternatively, you can directly install it using the IDE.
On the options at the bottom of the software, click on Dependencies.
You can choose to upgrade existing modules or install new ones.
Let’s say I want to install the Jackson library for JSON serialisation and deserialisation.
I just need to click on the Add button to the right and the IDE will install it for me.
Building the Project
Now the moment of truth prevails when you try to build the project. If there are existing issues, they will be detected here.
My current app automation project has been developed with Java 11. Upon building using the latest version (openJDK 17.0.2), there are hundreds of errors appearing that I have no idea of.
I do remember though, that the project worked fine last time I loaded it, apart from the SSL errors.
Searching on the Internet, I find no real solutions except that I should correct the errors, which can take hours if not days.
As the lazy developer I usually am, I prefer fixing it efficiently. But first I want to understand the cause.
Upon researching on sites like StackOverflow, I develop the hypothesis that the JDK version is wrong — even though in the Project Structure menu and language level I’ve selected — is the one that the project was developed with, that is 11. Yet, this does not build correctly. Maybe this feature does something else.
I try downloading, installing and using the JDK 11 that IntelliJ Idea proposes to me namely Amazon Correto 11. Even more errors now.
So I backtrack in search of the correct JDK version from Oracle.
Finally, I manage to get it from the Archives page, from where you can download most previous versions, especially those with Long Term Support.
Once I download, install this version and change it in the Project Structure, I select the Invalidate Caches option in the File menu to start setting up the project from scratch.
I clear everything and let the project launch again.
The IDE does its job of indexing and installing the Maven dependencies once again.
To be on the safe side though, I run the maven goals clean and install.
Finally, on building the project, I have only errors that I expected with the upgrade in the official Appium library. Once I correct them using IntelliJ’s magnificent refactor support, I run my test and everything works as expected.
I also take this opportunity to create and add these important details in the README.md file.
I’m not an expert in Java but being highly proficient in programming, I can debug most issues. However, someone else without enough experience can struggle for days with this set-up issue.