GETTING STARTED. The longest journey begins with a single step. In this chapter, you will learn about: Compiling and Running a Java Program Page 2

Similar documents

Sun ONE Integrated Development Environment

CT 229. CT229 Lecture Notes. Labs. Tutorials. Lecture Notes. Programming II CT229. Objectives for CT229. IT Department NUI Galway

TTh 9.25 AM AM Strain 322

Outline. Introduction to Java. What Is Java? History. Java 2 Platform. Java 2 Platform Standard Edition. Introduction Java 2 Platform

Supplement H.1: JBuilder X Tutorial. For Introduction to Java Programming, 5E By Y. Daniel Liang

Supplement II.B(1): JBuilder X Tutorial. For Introduction to Java Programming By Y. Daniel Liang

Road Map. Introduction to Java Applets Review applets that ship with JDK Make our own simple applets

Java Programming Language Mr.Rungrote Phonkam

Chapter 3 - Introduction to Java Applets

Introduction to Java

II. Compiling and launching from Command-Line, IDE A simple JAVA program

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Lecture 1: Introduction to Java

Before you start with this tutorial, you need to know basic Java programming.

Introduction to Java. Lecture 1 COP 3252 Summer May 16, 2017

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

Slide 1 Java Programming 1 Lecture 2D Java Mechanics Duration: 00:01:06 Advance mode: Auto

Special Edition Using Java Installing the JDK and Getting Started

Running Java Programs

Java Language. Programs. Computer programs, known as software, are instructions to the computer. You tell a computer what to do through programs.

CS335 Graphics and Multimedia

Networks Programming Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000)

Chapter 1 Introduction to Java

For live Java EE training, please see training courses at

Introduction. Overview of the Course on Java. Overview of Part 1 of the Course

The Computer System. Hardware = Physical Computer. Software = Computer Programs. People = End Users & Programmers. people

1. Introduction. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

page 1 Murach s Beginning Java 2 Table of Contents Murach s Beginning Java 2 (Includes Version 1.3 & 1.4) - 2 Introduction - 3

Understanding How Java Programs Work

This tutorial will teach you how to use Java Servlets to develop your web based applications in simple and easy steps.

Chapter 1 Introduction to Computers, Programs, and Java. What is a Computer? A Bit of History

MEAP Edition Manning Early Access Program Get Programming with Java Version 1

Notes of the course - Advanced Programming. Barbara Russo

Introduction to Java. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Certified Core Java Developer VS-1036

Chapter 1 Introduction to Computers, Programs, and Java

Introduction to Java. Nihar Ranjan Roy.

Life Without NetBeans

Supplement B. Creating, Compiling and Running Java Programs from the Command Window

Agenda. Announcements. Extreme Java G Session 2 - Main Theme Java Tools and Software Engineering Techniques

Chapter 1 Introduction to Computers, Programs, and Java

Setup and Getting Startedt Customized Java EE Training:

1B1a Programming I Getting Started

CSE 421 Course Overview and Introduction to Java

CSC System Development with Java Introduction to Java Applets Budditha Hettige

COMP1007 Principles of Programming

Eclipse Tutorial. For Introduction to Java Programming By Y. Daniel Liang

Introduction to Java Programming

What you need to know about Java and JetTrac Licensing

Chapter 1 Introduction to Computers, Programs, and Java

Learning objectives. The Java Environment. Java timeline (cont d) Java timeline. Understand the basic features of Java

Eclipse Environment Setup

Chapter 13. Applets and HTML. HTML Applets. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 1

JDB - QUICK GUIDE JDB - INTRODUCTION

Fundamentals of Programming. By Budditha Hettige

Chapter Two Bonus Lesson: JavaDoc

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

Chapter 2 Author Notes

Getting Started with Eclipse/Java

Objectives. Chapter 1 Introduction to Computers, Programs, and Java. What is a Computer?

IT151: Introduction to Programming (java)

CS260 Intro to Java & Android 02.Java Technology

Setting up your Computer

Fundamental C# Programming

Computer Science 62 Lab 8

