Compiling and Running from the Console

Preparing to Compile

Create a subfolder called bin and place it in your path. Put command (.bat) files and other frequently-used executables in that directory. See system configuration for details.

You must make the appropriate changes to your path. For Microsoft Visual C, use the command file vcvars32.bat from the command window. For Java, insert the SDK bin folder into your path. To run Python directly from the console you need to have the Python installation directory in your path.

I copied the vsvars32.bat into my bin directory and renamed the file vc.bat. Then I just type the command vc to define the Visual C environment.

You could also create a command file to add the Java bin directory to your path, for example:

set PATH=%PATH%;C:\Program Files\Java\jdk1.6.0\bin

Microsoft Visual C

To compile a C program:


cl /W4 AddArray1.c

To compile a C++ program:


cl /EHsc /W4 AddArray.cpp

Choosing an Executable Filename

C:\prog\intro>cl /EHsc AddArray.cpp /FemyArray.exe
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

AddArray.cpp
Microsoft (R) Incremental Linker Version 8.00.50727.42
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:myArray.exe
AddArray.obj

C:\prog\intro>myArray
C++ version: sum is 45

C/C++ Files

Compiling a C/C++ program produces an object module

Example: AddArray.obj

Object modules are linked to produce the executable file

Example: AddArray.exe

You run the executable file (console programs) by typing its name in the command window

Example: AddArray.

Java

To compile a Java program:


javac  AddArray.java

To run a Java program:


java AddArray

Java Naming Conventions

The filename must match the public class name used for the Java program. Java program names are case sensitive!

If you have a filename AddArray.java, you must declare a class called AddArray.

When you compile, you must use the java extension:


    javac AddArray.java

When you run a java application, you do not use the class extension:


    java AddArray

Java Files

To compile: javac AddArray.java

To run: java AddArray

The java compiler (javac) takes java source files (.java) and generates java class files (.class). A class file is a stream of Java tokens (bytecode) that is interpreted by the Java virtual machine.

Java is executed by the java program (java) using a java class file containing a static main method.

Python

To run Python from the command window, you must add the Python installation directory to your path, then you can run Python scripts as shown:

C:\work\python>set PATH=%PATH%;C:\python25

C:\work\python>python addarray.py
The sum is 45

You can also run Python interactively:

C:work\python>python
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Hello World!"
Hello World!
>>> quit()


Maintained by John Loomis, last updated 31 Dec 2007