Quantcast
Channel: Visual COBOL Knowledge Base
Viewing all articles
Browse latest Browse all 214

Visual COBOL and JAVA Integration

$
0
0
Current Revision posted to Visual COBOL Knowledge Base by Angela on 4/17/2013 5:19:03 AM

Problem

Unable to execute JAVA class or a COBOL JVM class on a UNIX machine.

 Resolution

This simple example demonstrates how well Visual COBOL and JAVA can integrate.

It covers the core subjects of CLASSPATH, PACKAGES, BUILDING JAR files and executing programs.

This will show you how to do this by using three programs on a test stub that invokes a JAVA program and a COBOL JVM program. This will also use some jar files created on a windows machine as well to show that the jar files are portable which is as they should be.

For all programs and scripts needed for this example, click the link and save as < jar-002.tar >

Setup build and run example

mkdir test
cd test
# mv jar into test or point to were you downloaded the file.
tar -xvf jar-002.tar
cd jar
ls
setup.sh  unixbld  unixjar  winjar

You will need to change setup.sh to point to your java version and your Visual COBOL version.

setup.sh

#
# set up java version
#
#JAVA_HOME=/usr/java/jdk1.7.0_03
#export JAVA_HOME
#PATH=$JAVA_HOME/bin:$PATH:
#export PATH
#java -version
#
. /home/products/vceclipse21/bin/cobsetenv
#
# make sure doing stuff in jvm 32 or 64 bit mode.
COBMODE=32
export COBMODE
#
# set original_classpath use all .jar file in current directory
#
ORIGINAL_CLASSPATH=$CLASSPATH
export ORIGINAL_CLASSPATH
echo $ORIGINAL_CLASSPATH
#
# set up env variable for this directory to use in classpath in runit scripts
#
ORIGINAL_ROOTDIR=$PWD
export ORIGINAL_ROOTDIR
echo $ORIGINAL_ROOTDIR
#

Execute the setup.sh script

. ./setup.sh
COBDIR set to /home/products/vceclipse21
/home/products/vceclipse21/lib/mfcobol.jar:/home/products/vceclipse21/lib/mfcobolrts.jar:/home/products/vceclipse21/lib/mfsqljvm.jar:
/home/tonyt/test/jar

The programs build and execution scripts are in unixbld

cd unixbld
ls *.java *.cbl *.sh
buildit.sh  JList.java  Program1.cbl  runitjava.sh  runit.sh  VCList.cbl

Here is how the windows jar files used in this test where built. This was done in a eclipse work space the directories follow the package name

winjar/makejarfiles.bat

cd C:\Users\tonyt\rhtony01shr\pcworkspace20\jclass\bin
jar -cvfe C:\Users\tonyt\bin\JList.jar com.microfocus.jexamples.list.JList com\microfocus\jexamples\list\JList.class
cd C:\Users\tonyt\rhtony01shr\pcworkspace20\vcclass\bin
jar -cvfe C:\Users\tonyt\bin\VCList.jar com.microfocus.vcexamples.list.VCList com\microfocus\vcexamples\list\VCList.class
cd C:\Users\tonyt\bin
copy C:\Users\tonyt\rhtony01shr\pcworkspace20\testem\bin\Program1.class Program1.class
copy C:\Users\tonyt\rhtony01shr\pcworkspace20\testem\src\Program1.cbl Program1.cbl
copy C:\Users\tonyt\rhtony01shr\pcworkspace20\jclass\src\com\microfocus\jexamples\list\JList.java JList.java
copy C:\Users\tonyt\rhtony01shr\pcworkspace20\vcclass\src\com\microfocus\vcexamples\list\VCList.cbl VCList.cbl

buildit.sh

# build simple program to execute class files in .jar's
# test standalone
# test using window jar files
#
# set back to mfcobol class path see ../setup.sh
CLASSPATH=$ORIGINAL_CLASSPATH
export CLASSPATH
echo $CLASSPATH
#
javac JList.java -d .
cob -j -C list="VCList.lis" VCList.cbl
cob -j -C list="Program1.lis" Program1.cbl
#
# create the unix jar files
#
jar -cvfe ../unixjar/VCList.jar com.microfocus.vcexamples.list.VCList com/microfocus/vcexamples/list/VCList.class
jar -cvfe ../unixjar/JList.jar com.microfocus.jexamples.list.JList com/microfocus/jexamples/list/JList.class

