November 23, 2024

Java 101, 14 years Later

As most of you know I am a computer programmer by trade. OK, I manage a team now, but I can still write code. C# code, that is, with all that comes with it: Visual Studio, IIS, deployment projects, Team Foundation, etc.

So now I’m taking this class at Georgia Tech in which I have to contribute to a team project that must be done in Java. The problem isn’t the language – Java is arguably the 2nd best programming language going – it’s the other stuff: web servers, build tools, class libraries, etc. 14 years ago I did a WebLogic project and ported it to Tomcat and WebSphere, but I’ve forgotten almost everything about that ordeal.

Today I managed to get the obligatory “Hello World!” app up and running. Big deal. And yet, some interesting lessons came from that.

Maven

First, Maven is pretty cool. My understanding is limited, but getting going is easy:

After perusing these docs I was able to create a project in Maven structure:

mvn archetype:generate -DgroupId=edu.gatech.ateam.helloworld -DartifactId=helloworld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Lesson Learned: Don’t use dashes in your groupId.  That might be OK if you specify a dash-less package parameter, but definitely not OK per the above.

JAR File Compilation

The Maven project generator creates the Hello World app, which then needs to be compiled. This is easily enough done:

mvn package

(Note that this is the point at which the dashed “hello-world” groupId caused a problem.)

When successful, Maven creates a JAR file in the project’s ./target folder. 

What’s it called?  helloworld.jar?  No, try helloworld-1.0-SNAPSHOT.jar on for size.

Execution

Given a nicely compiled project, all we’re left with is the simple task of running it.  What could be easier? Alas, in Java it’s not a simple double-click.

Given that the JAR name and groupId are as given above, change to the project directory and execute:

C:\Deve\CS6440\helloworld>java -cp target/helloworld-1.0-SNAPSHOT.jar edu.gatech
.ateam.helloworld.App

Lesson Learned:  I had a Java.exe and Javaw.exe in my C:\Windows\System32 folder, which were causing a what is evidently a common Java version error when running the app.

error: registry key ‘software\javasoft\java runtime environment’\currentversion’ has value ‘1.8’, but ‘1.7’ is required.

At first, deleting the rogue Java executables made things worse because no Java was found at all.  Adding the C:\Program Files\Java\jdk1.8.0_31\bin folder to Windows’ PATH solved the problem.

Conclusion

“Hello World!” indeed.

Next steps are to port Hello World to the web, create a WAR file using Maven, register the app as a Tomcat application, deploy it, and watch the sparks fly!

Leave a Reply