Guides/Standalone Applications

From J Wiki
Jump to: navigation, search

Overview

A J application consists of a script and any other required files, plus required J binaries. J is loaded, and in turn loads the script to start the application.

The full J development environment is not required for most applications. Instead, it is needed only for applications such as Debug, that are intended to be used for J development.

Applications that do not require the full development environment are "standalone" applications. There are some special considerations when building such applications, for example, the application may work fine within the development environment, but may fail when run outside it.

Required Files

A standalone application consists of the application script and other required files, plus the required J binaries.

For information on required binaries, see Binaries - Shared Libraries - Directories. For example, a normal windows application will require at least j.exe and j.dll, and if it uses regex, then also jpcre.dll.

The application script is an ordinary text file, and can be created in any way. The recommended procedure is to use Project Manager, as this takes care of most of the details needed to create an application script.

Typically an application has a single script, which is created by joining together one or more source scripts. However, there is no problem in an application having several scripts, that are loaded by an initial script.

Command Line

The command line documentation is at Command line parameters.

A simple example of a windows GUI application with a single script file, would have a directory containing j.exe, j.dll and the script file, and a command line such as:

c:\myapp\j.exe -jprofile myscript.ijs

Here, -jprofile means do not load the profile, and myscript.ijs is the application script. You can try this in a temporary directory. Copy in j.exe and j.dll from your J directory, and then create a script myscript.ijs with contents:

11!:0 'mb "J" "hello world"'

Next create a shortcut with a command line as above. Click the shortcut to see the message.

Note that the script uses 11!:0, and not wd or wdinfo. This is because the profile is not loaded, so definitions like wd or wdinfo are not available. If your application makes use of such definitions, they need to be built into the script - in Project Manager this means selecting the Include Standard Libraries checkbox in the Project|Build dialog.

Note also that as soon as the message box is clicked, it closes the J task. A J standalone application remains active only if there is code being executed, or if there is an active form. Otherwise, on a return to immediate execution with no form, J will terminate.

ARGV

The command line is stored in global ARGV_z_. For example, suppose that myscript.ijs is defined as:

(,(":ARGV),.10{a.) 1!:2 <'test.txt'
2!:55''

The running this in a command prompt with additional parameters:

./jconsole -jprofile myscript.ijs 123 'hello'

will create file test.txt containing:

+----------+---------+------------+---+-----+
|./jconsole|-jprofile|myscript.ijs|123|hello|
+----------+---------+------------+---+-----+

Application Errors

A common problem is that an application works fine in the development system, but fails when run by itself. Likely causes of this problem are:

  • the application uses directory names that are incorrect for the actual directory being used. In most cases, this can be fixed by either using relative pathnames, or with a configuration file that has the correct directory names.
  • the application requires some feature that is loaded in your startup script, but has not been included in the build. For example, if your application uses the dates script, and your start up script loads dates, but the application build does not include the dates script.
  • the application requires some feature that is in the development system. In this case, the application should be changed to avoid use of such a feature. Typically, development code is not required for standalone applications.

Debugging

Debugging an application that does not have the development environment can be tricky, since there is no J environment to work in. If there is an error in an application run with the -jprofile parameter, then J terminates with an error message: Error and no IDE window for debugging.

To debug an application:

First, ensure that your application works correctly in the normal J development environment, i.e. without any customizations you may have applied. It can be helpful to install a fresh copy of J elsewhere to test this.

Next, create an application directory and copy in all the files that are intended for distribution. In that directory, load J without the profile, i.e.

c:\myapp\j.exe -jprofile

This will bring up a barebones copy of J, with nothing defined. Note that you cannot simply run c:\myapp\j.exe by itself, since without the -jprofile parameter, J will attempt to load the profile - which does not exist in your application directory.

Next, try to load and run your application:

   0!:0 <'c:\myapp\myscript.ijs'

This has exactly the same behaviour as including myscript.ijs on the command line, except that there is now a very basic J session to enable you to see error messages at the point of failure - either as the script is being loaded, or later, as the application is being run. Use 0!:1 instead of 0!:0 to trace the script as it loads.

Usually, this technique suffices, but if not, try creating a log file, and writing messages to the log as the application proceeds.

minijx.ijs

When J is run with the -jprofile parameter, and no other script, it loads a barebones session manager with no definitions. The script system/extras/util/minijx.ijs has equivalent definitions. You can also build this basic J session into your application, for testing purposes.

See Also