Extending IMLPlus Using Java

You can write your own Java classes and call them from an IMLPlus program. The process to do this is straightforward. Assuming you have previously compiled your Java source files, all you need to do is to tell IML Studio where to find the .class files. There are two ways to do this.

Method 1: Put the .class files in IML Studio's user classes directory

Copy the .class files to the following directory:

<My IML Studio Files>\Classes

where <My IML Studio Files> is usually the following directory:

Windows 7 C:\Users\userid\Documents\My IML Studio Files
Windows 8 C:\Users\userid\Documents\My IML Studio Files
Windows 10 C:\Users\userid\Documents\My IML Studio Files

Method 2: Extend the directory search list for classes

IML Studio does not use the CLASSPATH environment variable that some Java virtual machines use to define the locations of classes. Instead, IML Studio provides a graphical directory search list editor. Follow these steps to add a new directory to IML Studio's directory search list for classes:

  1. On the Tools menu, click Options.

    IML Studio will display the Options dialog box.

  2. Click the Directories tab.
  3. In the Show directories for box, select Classes.
  4. Click Add.

    IML Studio will display a popup menu.

  5. On the popup menu, click Directory.

    IML Studio will display the Browse for Folder dialog box.

  6. Navigate to, and select, the directory containing the .class files.
  7. Click OK to close the Browse for Folder dialog box.
  8. Click OK to close the Options dialog box.
  9. Exit IML Studio and then restart it.

    This final step is necessary so that IML Studio can reinitialize the Java virtual machine with the revised directory search list.

Example

This example illustrates the use of a user-defined Java class in an IMLPlus program.

The source code for a Java class named Point2D appears below:

public class Point2D
{
double m_x, m_y;

public Point2D() {}

public Point2D( double x0, double y0 )
{
    m_x = x0;
    m_y = y0;
}

public double GetX() { return m_x; }

public double GetY() { return m_y; }

public void Move( double dx, double dy )
{
    m_x += dx;
    m_y += dy;
}

public Point2D Add( Point2D z )
{
    return new Point2D( m_x + z.GetX(), m_y + z.GetY() );
}

}

Copy and paste this source code into a Java source file named Point2D.java. Then, using your Java development tools, compile the Point2D.java file and copy the Point2D.class file to a directory that IML Studio searches for .class files.

You can now use the Point2D class in an IMLPlus program. As an example, the IMLPlus program

declare Point2D p = new Point2D( 1, 2 );
declare Point2D q = new Point2D( 2, -1 );
declare Point2D r = p.Add( q );

rx = r.GetX();
ry = r.GetY();

print rx ry;

q.Move( 2, 2 );

qx = q.GetX();
qy = q.GetY();

print qx qy;

produces the following output:

RX        RY

 3         1


QX        QY

 4         1