Importing a Java Class

 

To use a Java class within a 4GL program, you must import it. To import a java class the IMPORT JAVA statement is used. For a successful import the path to the Java class referenced should be specified either in the CLASSPATH environment variable or in the --java-option passed to qfgl, unless it is a standard Java class.

 

The syntax of the IMPORT JAVA is as follows:

 

IMPORT JAVA <ClassName>

 

For example to import the java.io.File class you should write:

 

IMPORT JAVA java.io.File

 

Class names must be fully qualified when used in the IMPORT JAVA statement. It can be unqualified when referred to later in the program.

 

 

 

When referring to this class later in the program (e.g. in a variable declaration), either the fully qualified (e.g. java.io.File) or unqualified (e.g. File) variation may be used. The only exception to this rule is when the unqualified class name conflicts with a previously defined type, in which case, the fully qualified class name must be used throughout the program.

 

For each newly imported Java class you should use a separate IMPORT JAVA statement.

 

IMPORT JAVA statement together with the FUNCTION, MAIN, REPORT, and GLOBALS statements can be used only outside any block (i.e. outside the MAIN block or outside the FUNCTION block). Thus, for example, you can use it before the MAIN block like this to be able to reference the class throughout the module - it will have the module scope of reference:

 

IMPORT JAVA java.util.regex.Pattern

MAIN

...

END MAIN

 

To import a Java class with the global scope of reference, you can add the IMPORT JAVA statement to the GLOBALS file. However, you cannot include this statement inside the GLOBALS statement - you should place it before the GLOBALS statement (e.g. like it is done for the DATABASE statement):

 

IMPORT JAVA java.io.File

GLOBALS

...

END GLOBALS

 

A Java class imported in a GLOBALS file can be referenced by any source file to which the GLOBALS file is linked.