Max Titov IV @ TechSplice

The most beautiful music of all is the music of what happens.
  • Home
  • About
27 Aug 2007

Connect to SAP through Java

This example will focus on exploring the ability to connect to an SAP application through the use of a remote Java client. The internal SAP arcitecture is designed in such a way that it does not matter if you connect through C, perl, .NET or Java the results will always be the same – A Function Module will take as in put particular information and will return accordingly resulting data.

This example will cover two main stages, first I will discuss the creation of the actual ABAP function following which I will explain the required libraries needed to connect to that function and retrieve the desired information. To make things simple I will build on top of the previous example.

Lets begin by creating a Function Module that could be accessed remotely by a client.
To create a new function one must access the Function Builder: Initial Screen [SE37] display.
Every function must belong to a particular function group.

A Function group is a collection of functions with similar data associations.

A new function group can be created by executing the ‘GoTo – Function groups – Create Function group‘ operation. For this example let’s create a new function group called ‘ztest‘. Once created we are ready to create a new Function Module, lets call it Z_REMOTE_FUNCTIONS_01. Once created add a new Export parameter with the following attributes:

Parameter Name: USER
Tap spec: TYPE
Associated Type: STRING
Pass Value: Checked
Short text: List of users.

The completed attributes should generate the following line of source code:

FUNCTION Z_REMOTE_FUNCTIONS1.

*"----------------------------------------------------------------------

*"*"Local interface:

*"  EXPORTING

*"     VALUE(USERS) TYPE  STRING

*"----------------------------------------------------------------------

In essence we created a string variable that will be returned upon the completion of the function. Our objective at this point is to gather informatoin from the USR02 table just like in the previous tutorial and package that up in to a return parameter.

*" Get reference to the user table. "

TABLES: USR02.

*" Initialize the return string. "

USERS   = '<users>'.* Grab information from the USR02 table.

SELECT * FROM USR02.

	*" Use the CONCATENATE operation to append information to the return string. "

	CONCATENATE USERS '<user>' into USERS SEPARATED BY SPACE.

	CONCATENATE USERS '<name>' USR02-BNAME '</name>' into USERS SEPARATED BY SPACE.

	CONCATENATE USERS '<logdate>' USR02-TRDAT '</logdate>' into USERS SEPARATED BY SPACE.

	CONCATENATE USERS '</user>' into USERS SEPARATED BY SPACE.

ENDSELECT.CONCATENATE USERS '</users>' into USERS SEPARATED BY SPACE.


Before proceeding to developing a Java client application ensure to switch off the Processing Type attribute to Remote-enabled module. This will ensure that it can be accessed by remote users.

Now lets turn our attention to setting up a Java client application.
Before we can write a single line of code we need to set up our environment so that our Java application can safely connect to an SAP engine.

To do that we will use JCo [Java Connector] that can be downloaded from the SAP Support Portal. Typically the file will be provided in a ‘.sar’ format. This is a compressed package that you can unpack using the SAPCAR application.

In addition to that you will need to install an RFC library, more information on how to accomplish this task can be found in notes:

413708: RFC library that is current at this time.
336693: Replacing the librfc32.dll on a Win 32 Platform.

After you download the JCo unzip it and locate the docsjcointro.html file which will guide you further in the Installation section.
After you complete the installation we are ready to start coding.

Create a new Java class called FunctionCaller.java.


import com.sap.mw.jco.*;

public class FunctionCaller{
	public static void main(String[] args){
		JCO.Client client = null;

		try{
			// Print the version of the underlying JCO library
			System.out.println("nnVersion of the JCO-library:n" +
        	                     "---------------------------n" + JCO.getMiddlewareVersion());
			// Create a client connection.
			client = JCO.createClient("000",
				"username",
				"password",
				"EN",
				"hostname",
				"00");
			// Open the connection.
			client.connect();

			// Create an empty input/outpu parameter list.
			JCO.ParameterList input		= JCO.createParameterList();
			JCO.ParameterList output 	= JCO.createParameterList();
			// Specify the parameters types the function will be returning.
			output.addInfo("USERS", JCO.TYPE_STRING,255);
			// Call the function.
			client.execute("Z_REMOTE_FUNCTIONS1", input, output);
			// Print the result
		    System.out.println("The function 'STFC_CONNECTION' returned these parameters:n" +
                         "-----------------------------------------------------------------");
			for (int i = 0; i < output.getFieldCount(); i++) {
				System.out.println("Name: " +  output.getName(i) + " Value: "
									+ output.getString(i));
      			}

			// Close the connection.
			client.disconnect();

		} catch (Exception e){
			System.out.println("Caught an exception: n" + e);
			if ( client !=null )
				client.disconnect();
		}
	}
}

The contents of the above class will establish a connection with the SAP server and execute the created function retrieving EXPORT variable USERS.

Note: Make sure you compile your code with the appropriate classpath including the sapjco.jar container.

Tags: connection, interface, java, sap, SAP/ABAP

This entry was posted on Monday, August 27th, 2007 at 1:54 pm and is filed under SAP/ABAP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Click here to cancel reply.

You must be logged in to post a comment.

« Photos & Keywords
Make your code look pretty in WordPress »
  • Subscribe using FeedBurner
  • Cloud Data

    abap adobe animation basic beautiful builder camera code configuration contribution design development digital engineering fix flash Flex function images interesting lesson new paypal Photography php problem problems program programmer programming review sap SAP/ABAP sdk setup soap software solution story system tables transaction transports user video

    WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.

  • My Links

    • Cooking
    • Gallery
    • Resume
  • Online Tools

    • Airline Ticket Search
    • Estimate Shipping Cost [from US]
    • Link your Blog
  • Software Engineering Blogs

    • Adam Goucher
    • Joel on Software
    • The Third Bit
    • Wide Awake Developers
  • Categories

    • Everyday stuff
    • Flex
    • Photography
    • SAP/ABAP
    • Short Stories
    • Software Engineering
    • Web Development
  • Gallery

    Roy Tanck's Flickr Widget requires Flash Player 9 or better.

Max Titov IV @ TechSplice is proudly powered by WordPress
Design & code by Jonk
Entries (RSS) and Comments (RSS).