Saturday, January 12, 2013

Trouble Building Murach Java Code

Murach's Java Programming by Joel Murach is a popular book for people learning Java.  You can go to Murach's website and download the code examples in the book and run them.  I recommend that.  But you might run into a snag building the code (and you might not).

Here is what can go wrong and a couple of ways to fix the problem.  If you already have Java on your machine, an older version of Java, then you will have trouble building the code.  The code expects Java 7 and if you have an older version, your build will fail.

There are two ways to fix this problem.  One way is to right-click on the project, select "Properties", then where it says "Souce/Binary Format" you can pick a version of Java that you have on your machine.  I had Java 6 installed, to I selected Java 6 from the drop-down menu, hit OK, then I was able to build and run the project.  The project came from the publisher with the binary format set to Java 7 and I had to adjust that project property before the project would build because I did not have Java 7 on my machine.
Single-click on this image and you can see a larger version (right-arrow to see the other images).  Here's another fact to learn:  just because you install the latest version of Netbeans, don't think that you will get the latest version of Java when you do that.  You won't.  So when you try to build the Murach code, Netbeans will complain until you fix it either by changing the properties of the project so it expects Java 6 or by installing Java 7.

You have to go to the Oracle website to download the latest Java, which at this time is the Java SE Development Kit 7.  If you download the version for older Windows machines, you will get this executable for the installer:  jdk-7-windows-i586.exe.

After you install Java 7 on your machine, you still need to get Netbeans to recognize it.  In your project, here we are looking at the ch15_FutureValue project, select "Libraries" as shown below.
Right-click on the Libraries, select Properties, click on Manage Platforms, click on Add Platform, maneuver to the Java folder, and select jdk1.7.0.  Now that you have fixed Netbeans so it can see the Java 7 JDK, you can once again right-click on the project, select Properties, go to where it says "Souce/Binary Format" and the drop-down menu now has JDK 7 as an option.  Select JDK 7.  Now you can build your project with Java 7 as the Murach intended.  You can do a clean-and-build.  It will build without errors or warnings.  You can run the project and the Future Value Calculator works just fine.
I ran a sample set of numbers in the calculator.  If you are a young person, remember it is never too early to save for your future.

You do not really need to install Java 7.  You can set the project properties to build with Java 6 and you won't have any problems.  But when you are learning to program and a sample project that should build gives you trouble, it can be frustrating, which is why I am sharing this information.

If you are learning to program, do not get frustrated when things do not work as expected.  That happens a lot.

Robert

No comments:

Post a Comment

Comments require my approval before they appear. Be patient. It will take a day or two for your comment to appear.