Execute the buildit.sh script

. ./buildit.sh
/home/products/vceclipse21/lib/mfcobol.jar:/home/products/vceclipse21/lib/mfcobolrts.jar:/home/products/vceclipse21/lib/mfsqljvm.jar:
added manifest
adding: com/microfocus/vcexamples/list/VCList.class(in = 2869) (out= 1472)(deflated 48%)
added manifest
adding: com/microfocus/jexamples/list/JList.class(in = 1988) (out= 1034)(deflated 47%)

Notes on what buildit.sh has done. You will see two .jar files have been created. Most importanly a directory called com has been created. This is the start of the underlying package directory structure, this is where the class files are put by java compilation and Visual COBOL compilation.

ls -R com ../unixjar/*.jar
../unixjar/JList.jar  ../unixjar/VCList.jar

com:
microfocus

com/microfocus:
cobol  jexamples  vcexamples

com/microfocus/cobol:
references

com/microfocus/cobol/references:
java

com/microfocus/cobol/references/java:
util

com/microfocus/cobol/references/java/util:
ListRef.class

com/microfocus/jexamples:
list

com/microfocus/jexamples/list:
JList.class

com/microfocus/vcexamples:
list

com/microfocus/vcexamples/list:
VCList.class

We have two scripts for running this test one uses java to run the programs and the other use cobjrun to run the programs, both work for this test. This shows you, For purejvm cobol apps, there are only a few .jars needed from the cobol server product. Pure jvm apps do not check licensing. For .jars needed see the CLASSPATH in this example.

runit.sh

# 
# run the simple VC COBOL program class file
# releativly accessining the invoked classes using the package directory structure.
# com/microfocus/jexamples/list
# com/microfocus/vcexample/list
#
# set back to mfcobol class path see ../setup.sh
CLASSPATH=$ORIGINAL_CLASSPATH
export CLASSPATH
echo $CLASSPATH
#
echo "test 1 using no jar files"
cobjrun Program1
#
# Run using the window jar files
#
CLASSPATH=$ORIGINAL_ROOTDIR/winjar/VCList.jar:$ORIGINAL_ROOTDIR/winjar/JList.jar:$ORIGINAL_CLASSPATH
export CLASSPATH
echo $CLASSPATH
#
# just for test remove the com package diretory
#
echo "remove the built packages so we know must use jar files."
rm -r com
#
# now we know it has to pick up the jar files
#
echo "test 2 using the windows built jar files see winjar/makejarfiles.bat"
cobjrun Program1
#
# test accessing the main method
#
echo "test3 execute the main method of a java jar file"
cobjrun com/microfocus/jexamples/list/JList
#
echo "test4 execute the main method of a VC jar file"
cobjrun com/microfocus/vcexamples/list/VCList
#
# Just do three test again should fail
# if jars not on CLASSPATH
# set back to mfcobol class path see ../setup.sh
CLASSPATH=$ORIGINAL_CLASSPATH
export CLASSPATH
echo $CLASSPATH
#
echo "test 5 should fail know jar on classpath"
cobjrun Program1
#
# test accessing the main method
#
echo "test6 should fail know jar on classpath"
cobjrun com/microfocus/jexamples/list/JList
#
echo "test7 should fail know jar on classpath"
cobjrun com/microfocus/vcexamples/list/VCList
#
# Run using the unix jar files
#
CLASSPATH=$ORIGINAL_ROOTDIR/unixjar/VCList.jar:$ORIGINAL_ROOTDIR/unixjar/JList.jar:$ORIGINAL_CLASSPATH
export CLASSPATH
echo $CLASSPATH
#
echo "test 8 using the unix built jar files see buildit.sh"
cobjrun Program1
#
# test accessing the main method
#
echo "test9 execute the main method of a java jar file"
cobjrun com/microfocus/jexamples/list/JList
#
echo "test10 execute the main method of a VC jar file"
cobjrun com/microfocus/vcexamples/list/VCList
#

runitjava.sh

# 
# run the simple VC COBOL program class file
# releativly accessining the invoked classes using the package directory structure.
# com/microfocus/jexamples/list
# com/microfocus/vcexample/list
#
# set back to mfcobol class path see ../setup.sh
CLASSPATH=$ORIGINAL_CLASSPATH
export CLASSPATH
echo $CLASSPATH
#
echo "test 1 using no jar files"
java Program1
#
# Run using the window jar files
#
CLASSPATH=$ORIGINAL_ROOTDIR/winjar/VCList.jar:$ORIGINAL_ROOTDIR/winjar/JList.jar:$ORIGINAL_CLASSPATH
export CLASSPATH
echo $CLASSPATH
#
# just for test remove the com package diretory
#
echo "remove the built packages so we know must use jar files."
rm -r com
#
# now we know it has to pick up the jar files
#
echo "test 2 using the windows built jar files see winjar/makejarfiles.bat"
java Program1
#
# test accessing the main method
#
echo "test3 execute the main method of a java jar file"
java com/microfocus/jexamples/list/JList
#
echo "test4 execute the main method of a VC jar file"
java com/microfocus/vcexamples/list/VCList
#
# Just do three test again should fail
# if jars not on CLASSPATH
# set back to mfcobol class path see ../setup.sh
CLASSPATH=$ORIGINAL_CLASSPATH
export CLASSPATH
echo $CLASSPATH
#
echo "test 5 should fail know jar on classpath"
java Program1
#
# test accessing the main method
#
echo "test6 should fail know jar on classpath"
java com/microfocus/jexamples/list/JList
#
echo "test7 should fail know jar on classpath"
java com/microfocus/vcexamples/list/VCList
#
# Run using the unix jar files
#
CLASSPATH=$ORIGINAL_ROOTDIR/unixjar/VCList.jar:$ORIGINAL_ROOTDIR/unixjar/JList.jar:$ORIGINAL_CLASSPATH
export CLASSPATH
echo $CLASSPATH
#
echo "test 8 using the unix built jar files see buildit.sh"
java Program1
#
# test accessing the main method
#
echo "test9 execute the main method of a java jar file"
java com/microfocus/jexamples/list/JList
#
echo "test10 execute the main method of a VC jar file"
java com/microfocus/vcexamples/list/VCList
#

Execute the runitit.sh script

. ./runit.sh
/home/products/vceclipse21/lib/mfcobol.jar:/home/products/vceclipse21/lib/mfcobolrts.jar:/home/products/vceclipse21/lib/mfsqljvm.jar:
test 1 using no jar files
Program1 start :::
Java the JList at start :
using List::tostring : [one, Three, two, four]
using JList::tostring : 
<one>
<Three>
<two>
<four>

Java the JList after sort :
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

Visual Cobol the VCList at start :
using List::tostring : [one, Three, two, four]
using VCList::tostring : 
<one>
<Three>
<two>
<four>

Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

back in Program1 lets play with objects
Create instance and access its public data use set.
Access a static method to sort 
Visual Cobol the JList after sort : 
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

Create instance and access its public data use declare and create.
Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

/home/tonyt/test/jar/winjar/VCList.jar:/home/tonyt/test/jar/winjar/JList.jar:/home/products/vceclipse21/lib/mfcobol.jar:/home/products/vceclipse21/lib/mfcobolrts.jar:/home/products/vceclipse21/lib/mfsqljvm.jar:
remove the built packages so we know must use jar files.
test 2 using the windows built jar files see winjar/makejarfiles.bat
Program1 start :::
Java the JList at start :
using List::tostring : [one, Three, two, four]
using JList::tostring : 
<one>
<Three>
<two>
<four>

Java the JList after sort :
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

Visual Cobol the VCList at start :
using List::tostring : [one, Three, two, four]
using VCList::tostring : 
<one>
<Three>
<two>
<four>

Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

back in Program1 lets play with objects
Create instance and access its public data use set.
Access a static method to sort 
Visual Cobol the JList after sort : 
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

Create instance and access its public data use declare and create.
Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

test3 execute the main method of a java jar file
Java the JList at start :
using List::tostring : [one, Three, two, four]
using JList::tostring : 
<one>
<Three>
<two>
<four>

Java the JList after sort :
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

test4 execute the main method of a VC jar file
Visual Cobol the VCList at start :
using List::tostring : [one, Three, two, four]
using VCList::tostring : 
<one>
<Three>
<two>
<four>

Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

/home/products/vceclipse21/lib/mfcobol.jar:/home/products/vceclipse21/lib/mfcobolrts.jar:/home/products/vceclipse21/lib/mfsqljvm.jar:
test 5 should fail know jar on classpath
Program1 start :::
/home/products/vceclipse21/bin/cobjrun32: Program1.main ended due to an exception
Exception in thread "main" java.lang.NoClassDefFoundError: com/microfocus/jexamples/list/JList
	at Program1.Program1(Program1.cbl:19)
	at Program1.main(Program1.cbl)
Caused by: java.lang.ClassNotFoundException: com.microfocus.jexamples.list.JList
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
	... 2 more
test6 should fail know jar on classpath
/home/products/vceclipse21/bin/cobjrun32: Could not find com/microfocus/jexamples/list/JList
Exception in thread "main" java.lang.NoClassDefFoundError: com/microfocus/jexamples/list/JList
Caused by: java.lang.ClassNotFoundException: com.microfocus.jexamples.list.JList
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
test7 should fail know jar on classpath
/home/products/vceclipse21/bin/cobjrun32: Could not find com/microfocus/vcexamples/list/VCList
Exception in thread "main" java.lang.NoClassDefFoundError: com/microfocus/vcexamples/list/VCList
Caused by: java.lang.ClassNotFoundException: com.microfocus.vcexamples.list.VCList
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
/home/tonyt/test/jar/unixjar/VCList.jar:/home/tonyt/test/jar/unixjar/JList.jar:/home/products/vceclipse21/lib/mfcobol.jar:/home/products/vceclipse21/lib/mfcobolrts.jar:/home/products/vceclipse21/lib/mfsqljvm.jar:
test 8 using the unix built jar files see buildit.sh
Program1 start :::
Java the JList at start :
using List::tostring : [one, Three, two, four]
using JList::tostring : 
<one>
<Three>
<two>
<four>

Java the JList after sort :
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

Visual Cobol the VCList at start :
using List::tostring : [one, Three, two, four]
using VCList::tostring : 
<one>
<Three>
<two>
<four>

Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

back in Program1 lets play with objects
Create instance and access its public data use set.
Access a static method to sort 
Visual Cobol the JList after sort : 
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

Create instance and access its public data use declare and create.
Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

test9 execute the main method of a java jar file
Java the JList at start :
using List::tostring : [one, Three, two, four]
using JList::tostring : 
<one>
<Three>
<two>
<four>

Java the JList after sort :
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

test10 execute the main method of a VC jar file
Visual Cobol the VCList at start :
using List::tostring : [one, Three, two, four]
using VCList::tostring : 
<one>
<Three>
<two>
<four>

Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

This test is destructive so execute the build script again

Execute the buildit.sh script

. ./buildit.sh
/home/products/vceclipse21/lib/mfcobol.jar:/home/products/vceclipse21/lib/mfcobolrts.jar:/home/products/vceclipse21/lib/mfsqljvm.jar:
added manifest
adding: com/microfocus/vcexamples/list/VCList.class(in = 2869) (out= 1472)(deflated 48%)
added manifest
adding: com/microfocus/jexamples/list/JList.class(in = 1988) (out= 1034)(deflated 47%)

Execute the runititjava.sh script

. ./runitjava.sh
/home/products/vceclipse21/lib/mfcobol.jar:/home/products/vceclipse21/lib/mfcobolrts.jar:/home/products/vceclipse21/lib/mfsqljvm.jar:
test 1 using no jar files
Program1 start :::
Java the JList at start :
using List::tostring : [one, Three, two, four]
using JList::tostring : 
<one>
<Three>
<two>
<four>

Java the JList after sort :
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

Visual Cobol the VCList at start :
using List::tostring : [one, Three, two, four]
using VCList::tostring : 
<one>
<Three>
<two>
<four>

Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

back in Program1 lets play with objects
Create instance and access its public data use set.
Access a static method to sort 
Visual Cobol the JList after sort : 
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

Create instance and access its public data use declare and create.
Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

/home/tonyt/test/jar/winjar/VCList.jar:/home/tonyt/test/jar/winjar/JList.jar:/home/products/vceclipse21/lib/mfcobol.jar:/home/products/vceclipse21/lib/mfcobolrts.jar:/home/products/vceclipse21/lib/mfsqljvm.jar:
remove the built packages so we know must use jar files.
test 2 using the windows built jar files see winjar/makejarfiles.bat
Program1 start :::
Java the JList at start :
using List::tostring : [one, Three, two, four]
using JList::tostring : 
<one>
<Three>
<two>
<four>

Java the JList after sort :
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

Visual Cobol the VCList at start :
using List::tostring : [one, Three, two, four]
using VCList::tostring : 
<one>
<Three>
<two>
<four>

Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

back in Program1 lets play with objects
Create instance and access its public data use set.
Access a static method to sort 
Visual Cobol the JList after sort : 
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

Create instance and access its public data use declare and create.
Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

test3 execute the main method of a java jar file
Java the JList at start :
using List::tostring : [one, Three, two, four]
using JList::tostring : 
<one>
<Three>
<two>
<four>

Java the JList after sort :
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

test4 execute the main method of a VC jar file
Visual Cobol the VCList at start :
using List::tostring : [one, Three, two, four]
using VCList::tostring : 
<one>
<Three>
<two>
<four>

Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

/home/products/vceclipse21/lib/mfcobol.jar:/home/products/vceclipse21/lib/mfcobolrts.jar:/home/products/vceclipse21/lib/mfsqljvm.jar:
test 5 should fail know jar on classpath
Program1 start :::
java.lang.NoClassDefFoundError: com/microfocus/jexamples/list/JList
	at Program1.Program1(Program1.cbl:19)
	at Program1.main(Program1.cbl)
Caused by: java.lang.ClassNotFoundException: com.microfocus.jexamples.list.JList
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
	... 2 more
test6 should fail know jar on classpath
Error: Could not find or load main class com.microfocus.jexamples.list.JList
test7 should fail know jar on classpath
Error: Could not find or load main class com.microfocus.vcexamples.list.VCList
/home/tonyt/test/jar/unixjar/VCList.jar:/home/tonyt/test/jar/unixjar/JList.jar:/home/products/vceclipse21/lib/mfcobol.jar:/home/products/vceclipse21/lib/mfcobolrts.jar:/home/products/vceclipse21/lib/mfsqljvm.jar:
test 8 using the unix built jar files see buildit.sh
Program1 start :::
Java the JList at start :
using List::tostring : [one, Three, two, four]
using JList::tostring : 
<one>
<Three>
<two>
<four>

Java the JList after sort :
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

Visual Cobol the VCList at start :
using List::tostring : [one, Three, two, four]
using VCList::tostring : 
<one>
<Three>
<two>
<four>

Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

back in Program1 lets play with objects
Create instance and access its public data use set.
Access a static method to sort 
Visual Cobol the JList after sort : 
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

Create instance and access its public data use declare and create.
Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

test9 execute the main method of a java jar file
Java the JList at start :
using List::tostring : [one, Three, two, four]
using JList::tostring : 
<one>
<Three>
<two>
<four>

Java the JList after sort :
using List::tostring : [four, one, Three, two]
using JList::tostring : 
<four>
<one>
<Three>
<two>

test10 execute the main method of a VC jar file
Visual Cobol the VCList at start :
using List::tostring : [one, Three, two, four]
using VCList::tostring : 
<one>
<Three>
<two>
<four>

Visual Cobol the VCList after sort :
using List::tostring : [four, one, Three, two]
using VCList::tostring : 
<four>
<one>
<Three>
<two>

Source code

Program1.cbl

      $set sourceformat(variable)      
      $set ooctrl(+p-f) intlevel"4"
      $set ilusing"java.util"
      $set ilusing"java.lang"
      $set ilusing"com.microfocus.vcexamples.list"
      $set ilusing"com.microfocus.jexamples.list"

       program-id. Program1 as "Program1".

       data division.
       working-storage section.
       
       01 tstJList type JList.
       01 result type void.
       01 args type String[].
       
       procedure division.
           Display "Program1 start :::"
           invoke type JList::main(args)
           invoke type VCList::main(args)
           
           display "back in Program1 lets play with objects"
           Display "Create instance and access its public data use set."
           set tstJList to new JList()
           display "Access a static method to sort "
           invoke type Collections::sort(tstJList::ls, String::CASE_INSENSITIVE_ORDER)
           display "Visual Cobol the JList after sort : "
           display "using List::tostring : ",  tstJList::ls
           display "using JList::tostring : "
           display tstJList

           Display "Create instance and access its public data use declare and create."
           declare tstVCList as type VCList
           create tstVCList
           invoke type Collections::sort(tstVCList::ls, String::CASE_INSENSITIVE_ORDER)  
           display "Visual Cobol the VCList after sort :"
           display "using List::tostring : ",  tstVCList::ls
           display "using VCList::tostring : "
           display tstVCList

           goback.

       end program Program1.

VCList.cbl

      $set sourceformat(variable)      
      $set ooctrl(+p-f) intlevel"4"
      $set ilusing"java.util"
      $set ilusing"java.lang"
      $set ilusing"com.microfocus.vcexamples.list"
        
       class-id com.microfocus.vcexamples.list.VCList public.
       
       01 ls type List public.
       
       *> no argument constructor
       method-id new public.
       procedure division.
           set ls to new ArrayList[String]
           invoke ls::add("one")
           invoke ls::add("Three")
           invoke ls::add("two")
           invoke ls::add("four")
       end method.

       *> constructor passing a List
       method-id new public.
       procedure division using by reference ls as type List.
           set self::ls to new ArrayList(ls);
       end method.
    	*> just exercise class for getting lists about
       method-id toString override.
       01 sb type StringBuffer.
       01 val string.
       
       procedure division returning lnkmessage as string.
            set sb to new StringBuffer
            perform varying val through ls
                invoke sb::append("<")
                invoke sb::append(val)
                invoke sb::append(">")
                invoke sb::append(x'0d0a') *> dont handle \n
            end-perform   
            set lnkmessage to sb::toString()
            
       end method.
		
       
      *>
      *> main entry point
      *>
       method-id main public static.
       local-storage section.
       linkage section.
       01 args type String occurs any.
       
       procedure division using by value args.
           *> Create the VCList
           *> use the declare and create syntax
           declare tstVCList as type VCList
           create tstVCList
           
           display "Visual Cobol the VCList at start :"
           display "using List::tostring : ",  tstVCList::ls
           display "using VCList::tostring : "
           display tstVCList
           *> Fire of a core java static method to sort 
           invoke type Collections::sort(tstVCList::ls, String::CASE_INSENSITIVE_ORDER)  
           display "Visual Cobol the VCList after sort :"
           display "using List::tostring : ",  tstVCList::ls
           display "using VCList::tostring : "
           display tstVCList
           

       end method.
       end class.

JList.java

package com.microfocus.jexamples.list;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class JList {
	// just exercise class for getting lists about
	
	public List<String> ls;
    
	public JList() {
		// nothing given so lets create one
		ls = new ArrayList<String>();
		
		ls.add("one");
		ls.add("Three");
		ls.add("two");
		ls.add("four");

	}
	
	public JList(List<String> ls) {
		this.ls = new ArrayList<String>(ls);
	}

	// just exercise class for getting lists about
	public String toString() {
		
		StringBuffer sb = new StringBuffer();
		Iterator<String> it = ls.iterator();

		while (it.hasNext()) {
			sb.append("<" + it.next() + ">\n");
		}
		return sb.toString();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// create the JList
		JList tstJList = new JList();
		System.out.println("Java the JList at start :");
		System.out.println("using List::tostring : " + tstJList.ls);
		System.out.println("using JList::tostring : \n" + tstJList);
		// Fire of a core java static method to sort
		Collections.sort(tstJList.ls, String.CASE_INSENSITIVE_ORDER);
		System.out.println("Java the JList after sort :");
		System.out.println("using List::tostring : " + tstJList.ls);
		System.out.println("using JList::tostring : \n" + tstJList);
		        		
	}

}

Main points

The main point is to understand packaging. This demo shows you the syntax required to achieve this and also how to build and deploy and execute your JVM code.

From this understanding you should be able to implement this example in Visual COBOL for eclipse using three projects 2 COBOL JVM projects for Program1 and VCList and JAVA project for JList.

Tags: JARFILE

Viewing all articles
Browse latest Browse all 214

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>