Some quick JDK 7 and Netbeans installation updates!

No Comments

Hi all,

John discovered that our instructions on the Debian (and therefore Ubuntu) install was not following the de-facto standards. In short the location of where you install the JDK has been altered for more flexibility and security. The post has now been updated with the corrected details.

I also recently discovered that the Netbeans 7.0 Beta 2 won’t always start with JDK1.6.0_24 and the -J-XX:MaxPermGen parameter. If this happens to you then you can remove the -J-XX:MaxPermGen from the %Netbeans_Home%/etc/netbeans.conf file.

Hope the Java 7 explorations are going well! We’ll be posting more code samples to run with shortly as well as a later piece on project Jigsaw (which is more Java 8, but hey!)

Cheers,
Martijn

PS: If you are interested in the book, the 50% promo code runs out on the 3rd of March, so get in fast!

Did you like this? Share it:

The Early Access Program has been updated!

No Comments

The 2nd release for the book has reached the MEAP (Manning Early Access Program).

This means that you can pre-order it, and get access to the chapters as we write them. There’s now an updated ToC, an updated Chapter 1 (Introducing Java 7) is available for free, with Chapters 2 and 3 (and now Chapter 4 – concurrency!!) available to people who pre-order.

Until the 3rd of March you can use our discount code wellgrndjava50 and get 50% off!

To download chapter 1, or pre-order, see our page on Manning’s website.

Did you like this? Share it:

Java 7 Developer preview (aka build 130) is here!

No Comments

So we’ve blogged previously about how to get JDK 7 builds running with Netbeans 7.0 Beta 2. As of today the first official developer release of JDK 7 has been released! For those of you early adopters, this is in fact simply b130, but it reaches an important milestone, that of stability. See http://blogs.sun.com/mr/entry/jdk7_preview for details on this latest release.

Did you like this? Share it:

Setting up and running NetBeans IDE 7.0 Beta 2 with JDK 7

11 Comments

Oracle has released NetBeans IDE 7.0 Beta 2!  Some developers might be disappointed with the discontinuation of official Ruby support, or the fact that JUnit is no longer being bundled by default. Happily we have good news on both of those fronts with the Ruby community stepping up to support the Netbeans Ruby plugin, and the Oracle engineers cleverly getting around the JUnit legal stoush by prompting you to immediately download and install JUnit upon starting Netbeans for the first time.

Of course there’s one extra big reason to be cheerful with Beta 2 coming out, seamless JDK7 support.  Beta 2 supports the latest JDK7 builds allowing you to try out the new JDK 7 features such as Project Coin:

  • try-with-resource statements
  • Simplified varargs method invocation
  • Multi-catch and more precise rethrow
  • Strings in switch
  • and more!

For you bold explorers you can get further information on all of the JDK 7 features.

Download & Installation of Netbeans 7.0 Beta 2

A full detailed description of installing, uninstalling and troubleshooting instructions for this version of NetBeans can be found on the official website.

First and foremost navigate to the official download site, select your favourite IDE configuration and click on one of the “Download” buttons.

Before installing it, make sure you have at least JDK 6 installed (preferably update 24), as NetBeans IDE 7.0 Beta 2 cannot be installed using JDK 5. Should you have a recent JDK 7 build on your computer (from following our earlier blog post), then the installer will recognize it without any problems.  If you install JDK 7 after Netbeans you can follow the instructions below to configure Netbeans 7.0 Beta 2 with JDK 7.

Configuring NetBeans to work with JDK 7

  1. Create a new Java Application project
  2. Right-click on the project root and select “Properties”
  3. Select “Libraries” on the left side and make sure “JDK 1.7” is selected at the Java Platform option. If not, click on “Manage Platforms” >> “Add Platform” then navigate to the installation folder of the JDK (e.g. in Debian it’s under /usr/lib/jvm/jdk1.7.0 ), then click “Next”, type a platform name unless it is not automatically detected, then press “Finish”
  4. Select “Sources” on the left side and make sure JDK 7 is selected under Source/Binary Format option
  5. Click OK and you’re good to go

