3680 tutorial console

11
BCIS 3680 Enterprise Programming  Console Tutorial  ‐ 1 BCIS 368 0 Enterpri s e Programming Ins tr uc tor : Dr . Andy Wu T ut or ial: Runni ng J ava in Command Console Although we will use NetBeans to compile and run  Java applications most of the time, the ability to use command console to compile  and run  Java applications  is essential  to learning  Java. Here I will show you step  by step how to do  just that. 1. S t art ing a C ommand C onsole First, let’s start a command console. There are two ways to do that. You may click Start | Run: Then, in the “Run” dialog  box, type in cm d. Click “OK”. Alternatively,  you may click Start | Accessories | Command Prompt to start a command console. 

Upload: daniel-roa-petrasic

Post on 14-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 3680 Tutorial Console

7/27/2019 3680 Tutorial Console

http://slidepdf.com/reader/full/3680-tutorial-console 1/11

BCIS 3680 Enterprise Programming  Console Tutorial ‐ 1

BCIS 3680 Enterprise Programming

Instructor: Dr. Andy Wu

Tutorial: Running Java in Command Console

Although we will use NetBeans to compile and run  Java applications most of the time, the ability to 

use command console to compile and run  Java applications is essential to learning  Java.  Here I will 

show you step  by step how to do  just that. 

1. Starting a Command Console

First, let’s start a command console.  There are two ways to do that.  You may click Start | Run: 

Then, in the “Run” dialog  box, type in cmd.  Click “OK”. 

Alternatively, you may click Start | Accessories | Command Prompt to start a command console. 

Page 2: 3680 Tutorial Console

7/27/2019 3680 Tutorial Console

http://slidepdf.com/reader/full/3680-tutorial-console 2/11

BCIS 3680 Enterprise Programming  Console Tutorial ‐ 2

 

Either way, a command console will appear.  Yours may have white characters on  black  background. 

To make my screenshots easier to read and to save toner, I have changed its color scheme. 

2. Basic Commands

There are

 a few

 terms

 you

 want

 to

 know

 to

 get

 started

 with

 the

 console.

 First,

 note

 the

 prompt

 ends

 

with a greater‐than sign (>).  The part  before it indicates your working directory at this moment.  By 

default, and as shown above, the working directory is the current user’s home directory.  In this 

example, it is C: \ Document s and Set t i ngs\ Andy. 

In Windows Explorer, each directory is represented  by a folder.  So the previous directory would 

look something like this: 

Page 3: 3680 Tutorial Console

7/27/2019 3680 Tutorial Console

http://slidepdf.com/reader/full/3680-tutorial-console 3/11

BCIS 3680 Enterprise Programming  Console Tutorial ‐ 3

 

If the

 current

 working

 directory

 of

 the

 command

 console

 does

 not

 have

 the

 files

 you

 want

 to

 work

 

with, you may use a few commands to move around the file system. 

If the directory (namely folder) you want to work with is not on the same drive as the current 

working directory, you may move to that drive first.  In our example, my  Java source code file is 

stored in F: \ j avademo\ hel l o\ sr c.  So I type in F:  and then press Enter. 

Note that the working directory is now the root of the F:  drive.  Next, I use the cd (change directory) 

command to move to the  j avademo directory. 

Notice that once I issue the cd command, the working directory now changes to F: \ j avademo: 

Page 4: 3680 Tutorial Console

7/27/2019 3680 Tutorial Console

http://slidepdf.com/reader/full/3680-tutorial-console 4/11

BCIS 3680 Enterprise Programming  Console Tutorial ‐ 4

 

Another useful command is dir (directory), which lists the contents within a directory. 

In the output of dir command, subdirectories (subfolders) are indicated  by <DI R>.  However, there 

are two “subdirectories” (.  and . . ) that exist in every single directory on the file system.  They have 

special meanings and don’t appear in Windows Explorer as folders.  So you can ignore them.  Thus, 

the above output shows that there is a subdirectory called hel l o under F: \ j avademo , which is true. 

However, my  Java source code file is in F: \ j avademo\ hel l o\ sr c.  So I keep on moving until I am 

in the

 sr c

 directory.

 There,

 I find

 my

 Hel l o. j ava

 file.

 

Page 5: 3680 Tutorial Console

7/27/2019 3680 Tutorial Console

http://slidepdf.com/reader/full/3680-tutorial-console 5/11

BCIS 3680 Enterprise Programming  Console Tutorial ‐ 5

In fact, I don’t have to issue the cd command repeatedly to get to the directory.  If I know exactly 

where I want to go, I can get there with one single cd command after I am in the right drive: 

3. Running Java Commands

An idiosyncrasy of command prompt is that, unless you do something (and we will cover that in the 

next section), only a small number of most essential commands are recognized  by the console  by 

default.  Most of these commands are related to system maintenance.  If you issue a command that is 

not one of those, the console will look in the current working directory to find it.  Since a command 

in fact is an executable file (most often a . exe file), the console looks for the executable file that has 

the same name as your command.  If it finds a match, the executable file is run.  Otherwise, the 

console simply tells you that the command is not recognizable and stops. 

Unfortunately, the

  Java

 compiler

 file

 ( j avac. exe)

 is

 not

 recognized

  by

 the

 console

 automatically.

 If

 

I am inside my source code directory and issue the javac command, it will not  be recognized: 

Page 6: 3680 Tutorial Console

7/27/2019 3680 Tutorial Console

http://slidepdf.com/reader/full/3680-tutorial-console 6/11

