Dec 29 2007

Command not found … do the following!

Tag: Linux, UbuntuAbhijeet Maharana @ 11:04 pm

I was working with a script and had to do a quick float calculation. I knew Accessories >> Calculator but wanted a command line utility. Struggling to remember the syntax for using bc, I tried

$ bc 2.38987 + 5.43534
File 2.38987 is unavailable.

Not quite amused with the response, I was wondering if it always has to be as geeky as

echo 2.38987 + 5.43534 | bc

or

bc -i
2.38987 + 5.43534

and not something like

calc 2.38987 + 5.43534

I happened to type ‘calc’ and hit return without really thinking about it, and Ubuntu promptly reported

The program 'calc' is currently not installed.  You can install it by typing:
sudo apt-get install apcalc
Make sure you have the 'universe' component enabled
bash: calc: command not found

I was impressed! It doesn’t just tell me “command not found” but also tells me how to enable it to find it! Given Ubuntu’s packaging and software repository system this may be simple to achieve but these “simple” little things are what make exploring Ubuntu a joy. I took my OS’s advice and in no time I was using “calc 2.38987 + 5.43534″ (I am using Ubuntu 7.04).


Dec 22 2007

Oracle 10g XE + Java on Ubuntu Feisty

Tag: Database, Java, Linux, Programming, UbuntuAbhijeet Maharana @ 3:30 pm

I needed a basic database on my Ubuntu Feisty system and decided to try out Oracle 10g Express Edition. The installation is pretty straightforward. Below is a log of what I did to get it running along with Java code to access the database.

My Setup:

  • AMD 3600+ 64 bit running Ubuntu 7.04 32 bit
    (2.6.20-15-generic kernel)
  • 2GB RAM

Download the .deb file for Oracle Database 10g Express Edition for Linux x86. I have oracle-xe_10.2.0.1_i386.deb which is a bit old now. The official installation guide is located here.

The installation steps are as follows:

  • Step1: Requirements and installation
    In short: 1.5 GB of disk space, 512 MB of RAM with 1GB swap. The Express Edition can use a maximum of 1GB of RAM even if more is available. The minimum and recommended requirements for installation can be found here . The installation went fine on my machine with 2GB of RAM and no swap space.

    Run the .deb. You may need to be connected to the Internet for the dependencies. The installation creates shortcuts under the Applications menu. However, we don’t need to use any at the moment.

    screenshot

  • Step2: Configuring the database
    Run

    sudo /etc/init.d/oracle-xe configure

    Use the default settings for Oracle Application Express (8080) and the database listener (1521) or change the ports if you have other servers using these ports.

    Enter the SYS/SYSTEM password. I used ‘manager’.

    Select ‘n’ for the option for starting the database at boot time since we can conveniently do the same using the created menu items. [See the troubleshooting section below]

    screenshot

    The user starting or stopping the database must be a member of the ‘dba’ group. This can be done from [Gnome Menu] >> System >> Administration >> Users and groups >> Manage groups. Select ‘dba’ and click on properties. Select user(s) to add them to this group.

  • Step3: Administration using the web interface
    Log into Application Express by hitting the URL http://127.0.0.1:8080/apex
    Enter SYSTEM / [password you had entered earlier]. There is a built-in user account HR which needs to be unlocked before it can be used. But, we will create a new account for our use.
    Goto Administration >> Database users >> Create user
    Enter ’scott’ / ‘tiger’ for the username / password and click Create.

    screenshot 1 | screenshot 2

    Log out and log in again as scott.

  • Step4: Create test tables
    Create a table

    • using the object browser
      Object browser >> create >> table

      screenshot 1 | screenshot 2

    • using a SQL Query
      SQl >> SQL Commands >> Enter Command
      You can enter multiple statements in the editor and execute one statement at a time by selecting a statement and hitting CTRL + ENTER.
      CTRL + ENTER works out of the box on Firefox 2.0.0.3. On Opera 9.24, you may need to change some settings. At this point I can’t say what.

      screenshot 1 | screenshot 2 | screenshot 3

  • Step5: Access database from Java

    Save the following code as OracleJdbcDemo.java. It prints values from the first column.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    import java.sql.*;
     
    public class OracleJdbcDemo
    {
    	public OracleJdbcDemo() throws Exception
    	{
    		Class.forName("oracle.jdbc.OracleDriver");
    		String connString = "jdbc:oracle:thin:@localhost:1521:XE";
    		Connection conn = DriverManager.getConnection(connString, "scott", "tiger");
    		Statement stmt = conn.createStatement();
    		ResultSet rs = stmt.executeQuery("select * from student");
    		while(rs.next())
    		{
    			System.out.println(rs.getString(1));
    		}
    	}
     
    	public static void main(String args[]) throws Exception
    	{
    		new OracleJdbcDemo();
    	}
    }

    Compile and run the code by supplying the ojdbc14.jar file in the classpath:

    $ javac OracleJdbcDemo.java
    $ java -cp "/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/jdbc/lib/ojdbc14.jar":. OracleJdbcDemo

    If you have downloaded and installed a JDK yourself, make sure the compiler and interpreter are both from the same installation.
    I have JDK1.6.0 installed on my system and this is the entry in my /home/abhijeet/.bashrc

    export JAVA_HOME="/apps/java/jdk1.6.0"
    export PATH=${JAVA_HOME}|>/bin:${PATH}|>



