blob: aef763e17d048d4a5d39b3c1ca6e009f0e865b45 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
-->
<html>
<head>
<title>Overview of JDK 7 Support in NetBeans IDE</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<link rel="stylesheet" type="text/css" href="../../../netbeans.css">
<meta name="KEYWORDS" content="NETBEANS, TUTORIAL, GUIDE, USER, DOCUMENTATION">
<meta name="description" content="A short guide to using JDK 7 features in NetBeans IDE. Also includes information on setting the JDK.">
</head>
<body>
<h1>Overview of JDK 7 Support in NetBeans IDE</h1>
<p>The NetBeans IDE introduced support for new JDK 7 language features, such as the diamond operator, strings in switch, multicatch, etc. When you use these constructs in your code, the IDE recognizes them, offers correct classes in code completion, correctly highlights errors, and lets you automatically fix syntax. Thus, the NetBeans IDE helps you write code that is compatible with the JDK 7 language specification. </p>
<p>In this tutorial, you learn how to enable JDK 7 support in the IDE and see how the IDE handles new language constructs. </p>
<p><b>Contents</b></p>
<img src="../../../images_www/articles/71/netbeans-stamp-71-72-73.png" class="stamp" alt="Content on this page applies to NetBeans IDE 7.0, 7.1, 7.2, or 7.3" title="Content on this page applies to NetBeans IDE 7.0, 7.1, 7.2, or 7.3">
<ul class="toc">
<li><a href="#add" title="Project Setup">Registering Java 7 in the NetBeans IDE</a></li>
<li><a href="#project">Configuring the Project to Use JDK 7</a></li>
<li><a href="#switch">Using New JDK 7 Language Constructs: Switch Statement</a></li>
<li><a href="#more">JDK 7 Support: More Examples</a></li>
<li><a href="#nextsteps" title="Next Steps">Next Steps</a></li>
</ul>
<p><b>To complete this tutorial, you need the software and resources listed in the following
table.</b></p>
<table>
<tbody>
<tr>
<th class="tblheader" scope="col">Software or Resource</th>
<th class="tblheader" scope="col">Version Required</th>
</tr>
<tr>
<td class="tbltd1"><a href="http://www.netbeans.org/downloads/index.html">NetBeans IDE</a></td>
<td class="tbltd1">version 7.0, 7.1, 7.2, or 7.3 </td>
</tr>
<tr>
<td class="tbltd1"><a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html" target="_blank">Java Development Kit (JDK)</a></td>
<td class="tbltd1">version 7</td>
</tr>
</tbody>
</table>
<h2><a name="add"></a>Registering Java 7 in the NetBeans IDE </h2>
<p><strong>To enable JDK 7 support in the NetBeans IDE: </strong></p>
<ol>
<li>Download and install JDK 7 on your system.</li>
<li>In the IDE, choose Tools &gt; Java Platforms from the main menu.</li>
<li>Click Add Platform and specify the directory that contains the JDK (e.g. on Windows, this is the JDK installation directory, default is <tt>C:\Program Files\Java\jdk1.7.0</tt>). <br>
The directory that contains the Java platform is marked with the <img src="../../../images_www/articles/70/java/javase-jdk7/platform.gif"
alt="Java Platform icon"> icon. </li>
<li>In the Platform Name step, verify that the default locations of the Platform Sources zip file and API documentation are valid. </li>
<li>Click Finish to close the Add Java Platform dialog box.</li>
<li>Ensure JDK 1.7 is chosen in the Platforms list and click Close.</li>
</ol>
<table>
<tr>
<!-- Start Embed Video -->
<td class="valign-top" class="align-center" width="480" >
<embed src="http://bits.netbeans.org/media/register-jdk7.swf" type="application/x-shockwave-flash" width="480" height="270" allowscriptaccess="always" allowfullscreen="true" autoplay="false" controller="true" scale="aspect"></embed> </td>
<td>Click <a href="http://bits.netbeans.org/media/register-jdk7.swf">here</a> to view full-sized, Ctrl-Click to download, 1,1 MB.</td>
<!-- End Embed Video -->
</tr>
</table>
<p class="notes"><strong>Note</strong>: The default Platform Javadoc API location is this <a href="http://download.java.net/jdk7/docs/" target="_blank">web URL</a>. For offline use, you can download the javadoc JAR file from the <a href="http://dlc.sun.com.edgesuite.net/jdk7/binaries/index.html" target="_blank">JDK7 binary snapshots page</a> and specify its location on your system. </p>
<h2><a name="project"></a>Configuring the Project to Use JDK 7</h2>
<p>Once you have registered JDK 7 in the IDE, you need to configure your project to use this JDK for compilation, running, and debugging. </p>
<ol>
<li>Create a Java project. Choose File &gt; New Project and select Java Application as the project type. Click Next.</li>
<li>Type <tt>SwitchTest</tt> as the project name and specify its location. </li>
<li>In the Files window, right-click the <tt>SwitchTest</tt> project's node and choose Properties &gt; Libraries. On this tab, choose JDK 1.7 from the list of Java Platforms.<br>
<img src="../../../images_www/articles/70/java/javase-jdk7/props-libs.png"
alt="Setting JDK 7 as the target format."
class="margin-around b-all"></li>
<li>Switch to the Sources tab of the Project Properties window and choose JDK 7 as the Source/Binary Format. <br>
<img src="../../../images_www/articles/70/java/javase-jdk7/props-sources.png"
alt="Specifying JDK 7 as the source format" width="573" height="431"
class="margin-around"></li>
<li>Click OK to save changes. Your project is set to recognize new JDK 7 language features.<br>
</li>
</ol>
<table>
<tr>
<!-- Start Embed Video -->
<td class="valign-top" class="align-center" width="480" >
<embed src="http://bits.netbeans.org/media/configuring-project-jdk7.swf" type="application/x-shockwave-flash" width="480" height="270" allowscriptaccess="always" allowfullscreen="true" autoplay="false" controller="true" scale="aspect"/></td>
<td>Click <a href="http://bits.netbeans.org/media/configuring-project-jdk7.swf">here</a> to view full-sized, Ctrl-Click to download, 1,3 MB.</td>
<!-- End Embed Video -->
</tr>
</table>
<h2><a name="switch"></a>Using New JDK 7 Language Constructs: Switch Statement</h2>
<p>JDK 7 brings a number of <a href="http://openjdk.java.net/projects/jdk7/features/" target="_blank">new features and enhancements</a> in different areas, including internationalization, I/O and networking, security, etc. The best way to illustrate the JDK 7 support by the IDE's Java Editor is to demonstrate a few language changes introduced by Project Coin. </p>
<p>One of these changes is a &quot;String in a switch&quot;. In the previous versions of Java, the argument of <tt>switch</tt> had to be only of the following primitive data types: <tt>byte</tt>, <tt>short</tt>, <tt>char</tt>, <tt>int</tt>, or <tt>enum</tt>. Starting from JDK 7, you can use arguments of type <tt>String</tt> in the expression of a <tt>switch</tt> statement. </p>
<ol>
<li>Open <tt>SwitchTest.java</tt> and add the following code. This small sample displays RGB codes for several colors. <br>
With JDK 7, the <tt>color</tt> variable can be a <tt>String</tt>.
<pre class="examplecode">package switchtest;
public class SwitchTest {
public static void main(String[] args) {
String color = &quot;red&quot;;
String colorRGB;
switch (color.toLowerCase()) {
case &quot;black&quot;: colorRGB = &quot;000000&quot;; break;
case &quot;red&quot;: colorRGB = &quot;ff0000&quot;; break;
case &quot;green&quot;: colorRGB = &quot;008000&quot;; break;
case &quot;blue&quot;: colorRGB = &quot;0000ff&quot;; break;
default: colorRGB = &quot;Invalid color&quot;; break;
}
System.out.println(colorRGB);
}
}
</pre>
<p class="tips">If the pasted code is formatted incorrectly in the editor, press Alt-Shift-F
to reformat.</p>
</li>
<li>In the Projects window, right-click the project's node and choose Run. You will see the output of the application, which is the RGB code for the red color. <br>
You can see that the build is successful and
the application works when the target platform and source format is JDK 7. <br>
<img src="../../../images_www/articles/70/java/javase-jdk7/output.png"
alt="Output of running the project." width="326" height="82"
class="margin-around b-all"></li>
<li>Let's rollback to using JDK 6 and test how the application is complied with the JDK 6 compiler. <br>
In the Files window, right-click the project's node and choose Properties. On the Libraries tab, set the Java Platform to <tt>JDK 1.6</tt> and on the Sources tab, set the Source Format option to <tt>JDK 6</tt>.<br>
You can immediately see that the JDK6 parser does not recognize the syntax. The compilation fails because of the incompatible variable type. <br>
<img src="../../../images_www/articles/70/java/javase-jdk7/error.png"
alt="Output of running the project." width="482" height="142"
class="margin-around b-all"></li>
<li>Now, let's rewrite the code using the <tt>if-then-else</tt> statement instead of <tt>switch</tt> as shown in the picture. <br>
With JDK 7 being the target platform, the IDE recognizes such cases and offers you to convert them to <tt>switch</tt>. <br>
Click the hint and the <tt>if-then-else</tt> construct will be automatically converted to exactly the same <tt>switch</tt> that we had before. <br>
<img src="../../../images_www/articles/70/java/javase-jdk7/convert.png"
alt="Converting the if-then-else to switch" width="398" height="320"
class="margin-around b-all"></li>
<li>Run the application with different values. </li>
</ol>
<table>
<tr>
<!-- Start Embed Video -->
<td class="valign-top" class="align-center" width="480" >
<embed src="http://bits.netbeans.org/media/jdk7-switch-statement.swf" type="application/x-shockwave-flash" width="480" height="270" allowscriptaccess="always" allowfullscreen="true" autoplay="false" controller="true" scale="aspect"></embed>
<td>Click <a href="http://bits.netbeans.org/media/jdk7-switch-statement.swf">here</a> to view full-sized, Ctrl-Click to download, 2,5 MB.</td>
<!-- End Embed Video -->
</tr>
</table>
<h2><a name="more" id="more"></a>JDK 7 Support: More Examples</h2>
<p>To demonstrate how the IDE's Java Editor recognizes and automatically fixes code to be compliant with the JDK 7 language spec, let's use a dummy code snippet, which is meaningless but contains all the major language improvements. </p>
<p>When walking through this dummy code snippet and applying editor hints, you will see the following examples of how to: </p>
<ul>
<li>Take advantage of automatic type inference, when the Java compiler is able to infer the type of a generic instance without the need to explicitly specify it. The so-called <em>diamond operator</em> is used to flag the type inference case. </li>
<li>Use improved exception handling or <em>multi-catch</em>, when one <tt>catch</tt> block can be used for several types of exceptions. <br>
</li>
<li>Use the new syntax of resource closure statements introduced by the Automatic Resource Management feature. </li>
</ul>
<ol>
<li>Replace the previous application code in the same <tt>SwitchTest.java</tt> file with the following<br>
<pre class="examplecode">
package switchtest;
import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class SwitchTest {
public void test() throws IOException {
List&lt;String&gt; list = new ArrayList&lt;String&gt;();
HashMap&lt;String, Integer&gt; map = new HashMap&lt;String, Integer&gt;();
HashMap&lt;String, Integer&gt; map2 = new HashMap&lt;String, Integer&gt;();
String a = &quot;ehlo&quot;;
try {
Method m = Object.class.getMethod(&quot;toString&quot;);
m.invoke(this);
} catch(NoSuchMethodException e) {
e.printStackTrace();
} catch(InvocationTargetException e) {
e.printStackTrace();
} catch(IllegalAccessException e) {
e.printStackTrace();
}
FileInputStream in = null;
try {
in = new FileInputStream(&quot;foo.txt&quot;);
int k;
while ((k = in.read()) != -1) {
System.out.write(k);
}
} finally {
if (in != null) {
in.close();
}
}
}
}
</pre>
</li>
<li>Note that the IDE displays several hints, as shown above in this tutorial,
of how you can optimize your code for the JDK 7 spec. Simply click on each hint and select the suggested action. <br>
</li>
<li>Finally, after you accept all the suggestions, you should have the JDK 7 compatible code shown below.<br>
<img src="../../../images_www/articles/70/java/javase-jdk7/converted.png"
alt="Converted code snippet"
class="margin-around b-all"><br>
</li>
</ol>
<table>
<tr>
<!-- Start Embed Video -->
<td class="valign-top" class="align-center" width="480" >
<embed src="http://bits.netbeans.org/media/jdk7-examples.swf" type="application/x-shockwave-flash" width="480" height="270" allowscriptaccess="always" allowfullscreen="true" autoplay="false" controller="true" scale="aspect"></embed> </td>
<td>Click <a href="http://bits.netbeans.org/media/jdk7-examples.swf">here</a> to view full-sized, Ctrl-Click to download, 1,8 MB.</td>
<!-- End Embed Video -->
</tr>
</table>
<div class="indent"></div>
<!--<br>-->
<div class="feedback-box"><a href="/about/contact_form.html?to=3&subject=Feedback: JDK 7 Support in NetBeans IDE 7.0: A Quick Overview">Send Feedback on This Tutorial</a></div>
<br style="clear:both;" />
<h2><a name="nextsteps"></a>Next Steps</h2>
<p>For more information about JDK 7 and the NetBeans IDE, see: </p>
<ul>
<li><a href="http://wiki.netbeans.org/NewAndNoteworthyNB70#JDK7_support" target="_blank">New and Noteworthy 7.0</a> - for more information about what was implemented in the NetBeans IDE.</li>
<!--<li><a href="https://blogs.oracle.com/arungupta/entry/screencast_35_jdk_7_project" target="_blank">Demo: JDK 7 Project Coin Features in NetBeans IDE 7.0 Beta</a> - a video demonstration of JDK 7 features support in the NetBeans IDE. </li>-->
<li><a href="http://docs.oracle.com/javase/tutorial/essential/io/fileio.html" target="_blank">The Java Tutorials: File I/O</a> - a section in the Java Tutorials that contains numerous examples of JDK 7 changes in I/O.</li>
<li><a href="http://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html" target="_blank">The Java Tutorials: Fork and Join</a> - explanation of the new fork/join framework in JDK7. </li>
</ul>
<p>For more information about developing Java applications in the NetBeans IDE, see: </p>
<ul>
<li><a href="javase-intro.html">Developing General Java Applications</a></li>
<li><a href="editor-codereference.html">Code Assistance in the NetBeans IDE Java Editor</a></li>
<li><a href="../../trails/java-se.html">General Java Development Learning Trail</a></li>
</ul>
</body>
</html>