Trying out new features of Java SE 7

You can go ahead and copy/paste the following code snippets directly in your IDE. They reflect some of the changes from the new Java specification.

Try-with-resources (TWR)

In the following example, the HTML content from an URL is read and written in a file. Notice that the TWR feature automatically handles closing of the FileOutputStream and InputStream resources.

  URL url = new URL("http://www.java7developer.com/blog/?page_id=97");
  try (FileOutputStream fos = new FileOutputStream(new File("output.txt"));
        InputStream is = url.openStream())
  {
    byte[] buf = new byte[4096];
    int len;
    while ((len = is.read(buf)) > 0)
    {
      fos.write(buf, 0, len);
    }
  }
  catch (IOException e)
  {
    e.printStackTrace();
  }

Now as Alan Bateman (lead for the NIO.2 project) has kindly pointed out, the latest developer preview of JDK 7 (build130) makes the actual file I/O work much simpler. The above code can be reduced down to:

URL url = new URL("http://www.java7developer.com/blog/?page_id=97");
try (InputStream in = url.openStream())
{
  Files.copy(in, Paths.get("output.txt"));
}
catch(IOException ex)
{
  ex.printStackTrace();
}

The sharp eyed amongst you will also realise we haven’t dealt with the MalformedURLException either. We’d love to see your versions of the above code with that handling factored in, please do post in the comments or on our google group!

Strings in the switch statement

The next example shows a simple usage of the switch statement containing a String as argument. In Java 6 and previous versions only byte, char, short, int and enum constants (along with the Byte, Character, Short and Integer wrappers) are allowed as values for the cases, whereas JDK 7 adds support for String objects

public void printDay(String dayOfWeek)
{
  switch (dayOfWeek)
  {
    case "Sunday": System.out.println("Dimanche"); break;
    case "Monday": System.out.println("Lundi");    break;
    case "Tuesday": System.out.println("Mardi");   break;
    case "Wednesday": System.out.println("Mercredi"); break;
    case "Thursday":  System.out.println("Jeudi");    break;
    case "Friday":    System.out.println("Vendredi"); break;
    case "Saturday":  System.out.println("Samedi");   break;
    default: System.out.println("Error: '" + dayOfWeek + "' is not a day of the week"); break;
  }
}

Handling Several Different Exceptions

The code below shows the new approach to ‘catching’ exceptions in JDK 7 – two or more exceptions can be grouped in the same catch statement, provided that the error handling code treats the exception argument as the common supertype of the exceptions which are part of the catch statement.

public Configuration getConfig(String fileName_)
{
  Configuration cfg = null;
  try
  {
    String fileText = getFile(fileName_);
    cfg = verifyConfig(parseConfig(fileText));
  }
  catch (FileNotFoundException | ParseException | ConfigurationException e)
  {
    System.err.println("Config file '" + fileName_ + "' is missing or malformed");
  }
  catch (IOException iox)
  {
    System.err.println("Error while processing file '" + fileName_ + "'");
  }
  return cfg;
}

As JDK 7 is more or less feature-complete (with API changes due to be halted at the end of April), you have the freedom of experimenting all its new functionalities, and using the NetBeans IDE 7.0 Beta 2 version will make your experience easier and more straightforward than before.

Did you like this? Share it:

We have a Facebook Page!

No Comments

Yes, for all of you crackbookI mean Facebook users, we now have our very own page for “The Well-Grounded Java Developer”. Please do visit us there and tell your colleagues!

Did you like this? Share it:

Getting started with JDK7

8 Comments

We are all aware that the next version of Java is about to make its flashy entrance this July; We’ve seen the list of features it brings to the table and we are nothing less than anxious to take the JDK for a “test drive”. So why not do just that?

Even though the current build (127 at the time this post was written) is available for download, we must take into account that it is still under development, so don’t expect the binaries you download now to provide the same functionality and stability as the stable release which we will get to see in summer. However, you have an opportunity to get an early impression of how the new Java version will behave at your fingertips and you should definitely take advantage of it. Moreover, if you want to track the progress of the various projects composing JDK 7, you may want to subscribe to their mailing lists.

