About software development for the enterprise. Focus on Java EE and more general Java platforms.
You'll read a lot about Conferences, Java User Groups, Java EE, Integration, AS7, WildFly, EAP and other technologies that hit my road.

Wednesday, October 27, 2010

Bleeding edge - First steps with OpenJDK 7 Build b115

08:10 Wednesday, October 27, 2010 Posted by Markus Eisele
,
Here it is. The guide, to making your first steps into the next version of Java we all are waiting for. Let all the others talk about politics. We are going to test drive what's there today. Prepare for some exciting times.

Preface
This is not a true beginners topic. I'll guide you through installing the binary builds and give some very brief hints at building your own OpenJDK7 version first. After that, we are going to take a look at some of the new features which are already working. But there are a couple of things, you should be aware of:
1) We are working with a development build. Expect this to be anything but mature, production ready or even stable. If you go on reading, you agree on being a geek willing to work with bleeding edge stuff. There is no other help from now on than yourself. Don't bother anyone of the OpenJDK developers with beginners questions. They are hard at work providing the complete set.
2) You should know how to write and compile Java classes without any IDE support. Non of the Eclipse or NetBeans versions were too happy about having a JDK 7 on board and they know nothing about it today. So you need to get yourself an editor and a cmd-line.

Download and install
If you are not willing to build everything yourself, you also could download a binary snapshot release. It's far easier to install this one. The list offers files for different platforms - please be sure to select the proper file(s) for your platform. Carefully review the files listed there to select the ones you want, then click the link(s) to download. The DEBUG builds have not been run through ANY quality procedures, and are only provided as a potential way to track down a bug in the more official product bits. There is no guarantee that the debug builds even will work. For a Windows Offline Installation, Multi-language JDK installation, you need the 86.22 MB jdk-7-ea-bin-b115-windows-i586-21_oct_2010.exe. It's installation is exactly what you know from previous versions. You have a couple of wizard screens. Follow them and you are done. It's better not to install the Public JRE! You are not willing to have OpenJDK7 in your browsers as the default JRE at the moment.


Download and build
If you are willing to build the OpenJDK 7 yourself. There are a couple of things you need:
- The source downloads for the OpenJDK Project. You could also grep the sources from the repositories.
- Some binary plugins. Also available from the source downloads page.
- Things listed at Minimum Build Environments
- GNU make
There is a very complete OpenJDK Build README out there, where to find more detailed explanations of what to do. If you are unhappy with it or you don't understand something. Don't try it. There are binary builds available.

First steps
Check, if your installation is valid. Switch to your %JDK7%\bin folder and type java -version.
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b115)
Java HotSpot(TM) Client VM (build 20.0-b02, mixed mode, sharing)
If you are willing to give it a try, you should put the %JDK7%\bin folder to your path variable.
Seems as if you are set. Next is to fire up some first tests. Make yourself familiar with the new features and give them a try. A good place to start is to look at Arul's post Rest of Project Coin explored, advantage Java 7. I tried the Strings in switch feature. I have waited for this too long :) Taken from Joe Darcy's Project Coin JavaOne presentation and adapted from Arul's blog. It takes a month name and a year as input and returns the days of the month in that year. Take your favorite editor and put it in (Classname and Package, as you like it).
int days = monthNameToDays("February", 2008);

public int monthNameToDays(String s, int year) {
   GregorianCalendar c = new GregorianCalendar();
    switch (s) {
        case "April":
        case "June":
        case "September":
        case "November":
            return 30;
        case "January":
        case "March":
        case "May":
        case "July":
        case "August":
        case "December":
            return 31;
        case "February":
            int days = 28;
            days += c.isLeapYear(year) ? 1 : 0;
            return days;
        default:
            return -1;
    }
}

And, as usual, compile the class and run it. For February, 2008 it returns 29 days. As expected. Great stuff! If you are looking for more advanced examples, you have to dig deeper. The new I/O API is probably a great place to look at. There is a quite old article (2008) about the new features on java.net.
Very short background: What used to be a java.io.File will now become a java.nio.file.Path. This is a more accurate name, since there was never any guarantee that a File object mapped to a real file. Also, paths can refer to both files and directories. The Path class is abstract and has no constructors. Instead, you'll ask a FileSystem object to create a path for you. This way, it can create a path that's specific to the type of file system.
import java.nio.file.*;

public class NioTest {
  public static void main(String... args) {
    FileSystem local = FileSystems.getDefault();
    Path p  =  local.getPath("bar.txt");
    String url = p.toAbsolutePath().toUri().toString();
    System.out.println("Path: " + p.toString());   
    System.out.println("Url: " + url);
    }
}

If I run it on my system I get what I expected:
E:\OpenJDK7Tests\src>java NioTest
Path: bar.txt
Url: file:///E:/OpenJDK7Tests/src/bar.txt

Further readings
This really was not everything waiting for you. You should make yourself familiar with all the new things to come. To be honest, it's not too easy to keep up with what's becoming Java SE 7 at the moment. As you might have heard, there are a couple of political things around which lead to many not technical search results, if you are googling for JDK7. The best place to start looking for further information is the openjdk.org homepage. If you are looking for a blog to subscribe to, look at the openJDK News blog or follow @OpenJDK on twitter. Next place to look are the mailing lists. Sad to say, that every project has it's own. If you are looking for some more advanced stuff, you can also have a look at Arul Dhesiaseelan's blog. He is looking into what's coming up for Java SE 8 (Lambdas, Rest of Coin ...). The offical Java Tutorials also start pushing content for JDK7 out. The Concurrency lesson now includes a chapter about the new Fork/Join framework. Also the NIO 2.0 parts are already featured. A short overview about the SDP (Sockets Direct Protocol) is also available. But be aware, that parts are still moving around. We are all waiting for the Java SE 7 JSR's to be filed and we will see the Expert Group (EG) moving around things, too. So, for the last time a friendly reminder not to take anything too serious at the very moment.
If I could make a Christmas wish, I would love to see some more Java SE 7/OpenJDK7 content coming up. @Java told me, that Santa heard my wish :) Let's look forward!