From J Wiki
Jump to navigation Jump to search

J can be used for CGI (common gateway interface) programming. Here are some considerations and examples.

Note: the examples assume Linux, but Windows would be similar - feel free to add Windows-specific references.

J Interpreter

For most purposes, the jconsole should suffice for CGI, but the full J system can also be used if required.


You need at least a script with definitions to read the CGI parameters and create the required CGI output. What else is needed depends on your application. This can include the J standard libraries.

A CGI script with a minimal set of required definitions is cgi.ijs in the web subversion repository. This is a standalone script, and can be used for testing.

The script needs to exit at the end, otherwise the J session remains alive. If necessary, wrap commands in try catch, so that after a failure, the catch line does the exit.


File permissions should allow the web server to run the J interpreter, and read the scripts.

CGI script

This can be a hash bang script to load the J interpreter and run the script.

Simple Example

Download the cgi.ijs script Copy cgi.ijs from below, and save it as test.ijs. It can be saved anywhere, but ensure that the web server has read access. Edit the script and uncomment the last line, so that the script runs cgitest''.

Create a test.cgi script in your cgi-bin directory, as follows. Use the correct pathnames:

/usr/lib/j/jconsole -jprofile /home/chris/test.ijs

This script should be executable, and can be tested by running it from the command line. The session should show the html generated. For example, run:


Next, save the following as file test.html in your web directory:

<form action="/cgi-bin/test.cgi" method="get">
<input type=text value="get me" name=tget><br>
<input type="submit" value="Run">

Open this file in your browser, and press the Run button. The resulting display should show the timestamp, and the edit box name and contents:

2006 1 3 13 44 36
|tget|get me|

If this does not work, you should check file permissions to ensure that the web server has access, and also check the server log.

J Environment Variable

No special environment settings were needed for the simple example above, or whenever you are just loading a single script. However, you should define the J environment variable if you need to load the J profile, i.e. if you do not use the -jprofile flag. You can do this either in the test.cgi script, or as part of the web server configuration. For example, the following test.cgi sets the J environment variable, and loads J with the standard profile. Use the correct variable name for your version of J:

export JPATHj601=/usr/lib/j
/usr/lib/j/jconsole /home/chris/test.ijs

Alternatively, for Apache, edit httpd.conf to include:

SetEnv JPATHj601 /usr/lib/j

Examples and References

John Howland's j-web system runs the Trinity University Computer Science Department [1] from J cgi scripts.

The J forum search is an example of a page using a J cgi script. For the source, see the web subversion repository.

Oleg Kobchenko's JHP hypertext processor allows J code to be embedded in a web page.

CGI introduction and reference.


(Currently, uploading this script as a file is encountering an unexpected error.)

NB. cgi
NB. This is a standalone script, and can also be included with other
NB. scripts, as there is no conflict with the standard library.
NB. main definitions:
NB. cgiparms - return cgi parameters as table of names,.values
NB. cgitest - use to test cgi

18!:4 <'z'

LF=: 10 { a.
stdin=: 1!:1@3:
stdout=: 1!:2&4
stderr=: 1!:2&5
getenv=: 2!:5
exit=: 2!:55

NB. =========================================================
NB. cgiparms v return cgi parameters
cgiparms=: 3 : 0
select. getenv 'REQUEST_METHOD'
case. 'GET' do.
  p=. getenv 'QUERY_STRING'
case. 'POST' do.
  p=. stdin''
case. do.
  p=. ''
cgiparms1 p

NB. =========================================================
NB. cgiparms1 v parse parameter string
cgiparms1=: 3 : 0
y=. ' ' (I. y = '+') } y
y=. <;._1 '&', y
ndx=. y i.&> '='
nms=. ndx {. &.> y
val=. (ndx+1) }. &.> y
cgiparms2 &.> nms,.val

NB. =========================================================
NB. cgiparms2 v url decode
cgiparms2=: 3 : 0
if. -. '%' e. y do. y return. end.
y=. <;._1 '%00', y
a=. '0123456789abcdef0123456789ABCDEF'
p=. a. {~ 16 #. 16 | a i. 2 {.&> y
}. ; p ,&.> 2 }.&.> y

NB. =========================================================
NB. cgitest v defines html with a timestamp and cgi parameters
cgitest=: 3 : 0
stdout 'Content-type: text/html',LF,LF,'<html><body><pre>',LF
stdout LF,~":6!:0''
stdout ,LF,.~":cgiparms''
stdout '</pre></body></html>'
exit 0

18!:4 <'base'

NB. =========================================================
NB. decomment for testing:
NB. cgitest''