This section describes how to execute programs from the command
line. You can use this information to run programs that you obtain from
the recipes
distribution or that you
write yourself. The first set of instructions applies to scripts written
in Perl, Ruby, PHP, or Python. A second set of instructions applies to
Java programs.
The example programs can be found in the progdemo directory of the recipes
distribution.
The following discussion shows how to execute scripts, using Perl for the examples. However, the principles are similar for Ruby, PHP, and Python scripts.
Begin with an example script named perldemo.pl that consists of a simple print statement:
print "I am a Perl program. ";
A script-execution method that works on any platform is to invoke the perl program and tell it the name of the script to run:
%perl perldemo.pl
I am a Perl program.
For a script written in another language, invoke the ruby, php, or python program.
It’s also possible to set up a script so that it is directly executable. The procedure is different for Unix and Windows. Both procedures are described here.
On Unix, to make a script directly executable, include a line at
the top of the file that begins with #!
and that specifies the pathname of the
program that should execute the script. Here is a script named
perldemo2.pl with a #!
line that names the perl program (if perl has a different location on your
system, you’ll need to change the pathname):
#!/usr/bin/perl print "I am a Perl program. ";
Next, enable the executable access mode with chmod +x:
%chmod +x perldemo2.pl
At this point, the script can be run the same way as its earlier
counterpart (that is, you can use perl
perldemo2.pl), but now you should also be
able to execute it by entering just its name. However, assuming that
the script is located in your current directory, your shell might not
find it. The shell searches for programs in the directories named in
your PATH
environment variable, but
for security reasons, the search path for Unix shells often is
deliberately set not to include the current directory (.
). In that case, include a leading path of
./
to explicitly indicate the
script’s location:
%./perldemo2.pl
I am a Perl program.
If you install the script by copying it to a directory that is
named in your PATH
setting, the
leading path is unnecessary when you invoke the script.
On Windows, the procedure for making scripts executable is somewhat different. chmod is not used. Instead, programs are treated as executable based on their filename extensions (such as .exe or .bat). Windows allows you to set up filename associations such that a program can be associated with filenames that end with a particular suffix. This means that you can set up associations so that Perl, Ruby, PHP, or Python are used to execute files with names that end in .pl, .rb, .php, or .py, respectively. (In fact, the installer for a given language might even create the association for you.) Then you can invoke a script with a given extension directly from the command line without naming its language interpreter:
C:>perldemo.pl
I am a Perl program.
No leading path is needed to invoke a script that is located in the current directory because the Windows command interpreter includes that directory in its search path by default.
Keep the preceding principles in mind when you run scripts from within a directory of the recipes distribution. For example, if you are currently located in the metadata directory of the distribution and you want to execute the get_server_version.rb Ruby script, you can do so with either of these commands on Unix:
%ruby get_server_version.rb
%./get_server_version.rb
On Windows, you can use either of these commands:
C:>ruby get_server_version.rb
C:>get_server_version.rb
Java programming requires a software development kit (SDK). If a Java SDK is not already installed on your system, obtain one and install it. For Solaris, Linux, and Windows, Java SDKs are available at java.sun.com. For Mac OS X, current versions come with an SDK; for earlier versions that do not, javac and other support needed for building Java applications is included in the Developer Tools distribution available at connect.apple.com.
After verifying that a Java SDK is installed, set the
JAVA_HOME
environment variable if
it is not already set. Its value should be the pathname of the
directory where the SDK is installed. If the SDK installation
directory is /usr/local/java/jdk
on Unix or C:jdk on Windows, set
JAVA_HOME
as follows:
For a shell in the Bourne shell family (sh, bash, ksh):
export JAVA_HOME=/usr/local/java/jdk
If you are using the original Bourne shell, sh, you may need to split this into two commands:
JAVA_HOME=/usr/local/java/jdk export JAVA_HOME
For a shell in the C shell family (csh, tcsh):
setenv JAVA_HOME=/usr/local/java/jdk
For Windows, get to the Environment Variables window as
described earlier in “Setting Environment Variables,”
and set JAVA_HOME
to this
value:
C:jdk
Adjust the instructions for the pathname actually used on your system.
With a Java SDK in place and JAVA_HOME
set, you should be able to compile
and run Java programs. Here is a sample program:
public class JavaDemo { public static void main (String[] args) { System.out.println ("I am a Java program."); } }
The class
statement indicates
the program’s name, which in this case is JavaDemo
. The name of the file containing
the program should match this name and include a .java extension, so the filename for the
program is JavaDemo.java. Compile
the program using javac:
%javac JavaDemo.java
If you prefer a different Java compiler, just substitute its name. For example, if you’d rather use Jikes, compile the file like this instead:
%jikes JavaDemo.java
The Java compiler generates compiled byte code to produce a class file named JavaDemo.class. Use the java program to run the class file (specified without the .class extension):
%java JavaDemo
I am a Java program.
To compile and run MySQL-based programs written in
Java, you’ll need the MySQL Connector/J JDBC driver (Connecting, Selecting a Database, and Disconnecting). If Java cannot find the driver,
you’ll need to set your CLASSPATH
environment variable. Its value should include at least your current
directory (.
) and the pathname of
the MySQL Connector/J driver. If the driver has a pathname of
/usr/local/lib/java/lib/mysql-connector-java-bin.jar
(Unix) or C:Javalibmysql-connector-java-bin.jar
(Windows), set CLASSPATH
as
follows:
For a shell in the Bourne shell family (sh, bash, ksh):
export CLASSPATH=.:/usr/local/lib/java/lib/mysql-connector-java-bin.jar
For a shell in the C shell family (csh, tcsh):
setenv CLASSPATH .:/usr/local/lib/java/lib/mysql-connector-java-bin.jar
For Windows, get to the Environment Variables window as
described earlier in “Setting Environment Variables,”
and set CLASSPATH
to this
value:
.;C:Javalibmysql-connector-java-bin.jar
Adjust the instructions for the pathname actually used on your
system. You might also need to add other class directories or
libraries to your CLASSPATH
setting. The specifics depend on how your system is set up.
18.118.138.195