blob: bedacdb7d5968176d5267aef78d1b66ffd6e2ee8 [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>NetBeans IDE Java Quick Start Tutorial</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 very simple and quick introduction to the NetBeans IDE workflow by walking you through the creation of a
simple Hello World Java console application." >
</head>
<body>
<h1>NetBeans IDE Java Quick Start Tutorial</h1>
<p>Welcome to NetBeans IDE!</p>
<p>This tutorial provides a very simple and quick introduction to the NetBeans IDE workflow by walking you through the creation of a
simple &quot;Hello World&quot; Java console application. Once you are done with this tutorial, you will have a general knowledge of how
to create and run applications in the IDE.</p>
<p>This tutorial takes less than 10 minutes to complete.</p>
<p>After you finish this tutorial, you can move on to the learning trails,
which are linked from the <a href="../../index.html">Documentation, Training &amp; Support</a> page.
The learning trails provide comprehensive tutorials that highlight a wider range of
IDE features and programming techniques for a variety of application types. If you do not want to do a &quot;Hello World&quot; application,
you can skip this tutorial and jump straight to the learning trails.
</p>
<p><b>Contents</b></p>
<img src="../../../images_www/articles/69/netbeans-stamp-69-70-71.png" class="stamp" alt="Content on this page applies to NetBeans IDE 6.9 and later" title="Content on this page applies to the NetBeans IDE 6.9 and later">
<ul class="toc">
<li><a href="#setup" title="Setting Up the Project">Setting Up the Project</a></li>
<li><a href="#code" title="Adding Code to the Generated Source File">Adding Code to the Generated Source File</a></li>
<li><a href="#run" title="Compiling and Running the Program">Compiling and Running the Application</a></li>
<li><a href="#build" title="Building and Deploying the Application">Building and Deploying the Application</a></li>
<li><a href="#nextsteps" title="Next Steps">Next Steps</a></li>
</ul>
<p><b>To complete this tutorial, you need the following software and resources.</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="https://netbeans.org/downloads/index.html">NetBeans IDE</a></td>
<td class="tbltd1">version 6.9 and later</td>
</tr>
<tr>
<td class="tbltd1"><a href="http://java.sun.com/javase/downloads/index.jsp">Java Development Kit (JDK)</a></td>
<td class="tbltd1">version 6 or version 7</td>
</tr>
</tbody>
</table>
<h2>Setting Up the Project<a name="setup"></a></h2>
<p>To create an IDE project:</p>
<ol>
<li>Start NetBeans IDE.</li>
<li>In the IDE, choose File &gt; New Project (Ctrl-Shift-N), as shown in the figure below.
<p class="align-center"><img src="../../../images_www/articles/71/java/quickstart/new-proj.png" alt="NetBeans IDE with the File &gt; New Project menu item selected." class="margin-around"></p>
</li>
<li>In the New Project wizard, expand the Java category and select Java Application as shown in the figure below. Then click Next.
<p class="align-center"> <img src="../../../images_www/articles/71/java/quickstart/proj-wizard.png" alt="NetBeans IDE, New Project wizard, Choose Project page." class="margin-around"></p>
</li>
<li>In the Name and Location page of the wizard, do the following (as shown in the figure below):
<ul>
<li>In the Project Name field, type <code>HelloWorldApp</code>.</li>
<li>Leave the Use Dedicated Folder for Storing Libraries checkbox unselected.</li>
<li>In the Create Main Class field, type <code>helloworldapp.HelloWorldApp</code>.</li>
<li>Leave the Set as Main Project checkbox selected.</li>
</ul>
<p class="align-center"><img src="../../../images_www/articles/71/java/quickstart/proj-wizard2.png" alt="NetBeans IDE, New Project wizard, Name and Location page." class="margin-around"></p>
</li>
<li>Click Finish.</li>
</ol>
<p>The project is created and opened in the IDE. You should see the following components:</p>
<ul>
<li>The Projects window, which contains a tree view of the components of the project,
including source files, libraries that your code depends on, and so on.
</li>
<li>The Source Editor window with a file called <code>HelloWorldApp</code> open.
</li>
<li>The Navigator window, which you can use to quickly navigate between
elements within the selected class.</li>
<li>The Tasks window, which lists compilation errors as well other tasks
that are marked with keywords such as XXX and TODO.</li>
</ul>
<p class="align-center"><img src="../../../images_www/articles/71/java/quickstart/proj-opened.png" alt="NetBeans IDE with the HelloWorldApp project open." class="margin-around"></p>
<h2><a name="code"></a>Adding Code to the Generated Source File
</h2>
<p>Because you have left the Create Main Class checkbox selected in the
New Project wizard, the IDE has created a skeleton main class for you.
You can add the &quot;Hello World!&quot; message to the
skeleton code by replacing the line:</p>
<pre class="examplecode">
// TODO code application logic here
</pre>
with the line:
<pre class="examplecode">
System.out.println(&quot;Hello World!&quot;);
</pre>
<p>Save the change by choosing File &gt; Save.</p>
<p>The file should look something like the following code sample.</p>
<pre class="examplecode">
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package helloworldapp;
/**
*
* @author &lt;your name&gt;
*/
public class HelloWorldApp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println(&quot;Hello World!&quot;);
}
}
</pre>
<a name="run"></a><h2>Compiling and Running the Program</h2>
<p>Because of the IDE's Compile on Save feature, you do not have to manually compile
your project in order to run it in the IDE. When you save a Java source file, the IDE
automatically compiles it.</p>
<p class="notes">The Compile on Save feature can be turned off in the Project Properties window. Right-click your project, select Properties. In the Properties window, choose the Compiling tab. The Compile on Save checkbox is right at the top. Note that in the Project Properties window you can configure numerous settings for your project: project libraries, packaging, building, running, etc. </p>
<p><b>To run the program:</b></p>
<ul>
<li>Choose Run &gt; Run Main Project (F6).</li>
</ul>
<p>The next figure shows what you should now see.</p>
<p class="align-center"><img src="../../../images_www/articles/71/java/quickstart/run-output.png" ALT="The program prints Hello World! to the Output window (along with other output from the build script)." class="margin-around"></p>
<p>
Congratulations! Your program works!
</p>
<p>If there are compilation errors, they are marked with red glyphs
in the left and right margins of the Source Editor. The glyphs in the left
margin indicate errors for the corresponding lines. The glyphs in the right
margin show all of the areas of the file that have errors, including errors
in lines that are not visible. You can mouse over an error mark to get a
description of the error. You can click a glyph in the right margin to jump
to the line with the error.
</p>
<a name="build"></a>
<h2>Building and Deploying the Application </h2>
<p>Once you have written and test run your application, you can
use the Clean and Build command to build your application for deployment.
When you use the Clean and Build command, the
IDE runs a build script that performs the following tasks:</p>
<ul>
<li>Deletes any previously compiled files and
other build outputs.</li>
<li>Recompiles the application and builds a JAR file
containing the compiled files.</li>
</ul>
<p><b>To build your application:</b></p>
<ul>
<li>Choose Run &gt; Clean and Build Main Project (Shift-F11) </li>
</ul>
<p>You can view the build outputs by opening the Files window and expanding
the <tt>HelloWorldApp</tt> node. The compiled bytecode file <code>HelloWorldApp.class</code> is within the <code>build/classes/helloworldapp</code> subnode.
A deployable JAR file that contains the <code>HelloWorldApp.class</code> is within the <code>dist</code> node.</p>
<p class="align-center"><img src="../../../images_www/articles/71/java/quickstart/files-window.png"
alt="Image showing the Files window with the nodes for the HelloWorldApp expanded to show the contents of the build and dist subnodes." class="margin-around"></p>
<p>For information on how to run the application from the command line for your operating system, see the <a href="http://docs.oracle.com/javase/tutorial/getStarted/cupojava/index.html" target="_blank">&quot;The &quot;Hello World&quot; Application&quot;</a> lesson of the Java Tutorials. </p>
<p>&nbsp;</p>
<div class="feedback-box"><a href="/about/contact_form.html?to=3&subject=Feedback: NetBeans IDE Java Quick Start Guide">Send Feedback on This Tutorial</a></div>
<p>&nbsp;</p>
<h2><a name="nextsteps"></a>Next Steps</h2>
<p>You now know how to accomplish some of the most common programming tasks in the IDE.</p>
<p>To learn more about the IDE workflow for developing Java applications,
including classpath management,
see <a href="../../docs/java/javase-intro.html">Developing General Java Applications</a>.</p>
<p>For more information on deploying Java desktop applications, see <a href="../../docs/java/javase-deploy.html">Packaging and Distributing Java Desktop Applications</a>.</p>
<p>To find information specific to the kind of applications you are developing, use the NetBeans IDE
learning trail for that type of application. Each learning trail contains a series of tutorials and
guides that range in scope from basic to advanced. The following
learning trails are available:</p>
<table width="100%">
<tr>
<td>
<ul>
<li><a href="../../trails/java-se.html">General Java Development </a></li>
<li><a href="../../trails/tools.html">Integration with External Tools and Services </a></li>
<li><a href="../../trails/matisse.html">Java GUI Applications</a></li>
<li><a href="../../trails/web.html">Web Services Applications</a></li>
<li><a href="../../trails/java-ee.html">Java EE &amp; Java Web Applications</a></li>
</ul>
</td>
<td>
<ul>
<li><a href="../../trails/php.html">PHP Applications</a></li>
<li><a href="../../trails/platform.html">NetBeans Platform and Module Development</a></li>
<li><a href="../../trails/cnd.html">C/C++ Applications</a></li>
<li><a href="../../trails/mobility.html">Mobile Applications</a></li>
</ul>
</td>
</tr>
</table>
</body>
</html>