OScript API/Built-in Package Index

Class: JavaObject

The built-in functions in the JavaObject package provide the ability to access Java classes and services from OScript. The major functionalities offered in the JavaObject package are the following:

  • New() Creates a new instance of a Java class.
  • InvokeStaticMethod() Invokes a static method defined for a Java class.
  • GetStaticField() Returns the value of a static field.
  • SetStaticField() Sets the value of a static field.
  • GetError() Returns the JNI exception error message from the last operation.
  • GetErrorStack() Returns a List for the stack trace associated with an exception in the last JNI operation.
  • InvokeMethod() Invokes an instance method defined for a Java class.
  • GetField() Returns the value of an instance field.
  • SetField() Sets the value of an instance field.

    Note:

  • To deploy your jar files in Livelink JVM, added the jar files in OTHOME/ojlib/ or OTHOME/module/somemodule_1_0_0/ojlib/ directory (and their subdirectories) to be recognized by the JVM's application classloader.

  • Since Livelink 10.0.0, Java methods invoked by InvokeMethod and InvokeStaticMethod can access OScript features. Please see the "Calling OScript from Java" section for details.

OScript and Java Type Mappings

OScript To Java Type Mappings (passing parameters to JavaObject methods)

OScript

Java

Assoc

java.util.Map

Boolean

boolean, Boolean

Integer

short, Short
int, Integer
long, Long



List, Record, RecArray

java.util.List

Long

short, Short
int, Integer
long, Long



Real

float, Float
double, Double


String

String

Undefined

null



Java to OScript Type Mappings (return values from JavaObject methods)

Java

OScript

boolean, Boolean

Boolean

byte
char, Character
short, Short
int, Integer




Integer

float, Float
double, Double


Real

long, Long

Long

java.util.List 1 List

java.util.Map 1

Assoc

String

String

null

Undefined

Java types not listed in this table

JavaObject



1. This conversion is not backward compatible with the earlier versions of Livelink which convert this type to JavaObject. To switch to the old behavior, you can set "ReturnAsCollectionType=false" in the [JavaObject] section of opentext.ini file. But we strongly recommend that you update your code to comply with the new conversion.

Class Methods

Returns the JNI exception error message from the last operation.

Returns a List for the stack trace associated with an exception in the last JNI operation.

GetStaticField( String className, String fieldName )

Returns the value of a static field.

InvokeStaticMethod( String className, String methodName, List parameters )

Invokes a static method defined for a Java class.

New( String className, List parameters )

Creates a new instance of a Java class.

SetStaticField( String className, String methodName, Dynamic value )

Sets the value of a static field.

Instance Methods

GetField( String fieldName )

Returns the value of an instance field.

InvokeMethod( String methodName, List parameters )

Invokes an instance method defined for a Java class.

SetField( String fieldName, Dynamic value )

Sets the value of an instance field.

Class Methods

GetError

String GetError()

Returns the JNI exception error message from the last operation.

Returns:

A string containing JNI exception error message from the last operation.

If no exception occurs, an empty string is returned.

Example

Here is an example:

    Dynamic result = JavaObject.SetStaticField( "com.opentext.util.DateFormat", "fDefaultMonthFormat", { "mm" } )
    if IsError( result )
        echo( JavaObject.GetError() )
    end

GetErrorStack

List GetErrorStack()

Returns a List for the stack trace associated with an exception in the last JNI operation.

Returns:

A [[List]] of [[String]] from the Java stack trace elements at the time of an exception. Each [[String]] represents a single stack frame. As described in the Java API specification documentation for Class StackTraceElement, all stack frames except for the one at the top of the stack represent a method invocation. The frame at the top of the stack represents the the execution point at which the stack trace was generated.

If no exception has occurred, an empty [[List]] is returned.

Example

Here is an example:

    List        errStack
    String      s
    JavaObject  fileStream = JavaObject.New( "java.io.FileInputStream", { 'c:\badfile' } )

    if IsError( fileStream )
        errStack = JavaObject.GetErrorStack()

        for s in errStack
            echo( s )
        end
    end

GetStaticField

Dynamic GetStaticField( String className,
                        String fieldName )

Returns the value of a static field.

Parameters

className

The name of the class that defines the field.

fieldName

The name of the field to get the value of.

Returns:

The return value can be either an Error object if it fails or a Dynamic if it succeeds.

Example

Here is an example:

Dynamic result = JavaObject.GetStaticField( "com.opentext.util.DateFormat", "fDefaultMonthFormat" )

InvokeStaticMethod

Dynamic InvokeStaticMethod( String className,
                            String methodName,
                            List parameters )

Invokes a static method defined for a Java class.

Parameters

className

The name of the class that defines the method.

methodName

The name of the method to invoke.

parameters

The parameter list to pass to the method.

Returns:

The return value can be either an Error object if it fails or a Dynamic if it succeeds.

Example

Here is an example:

Dynamic result = JavaObject.InvokeStaticMethod( "com.opentext.util.DateFormat", "GetDefaultMonthFormat", {} )

New

JavaObject New( String className,
                List parameters )

Creates a new instance of a Java class.

Parameters

className

The name of the class to instantiate.

parameters

The parameter list to pass to the constructor.

Returns:

The return value can be either an Error object if it fails or a JavaObject if it succeeds.

Example

Here is an example:

JavaObject dateFormat = JavaObject.New( "com.opentext.util.DateFormat", {} )

SetStaticField

Dynamic SetStaticField( String className,
                        String methodName,
                        Dynamic value )

Sets the value of a static field.

Parameters

className

The name of the class that defines the field.

methodName

The name of the field to set the value of.

value

The value to set the static field.

Returns:

The return value can be either an Error object if it fails or a Undefined if it succeeds.

Example

Here is an example:

Dynamic result = JavaObject.SetStaticField( "com.opentext.util.DateFormat", "fDefaultMonthFormat", { "mm" } )

Instance Methods

GetField

Dynamic GetField( String fieldName )

Returns the value of an instance field.

Parameters

fieldName

The name of the field to get the value of.

Returns:

The return value can be either an Error object if it fails or a Dynamic if it succeeds.

Example

Here is an example:

Dynamic result = dateFormat.GetField( "fMonthFormat" )

InvokeMethod

Dynamic InvokeMethod( String methodName,
                      List parameters )

Invokes an instance method defined for a Java class.

Parameters

methodName

The name of the method to invoke.

parameters

The parameter list to pass to the method.

Returns:

The return value can be either an Error object if it fails or a Dynamic if it succeeds.

Example

Here is an example:

Dynamic result = dateFormat.InvokeMethod( "GetMonthFormat", {} )

SetField

Dynamic SetField( String fieldName,
                  Dynamic value )

Sets the value of an instance field.

Parameters

fieldName

The name of the field to set the value of.

value

The value to set the field.

Returns:

The return value can be either an Error object if it fails or a Undefined if it succeeds.

Example

Here is an example:

Dynamic result = dateFormat.SetField( "fMonthFormat", { "mm" } )

 Copyright © 2022 OpenText Corporation. All rights reserved.