Problem solve Get help with specific problems with your technologies, process and projects.

Talk to SAP with JCo

To create a Java-based Web Service that relies on SAP data and functionality we need a mechanism for communicating with SAP.

To create a Java-based Web Service that relies on SAP data and functionality we will need a mechanism for communicating with SAP. That mechanism is JCO. This article is not a complete tutorial on JCO. More information about JCo is available at and downloading Unzip this file and view the file JCOTutorial.pdf in the docs directory.

In this article we will demonstrate how to use JCO to do the following:
1. Connect with SAP
2. Create a respository for BAPI metadata
3. Set BAPI input parameters
4. Execute the BAPI
5. Extract BAPI output parameters
6. Disconnect from SAP

Connect to SAP
The JCO.createClient() method uses SAP connection information to create a JCO.client object. Then the connect() attempts to login to R/3 using a specific userid and a password. If successful, the JCO.client object will represent an SAP connection.
JCO.Client client = JCO.createClient( look at docs for input parameters ); client.connect();

BAPI Repository and metadata
A BAPI Repository is a place to store BAPI metadata. This is information about the BAPI itself including all input, output, and in/out parameter names, data types, and lengths.
JCO.Repository repository = new JCO.Repository("Gamma", client);

We now ask the repository for metadata about a specific BAPI. Here we are working with the BAPI_MATERIAL_GETLIST. Please review SAP documentation for more information about this BAPI. This BAPI actually uses 10 different parameters. By asking the repository for BAPI metadata, we do not have to write the code to set up these 10 parameters. Also, we are returned a JCO.Function object that contains BAPI metadata.

JCO.Function function = repository.getFunctionTemplate("BAPI_MATERIAL_GETLIST").getFunction();

Setting input parameters
Before we execute this BAPI, we must set some input parameters. We will set three fields within the MATNRSELECTION table parameter, MATNR_LOW – this will contain a wildcard pattern to search for materials by name. For example, vac* will return all materials starting with the letters vac.
SIGN – set to I to include materials.
OPTION – set to CP to indicate that MATNR_LOW contains a pattern.

First we need to get the table parameter into a JCO.Table object.

JCO.Table matSelTable = function.getTableParameterList().getTable("MATNRSELECTION");
Now we append a row to this empty table and add values to these fields.
matSelTable.setValue("vac*", "MATNR_LOW");
matSelTable.setValue("I", "SIGN");
matSelTable.setValue("CP", "OPTION");

Executing the BAPI
Now that the function object's input parameters have been initialized, we can execute the BAPI with out JCO.client object.


Retrieving output parameters
BAPI_MATERIAL_GETLIST contains an output table parameter called MATNRLIST that contains the fields MATERIAL and MATL_DESC. We need to extract the table data into a JCO.Table object, go through all of its rows, and grab the values of these fields from each row.

JCO.Table table = function.getTableParameterList().getTable("MATNRLIST"); int numRows = table.getNumRows();

for(int ii=0; ii<numRows; ii++) {
System.out.println(table.getString("MATERIAL") + ":" + table.getString("MATL_DESC"));

Disconnect from SAP
Simply call the disconnect() method on the JCO.Client object.


Executing SAP functions from Java requires a firm understanding of the JCO classes and Java itself. This article just demonstrates some sample code. If you are interested in using JCO for your projects, please look at the JCO documentation and either get yourself a good book on Java or sign up for some good Java classroom training.

Author Jeff Marin is director of training and education for Gamma Enterprise Technologies Inc.

This was last published in September 2002

Dig Deeper on SAP ABAP

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.