Skip to main content
Question

Native Visual COBOL calling Java classes

  • February 16, 2026
  • 2 replies
  • 7 views

Eric Hardesty

I am trying to call non-static Java methods from an executable file since static methods work fine in this environment.  I can accomplish this using cobjrun but it seems like the $java$ is not resolved correctly without cobjrun.

I have tried adding all mf*.jar from the VisualCobol/lib directory into my CLASSPATH before execution but always get a load error similar to the following:

Load error : file '$java$JavaCalculator'
error code: 198, pc=0, call=1, seg=0
198     Load failure
 

Here are the files that show the failure:

natcobol.cbl

       $set ooctrl(+p-f)
       program-id. natcobol.

       class-control.
          claJavacalled is class "$java$JavaCalculator".
      * The above maps the Java class 'JavaCalculator' to the COBOL class name 'claJavacalled'

       working-storage section.
        copy "javatypes.cpy".
           01 hCla            object reference.
           01 l1              jlong value 10.
           01 l2              jlong value 5.
           01 retl            jlong.
           01 i1              binary-long value 10.
           01 i2              binary-long value 5.
           01 ret             binary-long.

       procedure division.
      *    display "To Static Java , i1 i2: " i1 "/" i2
      *    call "java.JavaCalculator.print" using by reference i1 i2.
      * Create an instance of the Java class
           invoke claJavacalled "new" returning hCla.
           display "To Java , i1 i2: " i1 "/" i2

      * Invoke the "add" method using the object instance
           invoke hCla "add" using by reference i1 i2 returning ret.
           display "From Java(int) , ret: " ret

           invoke hCla "add" using by reference l1 l2 returning retl.
           display "From Java(long) , ret: " retl
      * Release the object reference
           invoke hCla "finalize" returning hCla.

           goback.
       end program natcobol.
***************************************************************************

JavaCalculator.java

// JavaCalculator.java
public class JavaCalculator {

    // Constructor (the COBOL "new" invokes this)
    public JavaCalculator() {
        System.out.println("JavaCalculator instance created.");
    }

    // The method that COBOL calls
    public long add(long a, long b) {
        return a + b;
    }

    public int add(int a, int b) {
        return a + b;
    }

    public static void print(int a, int b) {
        System.out.println("Printing a+b=" + (a+b));
    }

    // Optional finalize method
    public void finalize() {
        System.out.println("JavaCalculator instance finalized.");
    }
}

***************************************************************************

makefile

CLASSPATH := ./classes:$(CLASSPATH)
export CLASSPATH

all: compile run

compile:
        javac -d classes JavaCalculator.java
        cob -x natcobol.cbl

run: compile
        cobjrun natcobol

runexe: compile
        ./natcobol

clean:
        rm -f classes/*.class *.o *.idy *.int *.class natcobol
 

***************************************************************************

Output:

make run
javac -d classes JavaCalculator.java
Note: JavaCalculator.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
cob -i natcobol.cbl
cobjrun natcobol
JavaCalculator instance created.
To Java , i1 i2: +0000000010/+0000000005
From Java(int) , ret: +0000000015
From Java(long) , ret: +00000000000000000015

make runexe
javac -d classes JavaCalculator.java
Note: JavaCalculator.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
cob -x natcobol.cbl
./natcobol

Load error : file '$java$JavaCalculator'
error code: 198, pc=0, call=1, seg=0
198     Load failure

make: *** [makefile:18: runexe] Error 255
 

Any pointers would be helpful since I have not seen anything documented that states cobjrun must be used.

Eric

2 replies

Blair McDonald
Forum|alt.badge.img+1

Hi Eric,

cobjrun is the recommended way to invoke the runtime in this situation. Please see the following page from the Visual COBOL product documentation: Java/COBOL Application Launcher (that page is for 11.0, but the same advice is offered in earlier versions.) One of the listed scenarios is: “Native COBOL applications calling Java”

Hope this helps,

Blair


Eric Hardesty
  • Author
  • New Participant
  • February 17, 2026

Thanks I missed that link since it was not listed as a requirement in the code examples.  I guess I will have to limit usage to only static calls since my complete environment is a little more complex and cannot use cobjrun.

I have C socket server that forks itself and then loads libraries of COBOL routines and “calls” them as needed.  I have been able to create the CLASSPATH of JAVA jar files before starting the server so I can allow Java calls from the COBOL routines but only static calls will work as pointed out without cobjrun.

I was hoping for some way to link and/or include jars to do the job but I will just have to stipulate the limitation to my customer.

Eric