CHAPTER 1. Introduction to JAVA Programming

Chapter 1: Introduction to Computers and Java

8/23/2014. Chapter Topics. Introduction. Java History. Why Program? Java Applications and Applets. Chapter 1: Introduction to Computers and Java

SECTION 1: JAVA PROGRAMMING LAB

You should now start on Chapter 4. Chapter 4 introduces the following concepts

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 1

Getting Started with Java. Atul Prakash

Lesson 01 Introduction

Core JAVA Training Syllabus FEE: RS. 8000/-

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

2 Introduction to Java. Introduction to Programming 1 1

Java applets & the RealJ IDE

JVM interprets the Java bytecode, controls how it interacts with the operating system and manages memory.

Lecture 4: Introduction to Java and your Development Environment

CPS109 Lab 1. i. To become familiar with the Ryerson Computer Science laboratory environment.

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Chapter 1 Introduction to Computers, Programs, and Java

Sentences Installation Guide. Sentences Version 4.0

Class 1: Homework. Intro to Computer Science CSCI-UA.0101 New York University Courant Institute of Mathematical Sciences Fall 2017

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Week - 01 Lecture - 04 Downloading and installing Python

The Command Shell. Fundamentals of Computer Science

Instructions. First, download the file

TEMPO INSTALLATION I O A. Platform Independent Notes 1. Installing Tempo 3. Installing Tools for the Plugins 5. v0.2.

Configuring IBM's Network Station 1000 for Java Applications by Paul Conte A22 32 MB Token-Ring

CS110/CS119 Introduction to Computing (Java) Bob Wilson S-3-176

1. What is Jav a? simple

Programming Principles 1 (CSC131) & 2 (CSC132) Software usage guide

Introduction to Computers, Programs, and Java. CSE 114, Computer Science 1 Stony Brook University

Why Operating Systems? Topic 3. Operating Systems. Why Operating Systems? Why Operating Systems?

CSC116: Introduction to Computing - Java

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Transcription:

ch01 11/17/99 9:16 AM Page 1 CHAPTER 1 GETTING STARTED The longest journey begins with a single step. CHAPTER OBJECTIVES In this chapter, you will learn about: Compiling and Running a Java Program Page 2 To attain proficiency with a computer programming language, you must get your hands dirty. In order to get your hands dirty, you must have appropriate tools at your disposal. Within this chapter, we will introduce the tools needed to create and run Java programs, and show you how to obtain and configure these tools on your system. With these tools installed on your computer, this interactive workbook offers you the opportunity to both read about Java programming and become an active participant in your Java education as well. There is no substitute for experience, so let s hit the ground running! 1

ch01 11/17/99 9:16 AM Page 2 2 Lab : Compiling and Running a Java Program COMPILING AND RUNNING A JAVA PROGRAM OBJECTIVES After this lab, you will be able to: Install and Configure the Java Development Kit Compile and Run a Java Program Compile and Run a Java Applet Before the introduction of the Java programming language, most software development was done in a platform-specific manner. Programs written in languages such as Fortran, C, and C++ were run through compilers to generate platform-specific executables. PCs, Macintoshes, and UNIX workstations all required their own version of an executable and often the programs would have to be specifically tailored for each platform. Java programs must be run through a compiler as well. However, the Java compiler does not generate platform-specific code it generates platformindependent bytecodes. These bytecodes can then be executed by a Java Virtual Machine (JVM). Strictly speaking, Java is not Write Once, Run Anywhere as claimed by Sun; realistically, it is Write Once, Run on Any System With a JVM. Because JVMs are available for most interesting platforms (and even a few not-so-interesting ones), the language has fulfilled its promise of cross-platform compatibility.

