Setting Up the Development Environment

Phase 0


Context

Our MIS division has identified the applications needed to develop, test and deploy FabFlixs so that it is consistent with our Windows XP production environment. Fortunately, all are freely available and have proven their stability and robustness.

PostgreSQL has been chosen as the database.

To develop Web software, we need the Java development environment; the most current version, 6.0, is required. In addition to the Java language itself, we need the JDBC libraries so that the developed Java code can interface with PostgreSQL.

We need C to allow us to add capabilities to the built-in SQL available in PostgreSQL. Management has decided to use the free version of Microsoft Visual C++, called the Microsoft Visual C++ 2008 Express Edition. It has a C compiler, a powerful linker and appears to be the best documented of the available free C compilers for Windows. Its for-sale cousin, the C/C++ environment in Microsoft's .NET Framework, has a huge user base, so there's vast material about it on the Web and elsewhere; as figuring out the right compiler and linker switches could be tricky, having good documentation for the environment and language is likely to be very helpful.

We install Tomcat, a service that handles dynamic web content and servlets, features we will need to deploy FabFlixs.

We also install pgAdmin, which provides a graphical interface to PostgreSQL. This tool is easier to use for some tasks than psql, the command-line interface to postgreSQL.

Management has okayed using any environemnt (Mac, Windwos, Linux, Windows Vista) for developing any aspect of FabFlixs that can be ported to Windows without change.

In developing FabFlixs, you may use any software tools to which you have legitimate access. In particular, consider using an integrated development environment (such as Eclipse or Netbeans); the latter is also available in a package with Java from java.sun.com.


References


Notes

If you want to use a non-Windows machine (e.g., a Mac, a computer running Linux) for development, that's fine—but your installation procedures will be different than those given here. Limits on course resources prevent us from supporting multiple environments. We'll help you as we can with installs on non-Windows platforms, but we cannot be responsible for any substantial research or debugging that may be required to get such a system to work correctly.

If using a Linux or Mac OS X machine, consider obtaining a Windows emulator for your operating system (such as VMware Fusion or VirtualBox; the latter is open source and thus free). These virtualization programs allow Linux and Mac O/S computers to run Windows. Install the emulator, then the Windows O/S; you can then install the software suite for FabFlixs development, as described here for Windows, within that environment.

These installations do not all need to be done in one sitting; a convenient place to break is when a package's installation is complete. (Testing of that installation can be done at a later time.) We recommend you install the packages in the order given, as some later ones require the presence of earlier ones (.e.g., Java needs to be installed for Tomcat to be installed).


What to Turn In

Nothing; the point of this phase is to set up tools so you are ready to undertake the FabFlixs phases that follow.


Installing Java

