Unable to find any JVMs matching version “1.7” and Mac OS X Mavericks can’t see JAVA_HOME

Background:

My .zlogin had a call to /usr/libexec/java_home 1.7 to set the JAVA_HOME to that of my Java 1.7 install, but it was an artifact of my former Mac OS X Lion configuration.

I finally decided to “fix” the problem by installing JRE 7, but found my JAVA_HOME to be /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java, and then found out that I needed to install the SDK to “fix” java_home. I made the mistake of attempting to install the Java Platform, Enterprise Edition 7 SDK (with JDK 7u45) bundle for Mac, which is a shell script. I first pointed it to the JRE 7 home, but found that that didn’t have jar in the bin directory.

I found someone else with the same problem and someone else commenting not having to use a shell script to install on Mac. It got me thinking about splitting up the installs into JDK install and EE install.

Solution:

Try installing the Java Development Kit first and then running the unbundled EE SDK as follows:

The second download is a shell script, but it works fine after JDK install (especially compared to the bundled version).


Trying to Dig a Little More In-Depth With Maven

I’ve been reading Maven: The Definitive Guide (affiliate link) as a Kindle eBook and finally got to the point of trying the first example project. The book had mentioned that maven might be installed on Mac OS X already (due to usage with some versions of XCode). Magically, it’s there:

So far, I like the book’s approach to Maven.  It evangelizes maven as a tool, but puts the purpose of Maven in context, and explains, “Why Maven?” as well as explaining that “Maven or Ant?” is the wrong question.

If you’re looking to download the files to complete the example Maven projects, they’ve moved from the URLs in the Kindle version of the book because Maven: The Definitive Guide has been split into two books, Maven by Example and Maven: The Complete Reference.

All the project examples can still be downloaded from a single zip file from the Maven by Example book. However, the chapter numbers are not the same in the Maven by Example book, and the folders in the examples are named by a chap-{title} convention.

Within the zip file:

  • Chapter 4’s project (Simple Weather Application) (originally at http://www.sonatype.com/book/mvn-examples-1.0.zip or http://www.sonatype.com/book/mvn-examples-1.0.tar.gz) is now available in the zip file under:
    • ch-custom/simple-weather
  • Chapter 5’s project (Simple Web Application)
    • ch-simple-web/simple-webapp

 

 


Java code to test if a string is a valid java identifier.

public class IdentifierTest {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        for(String arg : args) {
            boolean start = true;
            boolean validIdentifier = true;
            arg.toCharArray() 
            // commenter pointed out my error
            //for(byte b : arg.getBytes()) {
            for(char b : arg.toCharArray()) {
                if(start) {
                    validIdentifier = validIdentifier && Character.isJavaIdentifierStart(b);
                    start = false;
                } else {
                    validIdentifier = validIdentifier && Character.isJavaIdentifierPart(b);
                }
            }
            System.out.println("Identifier ""
            + arg + "" is "
            + (validIdentifier ? "" : "not ")
            + "valid");
        }
    }
}

Output:
>java IdentifierTest Test $ds ds$ 2$$
Identifier “Test” is valid
Identifier “$ds” is valid
Identifier “ds$” is valid
Identifier “2$$” is not valid


How I studied for the Sun Certified Java Programmer [SCJP] 6 Exam

Including an inline review of two SCJP study guides.

Disclaimer: This is not a guide to passing the test with zero experience and no preparation. The SCJP exam will quickly expose lack of working knowledge. I have roughly 15 years of experience with programming in C and derivative languages, including about 1 year of Java 1.1 through 1.5 and about 2 years of experience with C++.

Studying:

Note: Book links are paid Amazon associate links.

Week 1:

Week 2:

Week 3:

  • Took the “self test” questions at the end of every section of the SCJP 6 book. Got only 51% correct.
  • Hand wrote the answers or a fuller explanation of the answers for every question I got wrong.
  • Took the Programmer’s Guide mock exam. When I checked the answers, I noticed that the questions would say something like “select the 2 correct answers” and the answers would give 3 answers, or vice versa. Once I realized how bad the editing was on this book, I stopped using it.
  • Re-took the “self test” questions at the end of every section of the SCJP 6 book. Got only 88% correct. This time, I read through the answer explanations for the incorrect answers, and then re-read the “Two Minute Drill” sections for my two worst sections.

I believe that the SCJP 6 helped me because the format of the questions [choose all that apply] was ultimately harder than the format of the actual certification test questions. The Programmer’s Guide “mock test” format was closer to the actual test format, but I spent way too much time reconciling the test answers and test questions for it to be useful.

Other factors and more detail that you may not care about:

I had the opportunity through to take a class [June 7-June 11 2010] to become certified as a Sun Certified Java Programmer for Version 5. The book used in the class was [SCJP 5 study guide]. At the end of the class, we were presented with the option of registering for either the SCJP version 5 or SCJP version 6 exam by the end of June 2010. I took the risk that I could study the gaps between the Java 5 and Java 6 and pass the SCJP version 6 exam. Little did I realize that the studying effort would be a little more challenging than I first thought.

I had originally read through the Java 1.5 Tiger: A Developer’s Notebook (Java 5,Version 1.5) when Java 1.5 was brand new [5 years ago]. Since then, I have done a small amount of maintenance programming, which got me familiar with a few of the advanced concepts. In addition, the concept of generics was familiar as C++ templates and I have my basic computer science engineering classes from 16 years ago as a foundation for it all.


Boxing of an Integer and conservation of space in Java

Apparently, boxing of an int literal initialization into an Integer class will result in two different objects being assigned the same space in memory if the number is 127 or smaller, but different spaces in memory if the number is 128 or larger.

Take BoxTest.java:


public class BoxTest {
public static void main(String [] args) {
// These two objects will occupy different spaces in memory.
Integer i1 = 128;
Integer i2 = 128;

System.out.println("i1 = " + i1 + ", i2 = " + i2 + " => ");
if(i1 != i2) System.out.println("different objects");
if(i1.equals(i2)) System.out.println("meaningfully equal");

// These two, smaller objects, will occupy the same space in memory.
Integer i3 = 127;
Integer i4 = 127;

System.out.println("i3 = " + i3 + ", i4 = " + i4 + " => ");
if(i3 == i4) System.out.println("same object");
if(i3.equals(i4)) System.out.println("meaningfully equal");
}
}

The output:

[~/Dropbox/java/tech0x20] javac BoxTest.java
[~/Dropbox/java/tech0x20] java BoxTest
i1 = 128, i2 = 128 =>
different objects
meaningfully equal
i3 = 127, i4 = 127 =>
same object
meaningfully equal