java 7 from the nyjavasig

Google java 7 a bug’s life – for approach to bug

Java 7 has come a long way since the Java road show 14 months ago when I blogged about what may or may not be in Java 7. And not always forwards. Oracle sent Donald Smith (director of project management with some coding knowledge) to the NY Java Sig.  In this blog entry, we’ll look at what features made it in, the strategy discussion from Oracle and then some details.

How does Java 7 shape up compared to the road show?

Before we get to what actually happened at the sig, let’s see how Java 7 shaped up compared to the road show.

Category Feature How to Use or Status
Modularity Project Jigsaw Java 8 – modularize the JDK to address the “Java is too big” problem.  Will not replace OSGi.
Multi-lingual Support DaVinci Machine Includes invokedynamic and more. See docs
invokeDynamic Implemented in virtual machine/bytecode to make dynamic languages faster. Invokevirtual was closest but slow because doing extra type checking. Builds CallSite so only does that extra work once and improves performance of subsequent calls. JRuby noted 20% perfromance increase.
Small languages changes (Project Coin) Diamond Operator List>Map<String, Integer> list = new ArrayList<>();
Integer Literals int num = 1_234_567;
Try Catch With Resources try (InputStream s = …) {  … }
Collection literals (like associative array) Didn’t go anywhere. Haven’t seen talked about for Java 8 either.
Other While other (undetermined) features weren’t publicized last year, they will be part of Java 8.
Performance Fork Join New APIs in Java 7.  See end of post for details.
ParallelIntArray Not in Java 7. Didn’t go anywhere.
Closures Closures/Lambda Expressions Deferred to Java 8.  Will include support for multi-core.

High Level/Strategy

Donald didn’t sound like a typical Oracle speaker. He was funny, easy to relate to and started out by talking about his biases/background It felt more personal than corporate. Usualy when Oracle prents something it sounds like legal reviewed it and stripped out a lot. The slide deck did have the standard nine line Oracle disclaimer. He did note that IBM’s Sarbanes Oxley disclaimer is twice as long as Oracle’s. People asked tough questions and he answered honestly when not knowing the answer.

Interesting things from Strategy and Q&A

  1. Timelines
    • Java 6 came out in 2006.  The four years to Java 7 was the longest time between releases anywhere.
    • Java 7 update 1 is due out in early September and will contain the major bug fixes.
    • Java 7 update 2 is due out in October and will contain garbage collection enhancements.
    • Java 8 is targeting 2012.  The speaker thinks 18 months is too soon for Java 8 because the industry is no longer used to a 2 year release cycle anymore, let alone more frequent..  He thinks we need time for the tooling to catch up.
  2. Java heath (just Java; doesn’t include JVM languages)
  3. Official terms for past are “legacy sun employee” or “legacy bea employee”.
  4. HotSpot vs JRockit
    • Survey says 70% use Sun JDK and only 5% use JRockit.
    • Oracle decided to officially kill the JRockit JVM and just use the JRockit tooling. (Mission Control, Flight Recorder and RT)
    • Over time, the two will merge with the JRockit tools being premium features.
    • Oracle emphasized performance will also be free and part of the main JDK.
    • Googlefight between the two shows Hotspot as the clear winner.
  5. JavaOne – Oracle is holding back announcements for JavaOne.  Expect some on
    • Java 7 Certifications
    • Something about the Mac.  Maybe with respect to Java 8.  (Oracle claims the delay in Java 7 on the Mac is to “make things right” with the Apple UI.)
    • Recognition that Oracle needs to clean up their name in community by announcing things at JavaOne and doing them without surprises.
  6. Open jdk is slowly becoming more open. For example, Oracle recognizes the need to open bugs to public before Oracle sees/runs triage. Looking at using jira for this.
  7. Java 7 theme is “moving Java forward.”  In other words, just get something out without waiting for all the features.

More on Java 7