You will need Java on your machine; we strongly recommend getting the latest version, Java 6.0 (also known as Java 1.6.0) update 13. If you do not already have it installed on your machine, or you want to update from an older version:

  1. Go to http://java.sun.com/javase/downloads/index.jsp.

  2. Choose either the stand-alone JDK 6 Update 13 or that JDK bundled with the NetBeans development environment (don't choose the JRE--that does not have the compiler).

  3. For Platform, choose Windows; click in the box to agree to the license agreement; click on Continue.

  4. Click on the file name (jdk-6u13-windows-i586-p.exe)

  5. Click Run on the pop-up window that appears to begin downloading. The download typically takes several minutes.

  6. If a security window appears, click Run.

  7. Next, the Java license will display; click Accept.

  8. Accept the default for the direftory in which to install Java and the setups for the various components presented to you (these will differ depending on whether you are downloading just the JDK or both the JDK and NetBeans); click each time you are given a screen with that option.

  9. Java may warn you at some point that installation cannot proceed unless an application or two are quit. (This typiically occurs if your browser is still open.) If this warning appears, cllick the box next to Quit the applications listed.. and then click Retry.

    It can take some time for installation to complete.

  10. Click Finish.

  11. Your Web brower may now open to Java's regiatration page. Register if you wish; registrationis not required.

  12. Right-click on Start->My Computer; choose Properties. Click the Advanced tab. Click Environment Variables. Under System Variables, find the PATH variable and Edit it; add this to the end of the Variable Value:

    ;C:\jdk1.6.0_13\bin

    (If you had a previous version of Java installed on this machine, modify the existing jdk path to be 6.0_13)

    Click OK.

    Then find the CLASSPATH variable. If it doesn't exist (if you've never used your system to develop Java code, it probably won't), click the New... button. For Variable Name enter CLASSPATH; for Variable Value enter

    .;C:\jdk1.6.0_13\lib.

    or add that string onto the end of the existing CLASSPATH.

    (Again, if you had a previous version of Java installed on this machine, modify the existing jdk path to be 6.0_13)

  13. Click OK; click OK again; click OK again!

Java should now be ready to use. To check that the CLASSPATH and PATH variables are properly set, open a command window and enter javac -version. If the version prints, the PATH is working; if you get the message 'javac' is not recognized as an internal or external command, operable program or batch file, either the PATH is incorrect, or you did not start with a fresh command line window after you changed the PATH command. When trying to run a java program, if you get a ClassNotFoundException exception, check the CLASSPATH variable.

Once you are sure Java is working, you can (but do have have to) delete any old versions of Java from your machine.


Installing PostgresSQL, psql and pgAdmin

About pgInstaller, psql and pgAdmin

pgInstaller (PostgresSQL Installer) installs postgesSQL, psql, pgAdmin and related tools and libraries. EnterpriseDB has provided a "one click installer" that downloads and installs this software with minimal additional setup.

psql is a command-line tool for developing and administering PostgreSQL databases; it's distributed as part of the PostgreSQL software system. pgAdmin is a tool that allows for the same sorts of interactions as psql but via a graphical user interface. Typically, it's easier in psql to execute simple, common commands (for example, for ones where you've memorized the syntax) and to run lengthy or to-be-used-many-times SQL scripts. pgAdmin is typically the better tool for complicated one-time actions. Become familiar with both; use whichever one (or others, for that matter) you prefer. Note that sometimes the FabFlixs requirements will mandate that certain work (e.g., a script) is to be processed by a specific tool (e.g., psql); be sure to meet those requirements.

Installation

  • Go to the PosgreSQL 8.3 one-click download page. Click on the Windows button; in the pop-up window, click Run; the installation file will download. It can take several minutes for the download to complete.

    The installation will now occur; it may take a few minutes. When it's done, a screen appears saying installation is complete.

    If that this point you wish to continue with installing JDBC, leave Launch Stack Builder at exit checked; if not, uncheck that box.

    Click finish to leave pgInstaller.


    Installing JDBC

    Fabflixs will be using Java programs (among others) to communicate with the FabFlixs PostgreSQL database. JDBC is "community supported" software that enables communication between Java and PostgreSQL databases; the pgIntaller download includes a tool called Stack Builder that makes it easy to obtains JDBC drivers (and other tools) from the Web.

    If you left pgInstaller with Launch Stack Builder at exit checked, you will now see a welcome screen for Stack Builder. If you exited the installer without launching Stack Builder, go to Start -> All Programs -> postgreSQL 8.3 -> Application Stack Builder to get to the weclome screen.

    A confirmation screen will appear. Click Next. (There's no need to change the download directory.) A progress box will pop up anddownload will commence (it may happen very quickly, or take a few seconds, depnding on the speed of the download server). When it's done, a confirmation box will appear; click Next to launch a wizard that will guide you through the JDBC installation..

    The JDBC drivers are now installed. There are several; the one compatible with Java 6 and Tomcat is postgresql-8.3-603.jdbc3.jar. By the way, a "jar" file is just a compressed archive of Java class files, quite similar to a ZIP file. Java uncompresses jars "on the fly" as a program runs.

    You can now add the appropriate JDBC to your CLASSPATH environment variable if you want it available every time you compile a Java program-- the string is

    ;C:\yourFolderName\postgresql-8.3-603.jdbc3.jar

    Note the ";", which separates this CLASSPATH entry from the previous one.

    Instead, you can place the driver into the CLASSPATH switch on the javac command line when you need it (which is whenever you run a java class that uses JDBC commands); for example, if the program to run is PostgresJDBC then the command line is

    java -classpath .\;C:\yourFolderName\postgresql-8.3-603.jdbc3.jar

    Note the need for including .\, the current directory, in the classpath. It is not included automatically, as the classpath switch completely overrides any default or classpath environment variable settings.

    If you want to add something to your existing classpath for just this compile, start the -cp parameter with %CLASSPATH%, and since the existing classpath may have spaces in its file names, enclose the parameter in quotes; e.g.,

    java -classpath "%CLASSPATH%;C:\postgresql-8.3-603.jdbc3.jar" PostgresJDBC

    If you are using a development IDE, you may need to "tell it" about the JDBC jar file (of course, how that is done depends on the IDE you are using).


    Installing Microsoft Visual C++ 2008 Express Edition

    In Phase 3, you will add C functions to the FabFlixs database to give it functionality that SQL and built-in functions do not provide. PostgreSQL expects these added functions to be in a dynamic link library ("DLL") file. To make a DLL from C source code, one needs a C compiler and a linker that can make DLLs.

    For the reasons given above, management has chosen Microsoft Visual C++ 2008 Express Edition for C development. However, if you already have C/C++ from .NET installed on your machine, you do not need to install C++ Express; you should be able to make DLLs with .NET (following the same procedure we discuss for Express in Phase 4). And you can use a different C environment, provided it produces DLLs that are PostgreSQL compatible.

    To install Microsoft Visual C++ 2008 Express Edition (with service pack 1):


    Modifying the PostgreSQL libraries for a Windows environment

    The PostgreSQL-provided C include files are when writing C functions to be added to postgreSQL databases. They were written for a Unix environment. To work correctly with Windows, we need to provide the file libint.h. The contents of libint.h are never used(!) but another include file expects it to be present, so it must be!). We also need to comment out some lines of pg_config_os.h and elog.h that redefine variables that are already defined in Windows-based C libraries (failing to do so results in a doubly-defined variables that cause compilation errors).

    To fix these problems:


    Enabling pgAdmin and other applications to communicate with PostgreSQL

    Since the PostgreSQL server resides on the same computer we are using, that make this computer a "local host" of that server. By definition, the local host server has IP address 127.0.0.1, with domain name localhost. By convention, port 5432 is used for the localhost-PostgreSQL communications link. PostgreSQL comes preconfigured to allow communication to localhost on port 5432. The installer sets up two servers: one on 5432 that is accessbile by a superuser (to which the password is not provided!) and another that is set to the user "postrgres," has superuser privileges (which we need) and has the password that you provided during installation. Since Tomcat and JDBC expect communication to happen on port 5432, we need to change the port on the server with user "postgres." (There is a way to get JDBC to talk to a database on ports other than 5432, and there is supposed to be a way to do so for Tomcat. We were able to get the former to work but not the latter, so we're sticking with 5432.) Since we won't be using default database, we won't have a communications conflict between the two databases. The easiest way to change the maintenance database to port 5432 is to

    Postgres' default behavior is to "listen" for all addresses, but to then restrict who may access postgres; the default is to set the listen_addresses parameter in the postgresql.conf configuration file to "*" and to allow access only from programs running on the local machine, using password authentication; that is, setting the host line for IPv4 (the current Internet communication address scheme) in the pg_hba.conf file to

    host all all 127.0.0.1/32 md5

    This is the standard configuration for your projects, as it restricts testing to your machine, and enforces password checks, which is part of the security Fabflixs (eventually) implements.

    Enabling remote computers to communicate with the postgreSQL server

    If you want PostgreSQL to accept connections from other computers (for example, if you are doing a demo for us on one machine, but your database exists on another, say a computer at home), you need to change pg_hba.conf to specify which IP addresses are allowed to communicate with PostgreSQL.

    To enable "outside communication,"

    Of course, save the file and exit when you're done.

    For these changes to take effect, you have to shut down the postgreSQL server (if it is running) and start it up again. If the server was running:

    Note the use of a fixed IP address: A remote machine connecting via an ISP that assigns a different IP address each time a connection is made will likely not get the IP address for which PostgreSQL allows access.

    If your machine has a firewall, the firewall may need to be configured to let through the various addresses that you have allowed to communicate with PostgreSQL.

    If you have set up your accesses so that a password is required, when you run psql on a chosen database with a particular user id (i.e., use the -U switch), you will be asked for the that user's password.

    If you want to do something fancier, you will need some understanding of Internet networking and its jargon (or need to ask someone who has such knowledge) to configure things properly. You can also get more information from the comments in the file, from the PostgreSQL documentation and from pgAdmin's Help function.

    Creating an end user

    To test that the postgreSQL server is properly installed and configured, and that Java and JDBC are properly installed, we create an end user, a tiny test database and a small Java program that uses JBDC to "talk" to the database. We show how to communicate with the database both via psql and pgAdmin, to introduce you to each approach. (Read the documentation for these tools to learn more.) We start with creating the end user. (Since a superuser can do "anything," if we used it to connect to the database, we would not spot problems a more "normal" user would have. So, we create a more "normal" user for this test.)

    To use postgreSQL or any of the tools the communicate with it (e.g., psql and pgAdmin), the postgreSQL server must be running; so, if it is not, start it up: Start -> All Programs -> PostgreSQL 8.3 -> Start Server. A command line box will appear with a message that the server is starting; it will quickly tell you it was started. Press Return to exit.

    Using psql:

    Run psql: Start -> All Programs -> PostgreSQL 8.3 -> SQL Shell (psql). You will be asked to specify the server to use; accept the default (localhost). (You accept defaults by just hitting Return).Then you'll be asked to specify the database to emply; accept the default, postgres. Then you're asked for the port; again, accept the default, port 5432. Next, your asked for the user; accept postgres, the name of the default superuser. Finally, you'll be asked for the password for the postgres user; supply the one you specified during installation of postgreSQL. If all has gone well, you'll see the psql welcome message.

    At the prompt, enter the text in italics given below. Text not in italics is text PostgreSQL prints out; text in square brackets is a description of PostgreSQL-printed text (not the actual text itself).

    Press any key to exit the window.

    Using pgAdmin:

    Building the test database

    Now you create a movie database with one table, stars, that has just one entry (our Governator)...just enough to test a JDBC connection. When creating fields (columns) of the stars table below, create them in the order given; a different order will cause errors in the JBDC program: JDBC uses positional indicators to reference columns, so if you change the entry order of the columns, the program will (try to) access incorrect fields.

    Using psql:

    To enter PostgreSQL as user 'testuser', launch SQL Shell (psql) as before, but now, for user, enter testuser, and for the password, testpass.

  • Now create a database with a small table with one entry. Be sure to connect to the moviedb (\c moviedb below) or the tables you create will not be associated with the movie database, causing problems down the road.

  • postgres=> create database moviedb;
    CREATE DATABASE
  • postgres=> \c moviedb;
    You are now connected to database "moviedb".
  • moviedb=> CREATE TABLE stars(id INT NOT NULL PRIMARY KEY,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL,
    dob DATE,
    photo_url VARCHAR(200));

    NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "stars_pkey" for table "stars"
    CREATE TABLE
  • moviedb=>INSERT INTO stars VALUES(755011, 'Arnold', 'Schwarzeneggar', '07/30/1947', 'http://www.imdb.com/gallery/granitz/2028/Events/2028/ArnoldSchw_Grani_1252920_400.jpg?path=pgallery&path_key=Schwarzenegger,%20Arnold');
    INSERT <a number> 1
  • moviedb=>\q

    Using pgAdmin:

    There are a few ways to add Arnold's information into the table; here's a straight-forward one:

    Implementing the test Java program

    Now enter and compile the following java program; it uses JDBC to communicate with your test database and retrieve some basic information about it. Study it to get a feel for how JBDC works. (This is just a test program; a production program would clean some things up, such as not throwing an Exception out of main()!)

    import java.sql.*;
    
    public class PostgresJDBC
    {
     	public static void main(String[] arg) throws Exception
    	{
    		Class.forName("org.postgresql.Driver").newInstance();
     		Connection connection = DriverManager.getConnection(
     		"jdbc:postgresql://localhost/moviedb", "testuser", "testpass");
     
     		Statement select = connection.createStatement();
    		ResultSet result = select.executeQuery("Select * from stars");
      		System.out.println("The results of the query:");
     		ResultSetMetaData metadata = result.getMetaData();
     		System.out.println("There are " + metadata.getColumnCount() + " columns");
     
     		for (int i = 1; i <= metadata.getColumnCount(); i++)
     			System.out.println("Type of column " + i +" is " + metadata.getColumnTypeName(i));
    
     		while (result.next())
     		{
     			System.out.print("id = " + result.getInt(1));
     			System.out.println(" Name = " + result.getString(2));
     		}
     		connection.close();
    	}
    }
    

    Replace localhost with the IP address of the computer you are using, if it is not the same machine on which the database resides.


    Installing and testing Tomcat

    To support dynamic content on web pages and Java servlets (extensions of a server's basic services), we use Tomcat, a freeware package form the Apache Software Foundation. (You will be using its capabilities starting in Phase 2.) To install Tomcat

    1. Go to http://tomcat.apache.org; read a bit!

    2. Under Download, click on Tomcat 6.x (look on the left-hand side of the page).

    3. Under 6.0.18, select Windows Server Installer under Binary Distributions, Core; save the EXE file.

    4. Launch the EXE file you just downloaded.

    5. Read the instructions; click on Next; read the license and agree.

    6. On this screen, make sure Tomcat and Start Menu Items are checked. Leave Documentation checked if you want the documentation on your drive (it is also available at the Tomcat site); check Examples if you want some examples of servlets. Click Next.

    7. Leave the destination folder as is; click Next.

    8. Leave the port at 8080—this is crucial. Choose an administrator login name and password: do not forget them! Click Next.

    9. Check that the directory listed is indeed where you have the java virtual machine (java.exe and its associated files) stored; if you used the default directories for java when installing it above, it is C:\Program Files\Java\jre1.6.0_07. (Yes, iat the top of Window it says to select a 5.0 path; don't worry, the message is just a bit out of date!) Click Install.

    10. Wait a few seconds while installation occurs.

    11. Click Finish. A window should appear showing Tomcat starting up; the release notes file should appear in a window. Because Windows uses a different end-of-line marker than Unix, the notes may be quite difficult to read; don't wory, just close the window. If you need to read the release notes at some point, you can find them here.

    To be sure Tomcat is running, right click on the feather-like icon in the lower right of the Windows toolbar (if you hover over the correct icon, Apache Tomcat will appear); Start service should be grayed. To stop Tomcat at any time, right-click on the icon and select Stop service. You can also start and stop Tomcat on the General panel of the Apache Tomcat properties window (double click on the Tomcat icon in the toolbar).

    To test that Tomcat is working and communicating with PostgreSQL properly, we've prepared a servlet that performs basically the same tasks as the JDBC program; it queries the test database and returns some structural and content information. To run this program:

    1. Make sure the PostgreSQL database server is running. Be sure communications to the database server have been set up as described in Enabling pgAdmin and other applications to communicate with PostgreSQL, above, before proceeding.

    2. Download TomcatTest.war

      A WAR file is a "Web Application Archive." It contains a compressed directory of the files needed for a ready-to-run java servlet that Tomcat will launch when you go to the Web page designated in the WAR. (A WAR file must a have certain directory structure and contain appropriate files (with the appropriate contents) for Tomcat to launch the servlet. Details about WAR files will be covered in Phase 2.)

    3. Use a Web browser to go to http://localhost:8080; this is the web page that lets you access locally (stored on your computer) hosted Web services. After installing Tomcat (which is a Web service), it was set to the Tomcat welcome page; that page should appear.

    4. Click on Tomcat Manager; enter your admin user login and password (given when you installed Tomcat); click OK. If your login and password are correct, the Tomcat Web Application Manger page will appear.

    5. Under War file to deploy, enter in or Browse to find the location of TomcatTest.war; then click the Deploy button. This action decompresses the WAR and places its folders into their required locations. TomcatTest should now appear in the Applications list.

    6. Go to http://localhost:8080/TomcatTest/servlet/TomcatTest. Tomcat will run the servlet TomcatTest and print its output onto the web page. If the output is database information, all is well. If instead you see a message that an SQLException occurred, check that the PostgreSQL server is running, that it is configured to allow TCP/IP access and that the test database with the correct user name, user password and table structure has been created (see above).

    You are now ready to proceed with FabFlixs development!


    Setting Up Windows XP Computers for a Remote Demonstration

    If you plan to do your demontrations by connecting an XP computer at a meeting place to a remote XP computer that houses the software to be demonstrated, follow the instructions on the Get started using Remote Desktop with Windows XP Professional web page. Some additional things to keep in mind:

    As previously discussed, it will be up to you to make remote computing work when one or both of the machines are running something other than Windows XP; we can only support XP-to-XP connections.

    Written for ICS185 Spring 2005 by Norman Jacobson, March 2005.
       Some sections adapted from ICS185 Winter 2005 exercises written by Chen Li, and
       sample code and FAQs written by Chen Li and Kirill Petrov.
    Updated for CS122B Spring 2007 by Norman Jacobson, March 2007.
       Some provided information was obtained from the CS122B Winter 2007 'Fabflix:
       Project Setup the development environment' and 'FabFlixs: Project 5' web pages.
    A couple of sections rearranged and text updated to note that a password is needed if a user is invoked
       with md5 access, by Norman Jacobson, April 2007.
    Updated to include discussion of pgAdmin and to remove dcsussion of the Google Web
       by Norman Jacobson, September 2007.
    Added a warning about using older versions of postgresql, by Norman Jacobson, December 2007.
    Revised to reflect use of native Windows postgreSQL 8.3.3, use of pgInstaller, C/C++
       Express and new versions of software and other minor edits, by
       Norman Jacobson, September 2008.
    Minor revisions to reflect postgreSQL 8.3.4 and correct a few typos, by Norman Jacobson, October 2008.
    Minor revision to reflect needed replacing of elog.h to remove a symbol defined elsewhere, by
       Norman Jacobson, December 2008.
    Revised to include information about virtualization of Windows (for Linux and Mac users);
       major revisiions to installation insturctions and other updates to reflect postgreSQL
       8.3.7; minor updates to other s/w used for FabFlixs development, by Norman
       Jacobson, March 2009.
    Removed unneeded ":" from "localhost" parameter in JDBC test program (so it works now!), by
       Norman Jacobson, April 2009.