<< Chapter < Page Chapter >> Page >

Writing, compiling, and running Java programs

One of the reasons that I chose Java as the main programming language for this course is that while Java is a very powerful object-oriented programminglanguage, the mechanics of writing, compiling, and running Java programs are very simple.

Confirm your Java installation

First you need to confirm that the Java development kit (jdk) version 1.7 or later is installed on the computer. (The jdk is already installed in the CIT computer labs at the NRG campus of ACC, and perhaps in thelabs on other ACC campuses as well.) If you are working at home, see Oracle's JDK 7 and JRE 7 Installation Guide .

Creating your source code

Next, you need to use any text editor to create your Java source code files as text files with an extension of .java. (I prefer the free JCreator editor because it produces color-coded text and includes some other simple IDE features as well.JCreator is normally installed in the CIT computer labs at the NRG campus of ACC.)

Compiling your source code

The name of each source code file should match the name of the Java class defined in the file.

Assume that your source code file is named MyProg.java . You can compile the program by opening a command prompt window in the foldercontaining the source code file and executing the following command at the prompt:

Command to compile the source code: javac MyProg.java

Running your Java program

Once the program is compiled, you can execute it by opening a command prompt window in the folder containing the compiled source code files (files with an extension of .class) and executing the following command at the prompt:

Command to execute the program: java MyProg

Using a batch file

If you are running under Windows, the easiest approach is to create and use a batch file to compile and run your program. (A batch file is simply a text file with an extension of .bat instead of .txt.)

Create a text file named CompileAndRun.bat containing the text shown in the note-box below.

Contents of batch file are shown below: del *.class javac MyProg.javajava MyProg pause

Place this file in the same folder as your source code files. Then double-click on the batch file to cause your program to be compiled andexecuted.

That's all there is to it.

The program named PointLine01

Before we go any further, let's take a look at a simple Java program that illustrates one of the ways that points and lines are represented in Java code. (See Figure 1 .)

The Point2D.Double class

This program illustrates one implementation of the concepts of point and line segment in Java code.

Four points (locations in space) are defined by passing the coordinates of the four points as the x and y parameters to the constructor for the Point2D.Double class. This results in four objects of the Point2D.Double class.

( Point2D.Double is a class in the standard Java library.)

The Line2D.Double class

Two line segments are defined by passing pairs of points as parameters to the constructor for the Line2D.Double class. This results in two objects of the Line2D.Double class.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask