Netbeans For Java: How To Install and Get Started With Java Programming (On Windows, Mac Os and Ubuntu)
Netbeans For Java: How To Install and Get Started With Java Programming (On Windows, Mac Os and Ubuntu)
NOTE (2018 Feb 01): NetBeans 8.2 might not work with JDK 9 at this moment. Stick to JDK 8 now, if you want to
use NetBeans.
Step 1: Download
Download "NetBeans IDE" installer from http://netbeans.org/downloads/index.html. There are many "bundles"
available. For beginners, choose the 1st entry "Java SE" (e.g., "netbeans-8.2-javase-windows.exe" 95MB).
1 of 14 25/10/18, 11:46 AM
How to Install NetBeans for Java Programming ... http://www.ntu.edu.sg/home/ehchua/programmi...
To use NetBeans for Java programming, you need to first install JDK. Read "How to install JDK on Mac".
To install NetBeans:
1. Download NetBeans from http://netbeans.org/downloads/. Set "Platform" to "Mac OS X". There are many
"bundles" available. For beginners, choose "Java SE" (e.g., "netbeans-8.2-javase-macosx.dmg"
116MB).
2. Double-click the download Disk Image (DMG) file.
3. Double-click the "NetBeans 8.x.mpkg", and follow the instructions to install NetBeans. NetBeans will be
installed under "/Applications/NetBeans".
4. Eject the Disk Image (".dmg").
To install NetBeans:
1. Download NetBeans from http://netbeans.org/downloads/. Choose platform "Linux (x86/x64)" ⇒ "Java SE".
You shall receive a sh file (e.g., "netbeans-7.x-ml-javase-linux.sh") in "~/Downloads".
2. Set the downloaded sh file to executable and run the sh file. Open a Terminal:
$ cd ~/Downloads
$ chmod a+x netbeans-7.x-ml-javase-linux.sh // Set to executable for all (a+x)
$ ./netbeans-7.x-ml-javase-linux.sh // Run
To start NetBeans, run the script "netbeans" in the NetBeans' bin directory:
$ cd netbeans-bin-directory
$ ./netbeans
2 of 14 25/10/18, 11:46 AM
How to Install NetBeans for Java Programming ... http://www.ntu.edu.sg/home/ehchua/programmi...
To run the program, right-click anywhere in the source (or from the "Run" menu) ⇒ Run File. Observe the output
on the output console.
Notes:
You should create a NEW Java project for EACH of your Java application.
Nonetheless, NetBeans allows you to keep more than one programs in a project, which is handy for writing toy
programs (such as your tutorial exercises). To run a particular program, open and right-click on the source file
⇒ Run File.
You CANNOT RUN the program if there is any syntax error (marked by a RED CROSS before the filename).
Correct all the syntax errors; and RUN the program.
[TODO] Diagram
HINTS: In some cases, NetBeans shows a ORANGE LIGHT-BULB (for HINTS) next to the ERROR RED-CROSS
(Line 5 in the above diagram). You can click on the LIGHT-BULB to get a list of HINTS to resolve this particular
error, which may or may not work!
SYNTAX WARNING: marked by a orange triangular exclaimation sign. Unlike errors, warnings may or may not
cause problems. Try to fix these warnings as well. But you can RUN your program with warnings.
The "Help" ⇒ "Online Doc and Support" (@ http://netbeans.org/kb/index.html) contains many articles and tutorial
on using NetBeans.
The NetBeans "Start Page" also provides many useful links to get you started.
3 of 14 25/10/18, 11:46 AM
How to Install NetBeans for Java Programming ... http://www.ntu.edu.sg/home/ehchua/programmi...
As illustrated in the following diagram, the highlighted line (also pointed to by a green arrow) indicates the
statement to be executed in the next step.
4 of 14 25/10/18, 11:46 AM
How to Install NetBeans for Java Programming ... http://www.ntu.edu.sg/home/ehchua/programmi...
the step, examine the value of the variables (in the "Variable" panel) and the outputs produced by your program (in
the "Output" Panel), if any. You can also place your cursor at any variable to inspect the content of the variable.
Single-stepping thru the program and watching the values of internal variables and the outputs produced is the
ultimate mean in debugging programs - because it is exactly how the computer runs your program!
"Continue" resumes the program execution, up to the next breakpoint, or till the end of the program.
"Single-step" thru a loop with a large count is time-consuming. You could set a breakpoint at the statement
immediately outside the loop (e.g., Line 11 of the above program), and issue "Continue" to complete the loop.
Alternatively, you can place the cursor on a particular statement, and issue "Run-To-Cursor" to resume execution
up to the line.
"Finish" ends the debugging session. Always terminate your current debugging session using "Finish" or
"Continue" till the end of the program.
5 of 14 25/10/18, 11:46 AM
How to Install NetBeans for Java Programming ... http://www.ntu.edu.sg/home/ehchua/programmi...
1. Maximizing Window (double-click): You can double-click on the "header" of any panel to maximize
that particular panel, and double-click again to restore it back. This is particularly useful for editing source
code in full panel.
2. Code Auto-Complete (or Intelli-Sense) (ctrl-space): Enter a partial statement (e.g., Sys) and
press control-space to activate the auto-complete, which displays all the available choices.
3. Javadoc (ctrl-space, alt-F1): Place the cursor on a method or class, and press ctrl-space to view the
javadoc; or right-click ⇒ Show Javadoc (alt-F1) to open it on a browser.
4. Code Shorthand (tab): For example, you can enter "sout" and press TAB for
"System.out.println"; "psvm" for "public static void main(String[] args) { }" or "fori" +
tab for a for-loop. To view and configure code template, choose "Tools" menu ⇒ "Options" ⇒ "Editor" ⇒
"Code Templates".
5. Formatting Source Code (alt-shift-f): Right-click on the source (or from the "Source" menu) ⇒
Choose "Format". NetBeans will layout your source codes with the proper indents and format. To configure
the formatting, choose "Tools" menu ⇒ "Options" ⇒ "Editor" ⇒ "Formatting".
You can also select the section of codes to be formatted, instead of the entire file.
6. Hints for Correcting Syntax Error: If there is a syntax error on a statement, a red mark will show
up on the left-margin on that statement. You could click on the "light bulb" to display the error message, and
also select from the available hints for correcting that syntax error.
7. Rename (Refactor) (ctrl-r): To rename a variable, place the cursor on that variable, right-click ⇒
"Refactor" ⇒ "Rename" ⇒ Enter the new name. All the appearances of that variables in the project will be
renamed.
8. Small Programs: You can keep many small toy programs (with main()) in one Java project instead of
create a new project for each small program. To run the desired program, on the "editor" panel ⇒ right-click
⇒ "Run File".
9. Source Toggle Comment: To temporarily comment-off a block of codes, choose "Source" ⇒ "Toggle
Comment".
10. Error Message Hyperlink: Click on an error message will hyperlink to the corresponding source
statement.
11. Command-Line Arguments: To provide command-line arguments to your Java program in NetBeans,
right-click on the "project" ⇒ "Set as Main Project" ⇒ "Set Configurations" ⇒ "Customize..." ⇒ "Run" ⇒ select
the "Main" class ⇒ type your command-line arguments inside the "Arguments" field ⇒ choose "Run" menu ⇒
"Run Main Project".
12. Line Numbers: To show the line numbers, right-click on the left-margin ⇒ "Show Line Numbers".
13. Changing Font Face and Size: Tools ⇒ Options ⇒ Fonts & Colors ⇒ In "Category", select "Default"
⇒ In "Font", choose the font face and size.
14. Resetting Window View: If you mess up the window view (e.g., you accidentally close a window and
cannot find it anymore), you can reset the view via "Window" menu ⇒ "Reset Windows".
15. Code Templates: For example, when you create a new Java class, NetBeans retrieves the initial
contents from the "Java Class" code template. To configure code templates, select "Tools" menu ⇒
"Templates" ⇒ Choose the desired template ⇒ "Open in Editor". To set a value of a variable used in the all
the code templates (e.g., $User), select "Tools" menu ⇒ "Templates" ⇒ "Settings".
16. Displaying Chinese Character: Need to choose a font that support chinese character display, such
as "Monospace", in Tools ⇒ Options ⇒ Fonts & Colors ⇒ Syntax ⇒ default.
17. Changing the JDK Location: The Netbeans configuration file is located at "etc\netbeans.conf".
6 of 14 25/10/18, 11:46 AM
How to Install NetBeans for Java Programming ... http://www.ntu.edu.sg/home/ehchua/programmi...
2. Enabling JDK 7 support: If JDK 7 is already installed in your system, right-click on your Project ⇒
"Properties" ⇒ "Source" node ⇒ "Source/Binary Format" ⇒ Select "JDK 7". Also check "Libraries" ⇒ Java
Platform ⇒ JDK 7.
If JDK 7 is not installed/configured, install JDK 7. Add JDK 7 support to NetBeans via "Tool" menu ⇒ "Java
Platforms" ⇒ "Add Platform...".
3. Choosing Default Charset: Right-click on your project ⇒ "Properties" ⇒ "Source" node ⇒ "Encoding"
⇒ choose your desired charset for the text-file I/O from the pull-down menu.
4. Enabling Unicode Support for File Encoding: Right-click on your project ⇒ "Properties" ⇒
"Source" node ⇒ "Encoding" ⇒ choose your Unicode encoding (e.g., UTF-8, UTF-16, UTF-16LE, UTF-16GE)
for the text-file I/O.
5. To include Javadoc/Source: Use "Library Manager" (select the "Tools" menu ⇒ "Libraries"); or "Java
Platform Manager" (select "Tools" menu ⇒ "Java Platforms")
6. Adding External JAR files & Native Libraries (".dll", ".lib", ".a", ".so"): Many
external Java packages (such as JOGL, Java3D, JAMA, etc) are available to extend the functions of JDK.
These packages typically provide a "lib" directory containing JAR files (".jar") (Java Archive - a single-file
package of Java classes) and native libraries (".dll", ".lib" for windows, ".a", ".so" for Linux and Mac).
To include an external JAR file (".jar") into a project: Expand the project node ⇒ Right-click on "Libraries"
⇒ "Add JAR/Folder..." ⇒ Select the desired JAR file or the folder containing the classes.
If the external package contains many JAR files, you could create a user library to contain all the JAR files,
and add the library to all the projects that required these JAR files. From "Tools" menu ⇒ "Libraries" ⇒ "New
Library..." ⇒ Enter a library name ⇒ Use "Add JAR/Folder..." to add JAR files into this library.
Many JAR files come with native libraries in the form of ".dll", ".lib" (for Windows) and ".a", ".so" for
Linux/Mac. The directory path of these libraries must be included in JRE's property "java.library.path".
This can be done via right-click the project ⇒ Set Configuration ⇒ Customize... ⇒ Run ⇒ In "VM options",
enter "-Djava.library.path=xxx", where xxx is path of the native libraries.
Notes: The JAR files must be included in the CLASSPATH. The native library directories must be included in
JRE's property "java.library.path", which normally but not necessarily includes all the paths from the
PATH environment variable. Read "External JAR files and Native Libraries".
Step 0: Read
1. Java GUI Application Learning Trail @ http://www.netbeans.org/kb/trails/matisse.html.
2. Swing Tutorial's "Learning Swing with the NetBeans IDE" @ http://docs.oracle.com/javase/tutorial/uiswing
/learn/index.html.
7 of 14 25/10/18, 11:46 AM
How to Install NetBeans for Java Programming ... http://www.ntu.edu.sg/home/ehchua/programmi...
1. Right-click on the project "FirstNetBeansGUI" ⇒ "New" ⇒ "JFrame Form..." (or "Others" ⇒ "Swing GUI
Forms" ⇒ "JFrame Form").
2. In "Class Name", enter "NetBeansSwingCounter" ⇒ Finish.
3. Create the GUI Components visually:
a. From the "Platte" panel ⇒ "Swing Controls" ⇒ Drag and drop a "Label", "TextField", and "Button"
into the design panel.
b. Click on the "jLabel1" ⇒ In the "Properties" panel, enter "Count" in "text" (You can also single-click on
the jLabel1 to change the text). Right-click on the jLable1 ⇒ Change Variable Name ⇒ In "New
Name", enter "lblCount".
c. Similarly, for "jTextField1" ⇒ Change the "text" to 0, and change the "Variable Name" to "tfCount"
⇒ Resize the text field if necessary.
d. For "jButton1" ⇒ Change the "text" to "Count", and change the "Variable Name" to "btnCount".
4. Write the event handler for the button by double-clicking the button and enter the following codes:
5. Create an instance variable count (just below the class declaration) as follows:
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
lblCount.setText("Counter");
tfCount.setText("0");
btnCount.setText("Count");
// Create an anonymous inner as the listener for the ActionEvent fired by btnCount
btnCount.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCountActionPerformed(evt);
8 of 14 25/10/18, 11:46 AM
How to Install NetBeans for Java Programming ... http://www.ntu.edu.sg/home/ehchua/programmi...
}
});
pack();
}
// private variables
private javax.swing.JButton btnCount;
private javax.swing.JLabel lblCount;
private javax.swing.JTextField tfCount;
}
9 of 14 25/10/18, 11:46 AM
How to Install NetBeans for Java Programming ... http://www.ntu.edu.sg/home/ehchua/programmi...
problem here. If a "connection refused: connect" error occurs, enter the password again.]
2. Once the MySQL server is started and connected, you can see the list of databases by expanding the
MySQL server node. You can create a new database by right-clicking on it and choose "Create Database...".
Read:
"Introduction to Developing Web Applications" @ http://netbeans.org/kb/docs/web/quickstart-webapps.html.
More articles in "Java EE & Java Web Learning Trail" @ http://netbeans.org/kb/trails/java-ee.html.
Tomcat Server
To configure Tomcat Server, select "Tools" menu ⇒ "Servers" ⇒ click "Add Servers":
1. Choose Server: Select the desired Tomcat version ⇒ Next.
2. Installation and Login Details: In "Server Location", fill in the Tomcat installation directory
($CATALINA_HOME) ⇒ Enter the username/password of a tomcat user with "manager" role. You could
either check the "create user if it does not exist" or define the tomcat user in "$CATALINA_HOME\conf
\tomcat-users.xml" as follows:
<tomcat-users>
10 of 14 25/10/18, 11:46 AM
How to Install NetBeans for Java Programming ... http://www.ntu.edu.sg/home/ehchua/programmi...
<role rolename="manager"/>
<user username="tomcatmanager" password="xxxx" roles="manager,manager-script,admin" />
</tomcat-users>
package hello;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// Set the response message's MIME type (in Content-Type response header)
response.setContentType("text/html;charset=UTF-8");
// Get an output Writer to write the response message over the network
PrintWriter out = response.getWriter();
// Write the response message (in an HTML page) to display "Hello, world!"
try {
out.println("<!DOCTYPE html>");
11 of 14 25/10/18, 11:46 AM
How to Install NetBeans for Java Programming ... http://www.ntu.edu.sg/home/ehchua/programmi...
out.println("<html>");
out.println("<head><title>Hello Servlet</title></head>");
out.println("<body><h1>Hello, World (from Java Servlet)!</h1></body>");
out.println("</html>");
} finally {
out.close(); // Always close the output writer
}
}
}
5. To execute the servlet: Right-click on the project ⇒ run ⇒ Change the URL to http://localhost:8080
/HelloServletJSP/sayhello.
To distribute the project as a war-file, right-click project ⇒ "Clean and Build". The war file is created in the "dist"
directory. You can deploy the web application by dropping the war-file into Tomcat's "webapps" directory. Tomcat
will automatically unzip the war-file and deploy the application upon startup.
12 of 14 25/10/18, 11:46 AM
How to Install NetBeans for Java Programming ... http://www.ntu.edu.sg/home/ehchua/programmi...
3. "Name and Location" ⇒ In "File Name", enter "HelloJSF20" ⇒ In "Options", check "Facelets" ⇒ Finish.
4. In "HelloJSF20.xhtml", enter the following codes:
5. To execute the JSF page, right-click on the project ⇒ Run ⇒ Change the URL to
http://localhost:8080/HelloJSF20/HelloJSF20.xhtml.
13 of 14 25/10/18, 11:46 AM
How to Install NetBeans for Java Programming ... http://www.ntu.edu.sg/home/ehchua/programmi...
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Hello JSF 1.2</title>
</head>
<body>
<h1><h:outputText value="Hello World!"/></h1>
</body>
</html>
</f:view>
5. To execute the JSF page, right-click on the project ⇒ Run ⇒ Change the URL to
http://localhost:8080/HelloJSF12/faces/HelloJSF12.jsp.
Feedback, comments, corrections, and errata can be sent to Chua Hock-Chuan ([email protected]) | HOME
14 of 14 25/10/18, 11:46 AM