Troubleshooting


I came across a situation where just after the installation everything worked fine but after a reboot, neither http://localhost:8080/apex nor the Java program worked. However, the database was starting and stopping from the menu options as I was able to connect using the “Run SQL Command Line” utility found in the menu. First few lines of the exception thrown by the program are shown below:

Exception in thread "main" java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
        at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
...

I had answered “no” to start the database at boot time since I didn’t want it running when I am not using it. This may or may not be related to the problem above and I did not reinstall selecting “yes” to check.

After searching a little, this is what worked.
Register environment variables and start TNSLISTENER:

$ . /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
$ sudo /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/tnslsnr

If you get the following error after the first command

/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/nls_lang.sh: 114: [[: not found
/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/nls_lang.sh: 114: [[: not found

then edit /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/nls_lang.sh and change the first line from

#!/bin/sh

to

#!/bin/bash

Wait for sometime after starting the listener and both the web interface and Java program should start working.

[Reference: Oracle forums]


Dec 04 2007

Lively Kernel and Documancer

Tag: Javascript, Links, Linux, Programming, WebAbhijeet Maharana @ 10:47 pm

Lively Kernel

From being used in web pages to add “some” interactivity to being used in a web based “kernel”, Javascript has come a long way. A team at SUN has developed a kernel that provides an execution environment for applications meant for the browser. From the Sun Labs Lively Kernel page, “The Sun Labs Lively Kernel is a new web programming environment developed at Sun Microsystems Laboratories. The Lively Kernel supports desktop-style applications with rich graphics and direct manipulation capabilities, but without the installation or upgrade hassles that conventional desktop applications have.” Written entirely in Javascript, the Lively Kernel emphasizes on treating Web applications as real applications and aims to build a platform using a minimum number of technologies. So its Javascript for most of the things.

It comes with the ‘Morphic’ graphic library which was originally written for the ‘Self’ system at Sun. From the same page, “Morphic is a user interface framework that supports composable graphical objects, along with the machinery required to display and animate these objects, handle user inputs, and manage underlying system resources such as displays, fonts and color maps. A key goal of Morphic is to make it easy to construct and edit interactive graphical objects, both by direct manipulation and from within programs.”

It even includes an IDE that allows JavaScript applications to be inspected and modified from within the system itself. It is currently supported on Safari 3.0.3 public beta release. Runs on Firefox but has a lot of related nasty bugs and locked my browser. IE support is yet to be added.

Screenshot (image is located at SUN’s site)

Related links:

Documancer

This is an invaluable tool in any programmer’s toolkit. It is a documentation browser which supports HTML, DevHelp, Info pages, Manual pages, remote WWW sites and pydoc. But the feature that got me hooked is the full-text search. Specially when it comes to HTML documentation such as those created with javadoc. Documentation for the Java APIs is also available in WinHelp and HTMLHelp formats which enable searching. But for other libraries which use javadoc to generate their documentation, it is a pain to search for something. Specially when you are new to the library. That is where this gem shines.

Its UI is based on Gecko, Mozilla’s HTML rendering engine and it presents documentation of all formats in the same way. It supports bookmarks as well. It is implemented in wxPython and is available for Unix and Windows. It comes with a nice installer which takes care of all dependencies.

Related links:

Apparently, there is another Gecko! (that sells clothes) which comes before the Mozilla page in a Google search for “gecko home page”.


Dec 02 2007

Javascript 3D tricks

Tag: Javascript, Programming, WebAbhijeet Maharana @ 9:27 pm

Opera 9.50 Alpha [Windows download ~ 5 MB] has added support for the 3D canvas. Tim Johansson, responsible for the Canvas and image decoding support in Opera has a blog entry on this. Firefox 3 Beta 1 [Download page] has an extension for this purpose and Vladimir’s blog entry on the same is located here.

I did a quick search for “javascript 3d graphics” to see if any libraries have sprung up for the calculations and other 3D chores. But I found some other neat stuff instead. ‘Neat’ not in the sense that they render some heavy-duty graphics in your browser but they use simple tricks to draw 3D objects. However, the authors may not agree with the “simple” part.

  1. JS3D:
    Renders 3D objects using plain text. The text is placed in DIV tags whose coordinates are calculated by 3D matrix operations. The objects can also be interactive. You have to see the demos to experience it. Specially the Double Helix demo.

  2. Triangles in Javascript:
    This one uses Javascript/DOM/CSS to render 3D triangles complete with lighting. And it achieves this by using plain old borders applied to DIV tags!

While they may not be suitable for writing games or other heavy graphics, but these nifty little tricks do impress a lot.