ch01 11/17/99 9:16 AM Page 3 Lab : Compiling and Running a Java Program 3 To foster development with the Java programming language, Sun Microsystems provides a free software development kit (SDK), which includes a compiler, virtual machine, debugger, and a host of other useful development tools. This is called the Java Development Kit, or JDK, and the latest Windows and Solaris versions are always available from the Sun Web site at: http://java.sun.com/products/jdk/1.2/index.html If you are developing on a platform other than Windows or Solaris (good for you!), you can find information regarding JDKs for other platforms on the Web at: http://java.sun.com/cgi-bin/java-ports.cgi The JDK is free of charge and free of frills. There is no graphical user interface provided to these tools, so you can only use them from a commandline interface such as a DOS window or UNIX shell. The benefit of the JDK is that it is a reference implementation if you can successfully compile and run programs using Sun s JDK, you can be sure your programs will run under any compatible JVM. If you have done previous software development with an integrated development environment (IDE), you may find this adjustment difficult. Many commercial Java IDEs are available, and there are even a few free IDEs (like Sun s Java Workshop product), but they will not be addressed within this text use them at your own peril. Do not confuse the JDK with the Java Runtime Environment (JRE). The JRE contains only the virtual machine and supporting code necessary to run (not develop) Java programs. The computer industry has an affinity for ambiguous acronyms, and if you install the JRE instead of the JDK you will find yourself DOA without an SDK. EXERCISES.1 INSTALL AND CONFIGURE THE JAVA DEVELOPMENT KIT The latest version of Sun s Java2 JDK can be found on the Web at: http://java.sun.com/products/jdk/1.2/index.html

ch01 11/17/99 9:16 AM Page 4 4 Lab : Compiling and Running a Java Program Download the JDK version appropriate for your platform and install it. The Windows version will attempt to install itself into C:\JDK1.2.x, where x indicates the dot-version of the current JDK. The Solaris version will attempt to install itself into your current working directory. The JDK documentation, known as the Java API documentation or Javadocs, can be downloaded from the same Web site. Download the HTML-formatted documentation and install it. The Javadocs are an excellent example of documentation done right, and we will be referring to this documentation throughout the text. You didn t neglect to install the documentation, did you? Consider it a requirement for learning the language. As you will shortly discover, Javadocs will become your second-best friend during your forray into the depths of Java. Your best friend, of course, is this interactive workbook! Once the JDK is installed on your system, you must modify your PATH environment variable to include the location of your development tools. For those unfamiliar with the PATH variable, it tells your operating system where to look within your filesystem for programs. ON WINDOWS 95, 98 OR NT... If you are running Windows 95 or 98, you can modify your PATH within the autoexec.bat file in a manner similar to the following. Be sure to change the JDK1.2.x in the following line to match the location where you installed the JDK on your system. PATH=%PATH%;C:\JDK1.2.x\Bin This change will take effect next time you reboot Windows like that is news to any Windows user. Users of WindowsNT can make this modification within the Console application available within the Control Panel. To double-check that your PATH modification has been successful, you can input the following from an MS-DOS Prompt window: C:\WINDOWS>path

ch01 11/17/99 9:16 AM Page 5 Lab : Compiling and Running a Java Program 5 If your PATH was modified successfully, you should see the location of the Bin subdirectory of your JDK installation, similar to the following: PATH=C:\WINDOWS;C:\WINDOWS\COMMAND;C:\JDK12~1.2\BIN ON SOLARIS, UNIX OR LINUX OS... If your default shell is a Bourne-shell derivative, you can modify your PATH similar to the following in your.profile file. Modify the /usr/local/jdk1.2.x/bin below to reflect the bin subdirectory where you installed the JDK. PATH=$PATH:/usr/local/jdk1.2.x/bin export PATH To double-check your modification, open a new shell and input the following: echo $PATH If the modification was successful, you should see the location of the bin subdirectory of your JDK installation, similar to the following: /bin:/usr/bin:/usr/local/bin:/usr/local/jdk1.2.x/bin IF YOU HAVE ALREADY INSTALLED A PREVIOUS VERSION OF THE JDK ON EITHER PLATFORM... Similar to the PATH environment variable used by your operating system, Java uses another environment named CLASSPATH. The CLASSPATH environment variable informs the JVM where it should look for Java program files (classes). If you do not have this defined on your system, don t worry about it. If you do have this defined on your system, be sure to remove an entry for classes.zip if it is present this entry is needed for older Java releases, but will only confuse matters with Java2. Use the same steps as outlined previously for your PATH environment variable. CONGRATULATIONS! You have just installed and configured the JDK. Answer the following questions: From an MS-DOS prompt or UNIX shell, type the following: java version