BCIS 3680 Enterprise Programming  Console Tutorial ‐ 6

 

That’s  because the console searched the current working directory F: \ j avademo\ hel l o\ sr c and 

didn’t find an executable file with the name  j avac. exe. 

The console will recognize the javac command if I change my working directory to where 

 j avac. exe is

 (the

  Java

  binary

 folder).

 On

 my

 machine,

 the

 location

 is

 D: \ J ava\ j dk1. 6. 0\ bi n:

 

Another problem surfaces, though, when I issue the javac command from this directory: 

This time the console does not complain about not finding the javac command.  However, since I 

am now in D: \ J ava\ j dk1. 6. 0\ bi n , not F: \ j avademo\ hel l o\ sr c , where I have my source code files, I encounter a “f i l e not f ound” error.  In other words, when I run the  Java compiler in the 

console with the syntax javac source_file_name , the console looks in the working directory for 

 both  j avac. exe and the source file.  Usually, these two files are stored in different directories and it 

is  bad practice to do otherwise.  Thus, it looks like I have a dilemma here.  Regardless of which of 

the two directories I am in, either the console can’t find the  j avac. exe file or my source file. 

Page 7: 3680 Tutorial Console

7/27/2019 3680 Tutorial Console

http://slidepdf.com/reader/full/3680-tutorial-console 7/11

BCIS 3680 Enterprise Programming  Console Tutorial ‐ 7

4. Changing the System Path Variable

There is an easy and a hard way to solve the dilemma.  The hard way is to issue the command from 

the  Java  binary folder and also, in the second part of the command, tell the console where to locate 

the source code file  by preceding the file name with the complete path to that file. 

As shown above, the command went through and now there is a Hel l o. cl ass  file in the directory. 

This would not  be a problem if I were  just compiling one or two source code files.  But obviously, if I 

have to do it for many times, that will  be a lot of typing to do. 

There is an easier solution.  All I need to do is to add the path to the folder where  j avac. exe is 

installed on

 my

 system

 to

 Windows’

 PATH

 variable.

 To

 do

 that,

 first,

 right

 click

 the

 “My

 Computer”

 

icon on Windows desktop and select “Properties”. 

Alternatively, I can click Start | Settings | Control Panel | Performance and Maintenance.  Then click 

the “System” icon. 

Page 8: 3680 Tutorial Console

7/27/2019 3680 Tutorial Console

http://slidepdf.com/reader/full/3680-tutorial-console 8/11

BCIS 3680 Enterprise Programming  Console Tutorial ‐ 8

 

If you work with Windows Vista, the desktop may  be darkened and you may  be prompted for 

confirmation.  Agree to it to move ahead. 

Either way, the System Properties dialog  box will appear.  Click the “Advanced” tab: 

Page 9: 3680 Tutorial Console

7/27/2019 3680 Tutorial Console

http://slidepdf.com/reader/full/3680-tutorial-console 9/11

BCIS 3680 Enterprise Programming  Console Tutorial ‐ 9

Click the “Environment Variables”  button.  The “Environmental Variables” dialog  box will pop up: 

In the “System variables” area (in the  bottom), scroll down to find the “Path” variable.  Click it and 

then click the “Edit”  button.  The “Edit System Variable” dialog  box will appear. 

Initially, the entire content of the “Variable value” textbox is highlighted.  To prevent altering any 

part of it inadvertently, I would press Ctrl + C to make a copy of it and paste it into Notepad as a 

 backup copy.  Then I move to the very end of the value and get my cursor there: 

Next, I add a semicolon (; ) to it.  A semicolon separates one part of the path from the next.  Then I 

add the location of my  Java  binary folder (namely D: \ J ava\ j dk1. 6. 0\ bi n , where  j avac. exe is) 

to it.  In essence, I am appending this location to the system PATH variable. 

Page 10: 3680 Tutorial Console

7/27/2019 3680 Tutorial Console

http://slidepdf.com/reader/full/3680-tutorial-console 10/11

BCIS 3680 Enterprise Programming  Console Tutorial ‐ 10

 

Click “OK” several times until you get all the way out to the desktop.  You can check whether this 

has  been done successfully  by issuing the path command in the console: 

As it’s shown above, now the location of the  Java  binary folder is part of the PATH variable (if it isn’t, 

reboot the computer).  The significance of this is that, from now on, the console should  be able to 

recognize the javac command, regardless of which working directory I issue the command from. 

5. Running Java Commands Again

Now, I should

  be

 able

 to

 move

 into

 my

 source

 code

 file

 directory

 and

 issue

 the

 javac

 command

 

from there.  The console will find my . j ava file there and since javac is now recognizable, the 

command should go through.  To see the impact of changing the PATH variable clearly, let’s delete 

the Hel l o. cl ass file created  by the solution in the previous section.  Then, take a look at the 

content of the directory again. 

Page 11: 3680 Tutorial Console

7/27/2019 3680 Tutorial Console

http://slidepdf.com/reader/full/3680-tutorial-console 11/11

BCIS 3680 Enterprise Programming  Console Tutorial ‐ 11

As shown above, only Hel l o. j ava is in the source file directory.  From here I issue the javac 

command: 

The console doesn’t complain any more.  The compiler produces the Hel l o. cl ass file gain.  Let’s 

run it  by issuing the java command. 

The . cl ass  file works.  Since  j ava. exe resides in the same folder as  j avac. exe (see the first 

screenshot on page 6), the command console also recognizes the java command from anywhere.