Setting Up and Getting Started in Java Programming

   

New to Java Programming CenterProgramming Center




Java Platform Overview | Getting Started |
Learning Paths | References & Resources | Certification | Newsletters

To set up the Java platform on your computer, you need to download the developer kit, then set up whatever development environment you plan on using. You can also do this in one step by downloading the JDK that comes bundled with the NetBeans IDE.

 

Step 1: Download the JDK



Once you've installed the JDK download on your computer, set up should be complete. You may, however, want to set the classpath as an environment variable. See the installation notes for directions on how to do that for your operating system.

If you want to develop web applications, you need to use either the NetBeans IDE, Sun Java Studio Creator IDE, or Sun Java Studio Enterprise in addition to the JDK, as these environments provide a web server that's necessary for creating and testing servlets, JavaServer Pages, and database connections.

See Getting Started with an Integrated Development Environment (IDE) if you're unsure which IDE would best suit your development needs.

Step 2: Set Up a Development Environment

If you downloaded the JDK with the NetBeans IDE, start NetBeans, and begin programming.

You can also program using a simple text editor, and compile and run from the command line. Many text editors now come with the ability to run and compile Java files, but you may need to tell the program where javac.exe and java.exe reside on your computer. Use the Windows Find command, if you are unsure where these files were installed. Once, your IDE or text editor is set up, you can begin programming.

Next, copy and paste the following programs, and invoke the compiler and interpreter either at the command line or within the IDE you're using.

The following three examples use the javac and java commands and illustrate how to compile and run an application, applet, and servlet.

When compiling and running from the command line, be sure to cd into the directory where you saved your .java file.

Application

Compile the ExampleProgram.java application with the javac command and run it with the java command.

//A Very Simple Example
class ExampleProgram {

   public static void main(String[] args){

        System.out.println("I'm a Simple Program");
   }
}

Compile and then run with the following commands:

  javac ExampleProgram.java
  java ExampleProgram

Applet

Copy the SimpleApplet.java applet code, and compile with the javac command:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;

public class SimpleApplet extends Applet{

  String text = "I'm a simple applet";

  public void init() {
        text = "I'm a simple applet";
        setBackground(Color.cyan);
  }

  public void start() {
        System.out.println("starting...");
  }

  public void stop() {
        System.out.println("stopping...");
  }

  public void destroy() {
        System.out.println("preparing to unload...");
  }

   public void paint(Graphics g){
        System.out.println("Paint");
        g.setColor(Color.blue);
        g.drawRect(0, 0,
                   getSize().width -1,
                   getSize().height -1);
        g.setColor(Color.red);
        g.drawString(text, 15, 25);
   }
}

Compile:

  javac SimpleApplet.java

You can run the applet in appletviewer (a Java platform tool for running applets independently of the browser) or in the browser. Either way you need an HTML file for the applet.

Here is the HTML file:

<HTML>
<BODY>
<APPLET CODE=SimpleApplet.class WIDTH=200 HEIGHT=100>

</APPLET>

</BODY>
</HTML>

To run the applet in the browser, you load the HTML file. To run the applet in appletviewer with an HTML file named simple.html, type this in an MS-DOS window on Windows, or in a terminal window on Solaris:

  appletviewer simple.html

Servlet

As mentioned above, to compile and run a servlet, you need to use either NetBeans or Sun Java Studio Creator. Then compile with the javac command.

  import java.io.*;
  import javax.servlet.*;
  import javax.servlet.http.*;

  public class ExampServlet extends HttpServlet {

      public void doPost(HttpServletRequest request,
                        HttpServletResponse response)
                        throws ServletException, IOException
     {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
          out.println("<title>Example</title>" +
                       "<body bgcolor=FFFFFF>");

          out.println("<h2>Button Clicked</h2>");

          String DATA = request.getParameter("DATA");

          if(DATA != null){
                  out.println(DATA);
          } else {
                  out.println("No text entered.");
          }

          out.println("<P>Return to <A HREF=../simpleHTML.html>Form</A>");
          out.close();
  }
}

To compile:

  javac ExampServlet.java

Servlets can be called directly by typing their uniform resource locator (URL) into a browser's location window after you've started the server.

Servlets can also be invoked from an HTML form by specifying their URL in the definition for a Submit button, for example.

Servlets can be called by any program that can open an hypertext transfer protocol (HTTP) request.

Subscribe to the New to Java Programming Center Newsletter to learn the basics of the Java programming language and keep up-to-date on additions to the New to Java Programming Center.

Rate and Review
Tell us what you think of the content of this page.
Excellent   Good   Fair   Poor  
Comments:
Your email address (no reply is possible without an address):
Sun Privacy Policy

Note: We are not able to respond to all submitted comments.