The emphasis was the need to be careful about protecting the platform, the amount of work in the smallest of changes and the desire to not change the type system.  Changes fall into three categories:

  1. Language changes
    • All project coin changes noted in table up top
    • + Allowing Strings in the switch statement
    • + @SafeVarargs allowing the method itself to declare safety so all callers don’t have to suppress warnings.
    • + AutoClosable interface for I/O and JDBC 4.1 resources.  Using these within the try with resources syntax in the table above means they will get automatically closed.  It also means exceptions thrown in that auto generated finally will be suppressed but still available in the stack trace.
    • + Catching multiple unrelated exceptions catch(ExceptionType1 | ExceptionType2 e)
  2. Library changes
    • NIO – Better exceptions, more extensible to different file systems, rename behavior more consistent , more access to metadata
      • Path is the replacement for File. It understands symbolic links cross platform consistently and provides many methods.
      • Lots of methods to create/navigate/transform paths
      • Can call path.toFile() to get file from path to call old apis
      • Paths helper class to get path
      • Files helper class to copy files with lots of options such as copy with attributes or replacing exisiting attributes.  Also supports atomic move.
    • Concurrency (Fork/Join)
      • Phaser class which is similar to the cyclic barrier and countdown latch but has better synchronization and deadlock protection.  Can also add and remove threads on fly.
      • TransferQueue interface which is implmented by LinkedTransferQueue – the producer or consumer can block while waiting so dont get too far ahead
      • The key class to implement your logic in should implement RecursiveTask.  It is like RecursiveAction except that it returns a result.  All you have to do is implement the compute() method.
      • The ForkJoinPool is the executor so you can submit your task to have it run.  Methods are provided to see if it is done and get the result.  By default it uses the # available processors or you can specify explicitly.
  3. Runtime changes
    • See table up top for changes.
    • Oracle listed all the languages that can run on a JVM.  They noted that some are research projects by students and not “real” or “ready.”  I laughed because C# was on the list.  Why would you want to run C# on a JRE?
  4. Other
    • Swing nimbus look and feel is completed.  Metal is still the default.
    • Eliptic curve cryptography
    • Deadlock avoidance in classloader
    • Close method for UrlClassloader
    • Javadoc now has support for CSS.  Which means the JavaDoc now has “nice looking annoying frames”

downloading java 7 for the mac and a ubuntu linux vm

I’ve been thinking about setting up a Linux VM on my Mac for a little while now.  I already have a Chrome VM.   While I installed Java 7 on my Mac yesterday from Open JDK, I also wanted an “official” version.  In case I come across anything odd, I want a way to know if it is from the Mac version being not quite ready or “the way things work.”  Which gave me a reason/excuse to install the Linux VM.  This blog entry is about how to get started.

WARNING: Java 7 is not yet production ready.  See Java 7 Ships with Severe Bug.  (it has to do with loops not functioning properly and affects Lucene and likely other things.)

For the Mac

Download and install

  1. Download Java 7 dmg file
  2. Install it
  3. Optional go to Java in system preferences to choose Java 7 as default.  (I did not do this since it sounds experimental with known buts at this point.)
  4. Note the install location to reference is /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home/bin.

Pointing to Java 7

Since Java 7 has reported stability problems, I didn’t want to set it in my PATH but only use it for selected command line invocations.  To facilitate, I set up the following in my .bash_profile:
alias javac7='/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home/bin/javac'
alias java7='/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home/bin/java'

To validate the aliases are working:

Jeanne-Boyarskys-MacBook-Pro:~ nyjeanne$ javac7 -version
javac 1.7.0-internal
Jeanne-Boyarskys-MacBook-Pro:~ nyjeanne$ java7 -version
openjdk version "1.7.0-internal"
OpenJDK Runtime Environment (build 1.7.0-internal-b00)
OpenJDK 64-Bit Server VM (build 21.0-b17, mixed mode)

Linux VM on VirtualBox