ch01 11/17/99 9:16 AM Page 6 6 Lab : Compiling and Running a Java Program a) What was displayed?.2 COMPILE AND RUN A JAVA PROGRAM Java source code can be written with whatever editor you are most comfortable. On Windows systems this might be notepad, edit, or brief. On UNIX systems this is probably vi, emacs, or pico. Java source code files adhere to a strict naming convention. This will be discussed in following chapters, but it is important to know that filenames are case-sensitive (MyApp is not the same as myapp) and all end with a.java extension. Create your first Java program by firing-up your favorite editor and entering the following code exactly as it is written. Save this file as Welcome.java: import javax.swing.*; public class Welcome { public static void main(string[] argv) { JOptionPane.showMessageDialog( null, "Welcome to the wonderful world of Java!", "Welcome, new Programmer", JOptionPane.PLAIN_MESSAGE); System.exit(0); } } Once the file is saved, start up an MS-DOS Prompt or UNIX shell. Navigate to the directory that contains Welcome.java and run the following command: javac Welcome.java

ch01 11/17/99 9:16 AM Page 7 Lab : Compiling and Running a Java Program 7 a) What were your results from running this command? Without changing directories, run the following command: java Welcome b) What were your results from running this command?.3 COMPILE AND RUN A JAVA APPLET Fire up your favorite editor and create the following file, named Welcome- Applet.java: import java.awt.*; import java.applet.*; public class WelcomeApplet extends Applet { Label textlabel; public void init() { textlabel = new Label( "Welcome to the wonderful world of Java!"); textlabel.setalignment(label.center); this.add(textlabel); } } Start an MS-DOS Prompt or UNIX shell and navigate to the directory that contains WelcomeApplet.java. Run the following command: javac WelcomeApplet.java a) What were your results from running this command?

ch01 11/17/99 9:16 AM Page 8 8 Lab : Compiling and Running a Java Program Once again, start an editor and create the following file named Welcome- Applet.html: <HTML> <TITLE>Welcome</TITLE> <APPLET CODE="WelcomeApplet.class" WIDTH=400 HEIGHT=200> If you can read this, your browser does not support Java! </APPLET> </HTML> From within the same directory, run the following command: appletviewer WelcomeApplet.html b) What were your results from running this command? EXERCISE ANSWERS.1 ANSWERS From an MS-DOS prompt or UNIX shell, type the following: java version a) What was displayed? Answer: If everything worked properly, you should have seen output similar to the following: java version "1.2.2" Classic VM (build JDK-1.2.2-W, native threads, symcjit) The java program is responsible for starting a JVM. The argument version indicates that you are only interested in the version of the JVM, and do not care to run a Java program.

ch01 11/17/99 9:16 AM Page 9 Lab : Compiling and Running a Java Program 9 Depending upon the current version of the JDK, you may be running a slightly newer version than shown in the output above. So long as your version string is of the format 1.2.x, where x represents the dot-release of the JDK, you are doing well. If by some chance an older version was displayed, such as 1.0.x or.x, you may have an old version of Java on your system. If you do not wish to remove the old version of Java, you should modify your PATH environment variable so that the 1.2 JDK entry is placed before any other JDK entries. If you received an error message similar to or Bad command or file name java: command not found your PATH environment variable may not be set correctly. Review the Lab and double-check that the JDK bin directory is in your PATH..2 ANSWERS Once the file is saved, start up an MS-DOS Prompt or UNIX shell. Navigate to the directory that contains Welcome.java and run the following command: javac Welcome.java a) What were your results from running this command? Answer: If all went well, you should have received no output to your console. The javac program is the Java compiler. It translates source code into Java bytecodes. If the compiler was successful, it translated your Welcome.java source code into Java bytecodes and stored them in a new class file named Welcome.class. If you received any error messages from the javac command, check the source code to be sure it was input exactly as shown in the Lab. Java is case-sensitive, so be sure you did not mix character cases. Without changing directories, run the following command: java Welcome b) What were your results from running this command?

