🛠️ P3/COBOL 25.1.0-SNAPSHOT is available for preview.
Tutorials
Sample Programs

Sample Programs

P3/COBOL includes a variety of sample program projects in the Eclipse IDE distribution. To create a project with one of them, just use the New P3/COBOL Project context menu selection, and pick the sample that you are interested in working with. Most of the work (in many cases all) will be performed automatically to create the project. See the following for specific notes regarding each of the samples.

COBOL

hello-world

The purpose of this sample is to provide a very simple program to use as verification of correct installation and as an introduction to setting up and using the P3/COBOL command-line environment to compile and run a P3/COBOL application. Just select the sample in the New Project Wizard, and it is ready to run.

calculator

This sample is a simple program that comprises a calculator service used by the gui sample project and the simple-servlet sample web application project. It also shows how incoming arguments can be tested and, if omitted, allocated (based) and set to a default value. This allows the program to always run without error, regardless of the arguments it is supplied. This program may be run stand-alone with command line arguments, or it may serve as an example service used by other programs and projects.

In addition, it shows how JAS can be used to provide unit test entry points in the COBOL source code that can be executed by the JUnit4 system.

ℹ️

All requirements are configured in the demo project by default, but be sure and add the JUnit4 library to the project and add the generated Java source to the project's Java build path via the project Preferences. After doing so, you can launch JUnit4 and it will run the unit tests in the sample.

sql

This sample illustrates the use of the P3/COBOL built-in Embedded SQL capability (ESQL). It is using two different variants to create the database connection:

  1. The default connection is using the jdbc configuration from runtime-default.p3c.
  2. The java like connection is using the jdbc configuration from the H2DatabaseConnection.class which is configured to use when running with runtime-override.p3c.

To simplify the execution two preconfigured Run Configurations are included in the project.

💡

This sample uses the H2 relational database by default. The jar of H2 version 1.4.197 is automatically included in the project and added to the Java build path. If you prefer an newer version, feel free to update the jar.

xml

P3/COBOL includes a powerful, general purpose XML-based IMPORT and EXPORT capability. IMPORT and EXPORT are particularly well-suited to the "pipeline" model of XML processing, whereby XML transformations take place via XSLT transforms during the import or export of data. Since XSLT can produce not only XML, but arbitrary text as well, the EXPORT statement can be used to export COBOL data into any text-based form, including XML. Any well-formed XML may be imported and adapted by XSLT to match any COBOL data structures internally.

COBOL and Java

hello-universe

The purpose of this sample is to provide a very simple project, to show how P3/COBOL can be called from Java. Just select the sample in the New Project Wizard, and it is ready to run.

printing

P3/COBOL includes a powerfull and unique feature called: Embedded Java!

This printing sample shows, how Java classes/libraries can be used withing P3/COBOL use the advantages of both worlds: COBOL and Java.

gui

This sample provides four examples of a Java GUI interface interacting with a COBOL application using the "COBOL as master" model. In this model, the COBOL application is responsible for spawning the GUI thread(s) and regaining control when the Java-based GUI has completed a task (e.g., presenting a dialog and obtaining a field value). It also illustrates how multiple threads can safely interact at the COBOL data item level, thus allowing the GUI thread to directly set the COBOL values, rather than requiring the COBOL call into the GUI classes to retrieve values to be set in COBOL, as is the case with most COBOL GUI solutions.

mixed-language

This sample project illustrates the mechanics of calling in and out of COBOL from non-COBOL (Java) programs. The sample is started by launching the Pojo_that_mimics_cobol main method as a Java application, and it then calls a COBOL program that in turn calls a Java class method. Finally, a different and much simpler technique using the Launcher utility class is used.

JAS

sieve-of-eratosthenes

This sample project illustrates the use of P3/COBOL's Java Augmented Syntax (JAS). Note that the structure of the program is defined by the use of enclosing brackets (similar to C, C++, and Java), and the traditional COBOL statements often use a modern alternative that is more accessible to programmers not familiar with the classical COBOL idiom. Just select the sample in the New Project Wizard, and it is ready to run.