Download and install

  1. Download ubutntu iso file.   It took a number of hours to download the iso file.  (I let it run overnight.)  While the download was less than 100MB, I downloaded Lion the night before (over 3GB) so my ISP may be throttling my connection speed now.  That or a slow server.  Note that you don’t need to create a USB stick or CD.  Just the iso file is fine.
  2. Create a new VirtualBox VM and set CD drive to read from the downloaded iso file.  Even those these instructions are for Windows, they were easy to follow on a Mac.
  3. Try to downloadthe .tar.gz file for Java 7.  I got a page not found so I tried the .rpm file.  This gave me errors when I installed and tried to run it.
     javac -version gave:
    Error occurred during initialization of VM.  java/lang/NoClassDefFoundError: java/lang/Object
  4. Deleted the bad install.
  5. Tried again to download the .tar.gz file for Java 7.  This time the path was found.

Pointing to Java 7

Since my Linux machine is a VM specifically for trying Windows 7, I felt safe adding it to the beginning of my PATH in the .profile file.

A really simple Java program

A really simple program using a Java 7 feature to further test your setup.

<pre>import java.util.*;

public class JeanneTest {
  public static void main(String... args) {
    System.out.println("test");
    Set<String> test = new HashSet<>();
  }
}

Connecting to MS Access Files via JDBC in 64-bit Java

In 32-bit Java, the technique for connecting to a Microsoft Access file via JDBC connection is well-known and available as part of the Sun JDK. There are a number of errors, though, if you attempt to use a 64-bit version of Java that are not as well documented. This article points out some of those issues and a working strategy for how to successfully connect to a Microsoft Access file via JDBC in 64-bit Java.

1. Review of the 32-bit Connection Strategy

The well-known syntax for connecting to an Microsoft Access file via JDBC is as follows:

final String fileName = "c:/myDataBase.mdb";
Connection con = null;
try {
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+fileName;
	con = DriverManager.getConnection(url,"","");
} catch (Exception e) {
	// Handle exceptions
	...
} finally {
	try { if(con!=null) {con.close();} } catch (Exception e) {}
}

If you are using the Sun JDK, then the driver will be available in the classpath automatically. Notice, I make sure to close my connection object in a finally block as all good JDBC developers know to do.

2. Errors in the 64-bit World

Attempting to run the proceeding code returns the following error when using a 64-bit JDK:

[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

If you would prefer to stick with the 32-bit version of the driver, there are two options available:

  • Use the 64-bit Java installation but run java.exe with the “-D32” flag.
  • Use a 32-bit Java installation

Both of these solutions limit the amount of memory a Java application can use.

3. Adapting JDBC to 64-bit Java

If you would prefer to use the 64-bit Java, there is a solution, although for some users this may require removing Microsoft Office.

Step #1: Download the Microsoft Access Database Engine 2010 Redistributable, specifically the AccessDatabaseEngine_x64.exe file.

Step #2: Install the Microsoft Access Database Engine. If you are running a 32-bit version of Microsoft Office, you will likely get the following error when you try to install it:

You cannot install the 64-bit version of Office 2010 because you have 32-bit Office products installed.

At this point, you must decide whether or not to uninstall the 32-bit version of Microsoft Office. Newer versions of Office, such as 2010, often contain both 32-bit and 64-bit versions on the installation DVD, although the 32-bit version is used by default when installed via the AutoRun process. In this case, you would first uninstall the 32-bit version of Office. Restart the computer. Then, ignore the AutoRun on the DVD and manually open the setup executable in the x64 directory. After a 64-bit Office version is installed, continue with the Microsoft Access Database Engine installation.

Note: If you are installing a recent 64-bit version of Microsoft Office, you may be able to skip installing the Microsoft Access Database Engine, as it is often included in the Office installation.

If a 64-bit version of Office is not available, then you will unable to run the 32-bit version of Microsoft Office alongside the 64-bit Microsoft Access Database Engine, and must decide which is more important.

Step #3 Update the JDBC code to use the following revised connection string as follows:

final String fileName = "c:/myDataBase.mdb";
Connection con = null;
try {
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+fileName;
	con = DriverManager.getConnection(url,"","");
} catch (Exception e) {
	// Handle exceptions
	...
} finally {
	try { if(con!=null) {con.close();} } catch (Exception e) {}
}

After making these changes, you should be able to connect to your Access database in 64-bit Java.