ch01 11/17/99 9:16 AM Page 10 10 Lab : Compiling and Running a Java Program Answer: You should have been greeted by a popup window that contained the text Welcome to the wonderful world of Java!. The java command started a JVM that ran the Welcome program. This simple program merely created a popup window that contained a friendly welcome message similar to the following: If you received an error from the java command similar to the following: Exception in thread "main" java.lang.noclass DefFoundError: Welcome the JVM was unable to locate the Welcome.class file. Try running the command again, from the same directory which contains the Welcome.class file..3 ANSWERS Start an MS-DOS Prompt or UNIX shell and navigate to the directory which contains WelcomeApplet.java. Run the following command: javac WelcomeApplet.java a) What were your results from running this command? Answer: Similar to the previous lab section, you should have received no output if all went well. If the WelcomeApplet.java source code compiled properly, you received no messages from the Java compiler. If you did receive error messages from the compiler, double-check the source code to be sure it appears exactly as shown within the lab. You don t think we made a typo, do you?! From within the same directory, run the following command: appletviewer WelcomeApplet.html

ch01 11/17/99 9:16 AM Page 11 Lab : Compiling and Running a Java Program 11 b) What were your results from running this command? Answer: You should have been greeted by an applet that displayed the text message Welcome to the wonderful world of Java! The appletviewer program is included with the JDK as an alternative to running applets within a web browser, and provides an interface that makes testing of applets much less cumbersome than within a web browser. More importantly, at the time of this writing, neither Netscape Navigator nor Microsoft Internet Explorer include standard support for Java2. For this reason, we will assume throughout the workbook that appletviewer will be used to run all the example applets. If you did not see an applet displayed, check your WelcomeApplet.html file to be sure it does not deviate from that which is shown within the Lab. Also, be sure that both the WelcomeApplet.html and Welcome- Applet.class files exist within the same directory from which you ran appletviewer. What do you mean Java2 is not supported by my browser?! Relax in an effort to provide an upgrade to the JVM of your browser, Sun has created a free browser plug-in which allows you to run the JVM of your choice within the web browser. Check your browser documentation to see if it supports Java2. If it does not, check Sun s Java Plug-In web site for details: http://java.sun.com/products/plugin SELF-REVIEW QUESTIONS In order to test your progress, you should be able to answer the following questions. 1) The Java compiler transforms source code into what platform-independent format? a) opcodes b) bytecodes c) virtual machines d) applets

ch01 11/17/99 9:16 AM Page 12 12 Lab : Compiling and Running a Java Program 2) What command runs the Java compiler? a) java b) appletviewer c) jdb d) javac 3) Which of the following commands is used to run a Java application? a) java b) appletviewer c) jdb d) javac 4) Which of the following commands is used to run a Java applet? a) java b) appletviewer c) jdb d) javac 5) The freely-available collection of tools which can be used to create Java programs is known as what? a) JRE b) JDK c) JVM d) JNI Quiz answers appear in Appendix A, Section.

ch01 11/17/99 9:16 AM Page 13 CHAPTER 1 Chapter 1: Getting Started 13 TEST YOUR THINKING The purpose of this chapter was just to make sure you could run a Java program. There s not much point in learning a computer language if you can t use it. 1) Since you are going to be programming frequently in this book, we suggest that you setup a programming environment for yourself. A programming environment is a configuration of your computer, or computer account, which allows you to write, compile, and run computer programs. We recommend that you setup your login environment to allow you to write, compile, and run Java programs. Once you have successfully completed the labs of this chapter, you should lock in that configuration so the next time you login to use your computer, you don t have to worry about the configuration again. This will save you a great deal of time in the future.

ch01 11/17/99 9:16 AM Page 14