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: