blob: 36f4fe42720af2aafcb2d3bbcff5a2dc0b81ea00 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
-->
<html>
<head>
<title>Developing General Java Applications - NetBeans IDE 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="author" content="John Jullion-Ceccarelli, Patrick Keegan">
<meta name="description" content="A short guide to creating, running, and debugging a simple
Java SE application in NetBeans IDE. Also includes information on setting the JDK.">
</head>
<body>
<h1>Developing General Java Applications</h1>
<p>The following short tutorial takes you through some of the basic steps of developing
a Java SE application in NetBeans IDE.
This tutorial assumes you already have some familiarity with developing Java applications.
Along the way, you will see some of the IDE features that simplify application
development.</p>
<p>You will create an application that converts several words into a
single word that contains one letter from each of the other words. The
resulting word is called an <i>acrostic</i>.</p>
<p>This tutorial takes approximately 30 minutes to complete. If you would like
to do a quicker &quot;Hello World&quot; tutorial, see the
<a href="quickstart.html">NetBeans IDE Java Quick Start Tutorial</a>.</p>
<p><b>Contents</b></p>
<img src="../../../images_www/articles/74/netbeans-stamp-80-74.png" class="stamp" alt="Content on this page applies to NetBeans IDE 7.4 and 8.0" title="Content on this page applies to NetBeans IDE 7.4 and 8.0">
<ul class="toc">
<li><a href="#setup" title="Project Setup">Project Setup</a></li>
<li><a href="#create" title="Creating and Editing Java Source Code">Creating and Editing Java Source Code</a></li>
<li><a href="#comp" title="Compiling and Running the Application">Compiling and Running the Application</a></li>
<li><a href="#test" title="Testing and Debugging the Application">Testing and Debugging the Application</a></li>
<li><a href="#build" title="Building, Running, and Distributing the Application">Building, Running, and Distributing the Application</a></li>
<li><a href="#other-tasks" title="Other Common Tasks">Other Common Tasks</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="https://netbeans.org/downloads/index.html">NetBeans IDE</a></td>
<td class="tbltd1">7.4 or 8.0</td>
</tr>
<tr>
<td class="tbltd1"><a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Java Development Kit (JDK)</a></td>
<td class="tbltd1">6, 7, or 8</td>
</tr>
</tbody>
</table>
<h2>Project Setup<a name="setup"></a></h2>
<p>The application you create will contain two projects:</p>
<ul>
<li>A Java Class Library project in which you will create a utility class.</li>
<li>A Java Application project with a main class that implements
a method from the library project's utility class.</li>
</ul>
<p>After you create the projects, you will add the library project
to the classpath of the application project. Then you will code the application.
The library project will contain a utility class with an <code>acrostic</code>
method. The <code>acrostic</code> method takes an array of words
as a parameter and then generates an acrostic based on those words.
The MyApp project will contain a main class that calls the <code>acrostic</code>
method and passes the words that are entered as arguments when the
application is run.</p>
<p class="notes"><b>Note:</b> Strictly speaking, two projects are not needed for such a simple
application. This tutorial uses two projects to demonstrate features that you
might need for a more complex application.
</p>
<div class="indent">
<h3 class="tutorial"><a name="setup-library">Creating a Java Class Library Project</a></h3>
<ol>
<li>Choose File &gt; New Project (Ctrl-Shift-N). Under Categories, select
Java. Under Projects, select Java Class Library. Click Next. </li>
<li>Under Project Name, type <code>MyLib</code>. Change the Project Location
to any directory on your computer. From now on, this tutorial refers to this directory
as <code><i>NetBeansProjects</i></code>.
<p class="notes"><b>Note:</b> The path specified above should appear as follows in
the Project Folder field of the wizard: <code>/<code><i>NetBeansProjects</i></code>/MyLib/</code></p>
</li>
<li>(Optional) Select the Use Dedicated Folder for Storing Libraries
checkbox and specify the location for the libraries folder.
See <a href="http://www.oracle.com/pls/topic/lookup?ctx=nb8000&id=NBDAG455">Sharing a Library with Other Users</a> in <i>Developing Applications with NetBeans IDE</i></a> for more information on this option.</li>
<li>Click Finish. The MyLib project opens in both the Projects window and
the Files window.</li>
</ol>
<h3 class="tutorial"><a name="setup-application">Creating a Java Application Project</a></h3>
<ol>
<li>Choose File &gt; New Project. Under Categories, select Java. Under
Projects, select Java Application. Click Next.</li>
<li>Under Project Name, type <code>MyApp</code>. Make sure the Project Location
is set to <code><i>NetBeansProjects</i></code>. </li>
<li>(Optional) Check the Use Dedicated Folder for Storing Libraries checkbox.
</li>
<li>Enter <code>acrostic.Main</code> as the main class. </li>
<li>Ensure that the Create Main Class checkbox is checked. </li>
<li>Click Finish. The MyApp project is displayed in the Project window and
<code>Main.java</code> opens in the Source Editor.</li>
</ol>
<h3 class="tutorial"><a name="setup-classpath">Configuring the Compilation Classpath</a></h3>
<p>Since MyApp is going to depend on a class in MyLib, you have to add MyLib
to the classpath of MyApp. Doing so also ensures that classes in the MyApp
project can refer to classes in the MyLib project without causing compilation
errors. In addition, this enables you to use code completion in the MyApp
project to fill in code based on the MyLib project. In the IDE, the classpath
is visually represented by the Libraries node.</p>
<p><b>To add the library's utility classes to the project classpath:</b></p>
<ol>
<li>In the Projects window, right-click the Libraries node for the MyApp project
and choose Add Project as shown in the image below.
<p><img src="../../../images_www/articles/80/java/javase-intro/addproj.png"
alt="Image showing the Add Project command that is offered when you right-click a project's node." class="margin-around"></p></li>
<li>Browse to <code><i>NetBeansProjects</i>/</code> and select the <code>MyLib</code>
project folder. The Project JAR Files pane shows the JAR files that can
be added to the project. Notice that a JAR file for MyLib is listed even
though you have not actually built the JAR file yet. This JAR file will get
built when you build and run the MyApp project. </li>
<li>Click Add Project JAR Files.</li>
<li>Expand the Libraries node. The MyLib project's JAR file is added
to the MyApp project's classpath.</li> </ol>
</div>
<!-- =============================================================================== -->
<h2>Creating and Editing Java Source Code<a name="create"></a></h2>
<p>Now you need to create a Java package and add the method that you will use to construct
the acrostic. After that you need to implement the <code>acrostic</code> method in the
<code>Main</code> class. </p>
<div class="indent">
<h3 class="tutorial">Creating a Java Package and Class File</h3>
<ol>
<li>Right-click the MyLib project node and choose New &gt; Java Class. Type
<code>LibClass</code> as the name for the new class, type <code>org.me.mylib</code>
in the Package field, and click Finish. <code>LibClass.java</code> opens in
the Source Editor.
</li>
<li>In <code>LibClass.java</code>, place the cursor on the line after the class
declaration (<code>public class LibClass {</code>.</li>
<li>Type or paste in the following method code: <br>
<pre class="examplecode"> public static String acrostic(String[] args) {
StringBuffer b = new StringBuffer();
for (int i = 0; i &lt; args.length; i++) {
if (args[i].length() &gt; i) {
b.append(args[i].charAt(i));
} else {
b.append('?');
}
}
return b.toString();
}</pre></li>
<li>If the code that you pasted in is not formatted correctly, press Alt-Shift-F
to reformat the entire file.</li>
<!--note: reformat doesn't work for mac-->
<li>Press Ctrl-S to save the file.</li>
</ol>
<h3 class="tutorial">Editing a Java File</h3>
<p>Now you will add some code to <code>Main.java</code>. In doing so,
you will see the Source Editor's code completion and
code template (abbreviation) features.</p>
<ol>
<li>Select the <code>Main.java</code> tab in the Source Editor. If it isn't already
open, expand MyApp &gt; Source Packages &gt; acrostic in the Projects window and double-click
<code>Main.java</code>. </li>
<li>Delete the <code>// TODO code application logic here</code> comment in the
<code>main</code> method.</li>
<li>In place of the comment type the following:
<pre class="examplecode">String result = Li</pre>
<p>Leave the cursor immediately after <code>Li</code>. In the next step you will
use code completion to turn <code>Li</code> into <code>LibClass</code>.</p>
</li>
<li>Press Ctrl-Space to open the code completion box.
<p>A short list of possible ways to complete the word appears.
However, the class that you want, <code>LibClass</code> might not be there.</p></li>
<li>Press Ctrl-Space again to display a longer list of possible matches.
<p><code>LibClass</code> should be in this list.</p></li>
<li>Select <code>LibClass</code> and press Enter. The
IDE fills in the rest of the class name and also automatically creates an
import statement for the class.
<p><b class="notes">Note:</b> The IDE also opens a box above the code completion box that
displays Javadoc information for the selected class or package. Since there
is no Javadoc information for this package, the box displays a &quot;Cannot
find Javadoc&quot; message.</p></li>
<li>In the main method, type a period (.) after <code>LibClass</code>. The code
completion box opens again.</li>
<li>Select the <code>acrostic(String[]args)</code> method
and press Enter. The IDE fills in the <code>acrostic</code> method and
the highlights the <code>args</code> parameter. </li>
<li>Press Enter to accept <code>args</code> as the parameter.</li>
<li>Type a semicolon (;).
<p>The final line should look like the following line.</p>
<pre class="examplecode">String result = LibClass.acrostic(args);</pre>
</li>
<li>Press Enter to start a new line. Then type <code>sout</code> and press Tab.
The <code>sout</code> abbreviation expands to <code>System.out.println(&quot;&quot;);
</code>with the cursor positioned between the quotation marks. Type <code>Result
=</code> inside the quotation marks and <code>+ result</code> after the end quotation
mark.
<p>The final line should look like the following line.</p>
<pre class="examplecode">System.out.println(&quot;Result = &quot; + result);</pre>
</li>
<li>Press Ctrl-S to save the file.</li>
</ol>
<p class="notes"><b>Note:</b> <code>sout</code> is one of many code templates
that are available in the Source Editor.
To find and edit the list of code templates, choose Tools &gt; Options &gt; Editor &gt; Code Template.</p>
</div>
<h2>Compiling and Running the Application<a name="comp"></a></h2>
<p>Now you need to set the main class and execution arguments so that you can run the
project. </p>
<p class="notes"><b>Note:</b> By default, the projects have been created with the Compile on Save
feature enabled, so you do not need to compile your code first
in order to run the application in the IDE. For more information, see
<a href="http://www.oracle.com/pls/topic/lookup?ctx=nb8000&id=NBDAG525">Compiling a Single Java File</a> in <i>Developing Applications with NetBeans IDE</i></a>. </p>
<div class="indent">
<h3 class="tutorial">Setting the Main Class and Execution Arguments</h3>
<p>The output of this program is based on arguments that you provide when
you run the program. As arguments, you will provide five words, from which
the acrostic &quot;Hello&quot; will be generated. The acrostic is assembled from
the first letter of the first word, the second letter of
the second word, the third letter of the third word, and so on. </p>
<p><b>To add the arguments for the IDE to use when running the application:</b></p>
<ol>
<li>Right-click the MyApp project node, choose Properties, and select the
Run node in the dialog's left pane.
<p>The main class should already be
set to <code>acrostic.Main</code>.</p></li>
<li>Type <code>However&nbsp;we&nbsp;all&nbsp;feel&nbsp;zealous </code>in
the Arguments field and click OK.</li>
</ol>
<h3 class="tutorial">Running the Application</h3>
<p>Now that you have created the application and provided runtime
arguments for the application, you can test run the application in the IDE.</p>
<p><b>To run the application in the IDE:</b></p>
<ol>
<li>Right-click the MyApp project node and choose Clean and Build.</li>
<li>Choose Run&nbsp;&gt; Run&nbsp;Project (F6).
<p>In the Output window, you should see the output from the program, <code>Result =
Hello </code> (the acrostic of the phrase that was passed to the program
as an argument). </p></li>
</ol>
</div>
<!-- =============================================================================== -->
<h2>Testing and Debugging the Application<a name="test"></a></h2>
<p>Now you will create and run a test for the project using JUnit and then run the application
in the IDE's debugger to check for errors. In the JUnit test, you will test the
LibClass by passing a phrase to the <code>acrostic</code> method and using an assertion
to indicate what you think the result should be.</p>
<div class="indent">
<h3 class="tutorial">Creating JUnit Tests</h3>
<ol>
<li>Right-click the <code>LibClass.java</code> node in the Projects window and
choose Tools &gt;Create/Update Tests (Ctrl-Shift-U).
</li>
<li>In the Create Tests dialog box, click OK
to run the command with the default options.
<p class="notes"><b>Note:</b> If this is the first time you have created JUnit tests in the IDE, you
will be prompted with the Select JUnit Version dialog box. Press Enter to
select JUnit 4.x and continue to the Create Tests dialog box.</p>The IDE creates the <code>org.me.mylib</code>
package and the <code>LibClassTest.java</code> file in a separate <code>test</code> folder. You can find this
file by expanding the Test Packages node and the <code>org.me.mylib</code> subnode.</li>
<li>In <code>LibClassTest.java</code>, delete the body of
the <code>public void testAcrostic()</code> method.
</li>
<li>In place of the deleted lines, type or paste in the following:<br>
<pre class="examplecode">System.err.println(&quot;Running testAcrostic...&quot;);
String result = LibClass.acrostic(new String[]
{&quot;fnord&quot;, &quot;polly&quot;, &quot;tropism&quot;});
assertEquals(&quot;Correct value&quot;, &quot;foo&quot;, result);</pre></li>
<li> Save the file by pressing Ctrl-S. </li>
</ol>
<h3 class="tutorial">Running JUnit Tests</h3>
<ol>
<li>Select the MyLib project node and choose Run &gt; Test Project&nbsp;(MyLib)
or press Alt-F6. The<code> MyLib&nbsp;(test)</code> tab opens in the Output window.
The JUnit test cases are compiled and run. The JUnit test result shows that
the test passes. </li>
<li>You can also run a single test file rather than testing the entire project.
Select the <code>LibClass.java</code> tab in the Source Editor and choose Run
&gt; Test File. </li>
</ol>
<p>The JUnit API documentation is available from the IDE. Choose Help &gt;
Javadoc References &gt; JUnit API.</p>
<p class="notes"><b>Note:</b> If this is the first time you access Javadoc in the IDE, you need to first choose Help &gt;
Javadoc References &gt; More Javadoc. Click Cancel in the Javadoc References dialog box. Then choose Help &gt;
Javadoc References &gt; JUnit API.</p>
<p>You can learn more about JUnit by visiting
<a href="http://www.junit.org">http://www.junit.org</a></p>
<h3 class="tutorial">Debugging the Application</h3>
<p>In this section, you will use the debugger to step through the
application and watch the values of variables change as the
acrostic is assembled.</p>
<p><b>To run the application in the debugger:</b></p>
<ol>
<li> In the <code>LibClass.java</code> file, go to the <code>acrostic</code> method
and place the insertion point anywhere inside <code>b.append(args[i].charAt(i));</code>.
Then press Ctrl-F8 to set a breakpoint. </li>
<li>Select the MyApp project node and choose Debug&nbsp;&gt; Debug Project (Ctrl-F5). The IDE opens the Debugger
windows and runs the project in the debugger until the breakpoint is reached. </li>
<li>Select the Local&nbsp;Variables window in the bottom of the IDE and expand the <code>args</code> node.
The array of strings contains the phrase you entered as the command arguments.
</li>
<li>Press F7 (or choose Debug &gt; Step Into) to step through the program and watch
the <code>b</code> variable change as the acrostic is constructed.
<p>When the program reaches the end, the debugger windows close.</p>
</li>
</ol>
<p>For more information, see
<a href="junit-intro.html">Writing JUnit Tests in NetBeans IDE</a>.</p>
</div>
<a name="build"></a>
<h2>Building, Running, and Distributing the Application</h2>
<p>Once you are satisfied that your application works properly, you can
prepare the application for deployment outside of the IDE. In this
section you will build the application's JAR file and then run the JAR
file from the command line.</p>
<div class="indent">
<h3 class="tutorial">Building the Application</h3>
<p>The main build command in the IDE is the Clean and Build command.
The Clean and Build command deletes previously compiled classes and other
build artifacts and then rebuilds the entire project from scratch.</p>
<p class="notes"><b>Notes:</b> There is also a Build command,
which does not delete old build artifacts, but
this command is disabled by default. See <a href="http://www.oracle.com/pls/topic/lookup?ctx=nb8000&id=NBDAG512">About Building Java Projects</a> in <i>Developing Applications with NetBeans IDE</i></a>
for more information.</p>
<p><b>To build the application:</b></p>
<ul>
<li>Choose Run&nbsp;&gt; Clean and Build Project (Shift-F11).</li>
</ul>
<p>Output from the Ant build script appears in the Output window.
If the Output window does not appear, you can open it manually by
choosing Window &gt; Output &gt; Output.</p>
<p>When you clean and build your project, the following things occur:</p>
<ul>
<li>Output folders that have been generated by previous build
actions are deleted (&quot;cleaned&quot;). (In most cases, these are the
<code>build</code> and <code>dist</code> folders.)</li>
<li><code>build</code> and <code>dist</code> folders are added to your
project folder (hereafter referred to as the <i>PROJECT_HOME</i>
folder). You can view these folders in the Files window. </li>
<li>All of the sources are compiled into <code>.class</code> files, which are
placed into the <code><i>PROJECT_HOME</i>/build</code> folder.</li>
<li>A JAR file containing your project is created inside the
<code><i>PROJECT_HOME</i>/dist</code> folder.</li>
<li>If you have specified any libraries for the project (in addition to the JDK),
a <code>lib</code> folder is created in the <code>dist</code> folder.
The libraries are copied into <code>dist/lib</code>.
</li>
<li>The manifest file in the JAR is updated to include entries that designate
the main class and any libraries that are on the project's classpath.</li>
</ul>
<p class="notes"><b>Note:</b> You can view the contents of the manifest in the IDE's Files window.
After you have built your project, switch to the Files window and navigate to
<code>dist/MyApp.jar</code>. Expand the node for the JAR file, expand the
<code>META-INF</code> folder, and double-click <code>MANIFEST.MF</code>
to display the manifest in the Source Editor.</p>
<pre class="examplecode">Main-Class: acrostic.Main
Class-Path: lib/MyLib.jar</pre>
<p>(To find more about manifest files, you
can read
<a href="http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html" target="_blank">this chapter</a>
from the Java Tutorial.) </p>
<a name="running-outside-IDE"></a><h3>Running the Application Outside of the IDE</h3>
<p><b>To run the application outside of the IDE:</b></p>
<ol>
<li>On your system, open up a command prompt or terminal window.</li>
<li>In the command prompt, change directories to the <code>MyApp/dist</code> directory.</li>
<li>At the command line, type the following statement:
<pre class="examplecode">
java -jar MyApp.jar However we all feel zealous </pre></li>
</ol>
<p>The application then executes and returns the following output as shown
in the image below:</p>
<pre class="examplecode">Result = Hello
</pre>
<a href="../../../images_www/articles/80/java/javase-intro/command-prompt.png"
rel="lytebox" title="Command prompt showing application output - Click to Enlarge">
<img src="../../../images_www/articles/80/java/javase-intro/command-prompt-smaller.png"
alt="Command prompt showing the output from the running program." class="margin-around"></a>
<h3 class="tutorial">Distributing the Application to Other Users</h3>
<p>Now that you have verified that the application works outside of the IDE,
you are ready to distribute the application.</p>
<p><b>To distribute the application:</b></p>
<ol>
<li>On your system, create a zip file that contains
the application JAR file (<code>MyApp.jar</code>) and the accompanying
<code>lib</code> folder that contains <code>MyLib.jar</code>.</li>
<li>Send the file to the people who will use the application.
Instruct them to unpack the zip file, making sure that
the <code>MyApp.jar</code> file and the <code>lib</code> folder
are in the same folder.
</li>
<li>Instruct the users to follow the steps in the
<a href="#running-outside-IDE">Running the Application Outside of the IDE</a>
section above.</li>
</ol>
</div>
<a name="other-tasks"></a><h2>Other Common Tasks</h2>
<p>You have now completed the main part of the tutorial, but there are still
some basic tasks that have not been covered. This section includes a few of those
tasks.</p>
<div class="indent">
<a name="projects-jdk-javadoc"></a>
<h3 class="tutorial">Making the Javadoc Available in the IDE</h3>
<p>To view the JavaSE API documentation in the NetBeans IDE, use the Source &gt; Show Documentation command or choose Window &gt; IDE Tools &gt; Javadoc Documentation from the main menu to view API documentation in a separate window.</p>
<p>However, for some third-party libraries, API documentation is not available. In these cases, the Javadoc resources must be manually associated with the IDE.</p>
<p><b>To make the Javadoc API documentation available for the Show Javadoc command:</b></p>
<ol>
<li>Download the Javadoc API documentation source (see the <a href="http://wiki.netbeans.org/FaqJavaDoc#Adding_the_JDK_Javadoc_to_the_NetBeans_IDE">FaqJavaDoc</a> page for additional details).</li>
<li>Choose Tools &gt; Java Platforms.</li>
<li>Click the Javadoc tab.</li>
<li>Click the Add ZIP/Folder button and navigate to the zip file or the
folder that contains the Javadoc API documentation on your system. Select the zip file
or the folder and click the Add ZIP/Folder button.
<li>Click Close.</li>
</ol>
<h3 class="tutorial">Generating Javadoc for a Project</h3>
<p>You can generate compiled Javadoc documentation for your project
based on Javadoc comments that you have added to your classes.</p>
<p><b>To generate Javadoc documentation for a project:</b></p>
<ol>
<li>Select the MyLib project.</li>
<li>Choose Run &gt; Generate&nbsp;Javadoc for&nbsp;&quot;MyLib&quot; from
the IDE's main menu.
<br>The generated Javadoc is added to the <code>dist</code>
folder of the project. In addition, the IDE opens a web browser
that displays the Javadoc.
</li>
</ol>
</div>
<!--<br>-->
<div class="feedback-box"><a href="/about/contact_form.html?to=3&subject=Feedback:%20Developing%20General%20Java%20Applications">Send Feedback on This Tutorial</a></div>
<br style="clear:both;" >
<h2><a name="nextsteps"></a>Next Steps</h2>
<p>For more information about using NetBeans IDE to develop Java SE applications, see the following resources:
</p>
<ul>
<li><a href="javase-deploy.html">Packaging and Deploying Desktop Java Applications</a></li>
<li><a href="annotations.html">Annotation Processors Support in the NetBeans IDE</a></li>
<li><a href="debug-multithreaded.html">Debugging Multithreaded Applications</a></li>
<li><a href="../../trails/java-se.html">General Java Development Learning Trail</a></li>
<li><a href="../../trails/matisse.html">Java and JavaFX GUI Learning Trail</a></li>
<li><a href="../../index.html">NetBeans IDE Docs and Support page</a></li>
<li><a href="http://www.oracle.com/pls/topic/lookup?ctx=nb8000&id=NBDAG366">Creating Java Projects</a> in <i>Developing Applications with NetBeans IDE</i></a></li>
</ul>
</body>
</html>