On January 13th 2011, Mark Reinhold – chief architect of the Java Platform Group at Oracle – announced on the jdk7-dev mailing list: “The JDK 7 Project has reached a major milestone: It is Feature-Complete. This means that all of the planned features have been implemented and integrated into the master forest, along with their unit tests, and all other planned tests have been written and run on a representative set of platforms”.

Without further ado, below is a step-by-step detailed description of downloading and setting up the latest JDK 7 build (for Debian/Windows/MacOS X) on your machine.

Debian

1. Download binaries from http://download.java.net/openjdk/jdk7/

Alternatively, open a terminal and type:

  • for 32 bit OS:
    wget http://www.java.net/download/jdk7/archive/b125/binaries/jdk-7-ea-bin-b125-linux-i586-13_jan_2011.bin
  • for 64 bit OS:
    wget http://www.java.net/download/jdk7/archive/b125/binaries/jdk-7-ea-bin-b125-linux-x64-13_jan_2011.bin

2. Run the previously downloaded self-extracting .bin file (first make sure you have executing permissions by chmodding those files to have +x access), by typing in the command line:

  • for 32 bit OS
    ./jdk-7-ea-bin-b125-linux-i586-13_jan_2011.bin
  • for 64 bit OS
    ./jdk-7-ea-bin-b125-linux-x64-13_jan_2011.bin

Accept the license agreement by writing “yes” in the command line and the JDK 7 will unpack itself in the same folder as the .bin file . You are required to press “enter” to finish unpacking.

3. Move the newly unpacked jdk1.7.0 folder to the /opt/jvm/ folder – creating /opt/jvm folder first:

sudo mkdir /opt/jvm ; sudo mv jdk1.7.0 /opt/jvm

If you have more than one JDK’s installed, type:

sudo update-alternatives –-config java.

This will get you a list of the installed JDK’s and their indexes. To install JDK 7 you just type:

sudo update-alternatives –-install /usr/bin/java java /opt/jvm/jdk1.7.0/jre/bin/java 2

where 2 is the next unused index from the previous command.  This should be repeated for javac and javadoc if you are going to use them via the command line. Finally check that the proper Java is installed by typing:

java -version

Windows

  1. Download binaries from: http://dlc.sun.com.edgesuite.net/jdk7/binaries/index.html
  2. Run the downloaded .exe file and go through the installation wizard, we recommend installing it to “C:\Java\jdk1.7.0_build<x>”
  3. Set JAVA_HOME & PATH environment variables: In the command line write:set JAVA_HOME=”C:\Java\jdk1.7.0″
    set PATH=C:\Java\jdk1.7.0\bin;%PATH%

NOTE: GUIs for setting path variables can be found here:

4. [OPTIONAL]run java -version & javac -version to check that they point to the correct jdk version

MacOS X

On 12 January 2011, Richard Mayhew wrote an article on The Server Side about the MacOS and JDK 7 tandem. He stated the following: “Just in case you didn’t know: wikis.sun.com has directions for building OpenJDK7 for MacOS. Also, code.google.com has a prebuilt JDK7 for 32-bit and 64-bit MacOS.” Following the links provided in this article you will be able to set up JDK 7 on your Mac OS.

You are now ready to start juggling with the new features which JDK 7 has to offer. You may test your code in your favourite text editor, or perhaps you wish to try Netbeans 7.0 Beta IDE which introduces language support for development to the Java SE 7 specification with the JDK 7 platform.

Enjoy!  Dragos (our fantastic intern!), John, Ben & Martijn

Did you like this? Share it:

50% Discount Promo Code For Our MEAP

2 Comments

The lovely people at Manning have provided a discount code, so that readers of our blog can get a whopping 50% off our MEAP.

To use it, just go to our page on Manning’s website http://www.manning.com/evans/ and then enter the code: wellgrndjava50 as you go through checkout, to get the MEAP (and a printed book if you want one, when it’s released) at a 50% discount.

There’ll be new content (including Chapter 4) for the MEAP very soon, so this is an excellent time to get your copy.

Did you like this? Share it: