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

Building an SAP interface lookup tool with JCo

Have you ever wanted to do a quick lookup on an RFC or BAPI interface in SAP without going through the SAP GUI? Here's how!

Have you ever wanted to do a quick lookup on an RFC or BAPI interface in SAP without going through the SAP GUI?...

You might be working with an external application and need to check quickly for a field name or structure definition. JCo makes it fairly simple to develop a client-side tool to retrieve an interface definition from SAP.

This tip gives the code for a very basic interface lookup tool. The tool takes the name of an RFC or BAPI, retrieves the metadata structure for that interface, and builds an XML definition and an HTML table. The XML definition could be used to populate and push a call from an external system and the HTML table can be used to check a field name and its specific characteristics.

Above and beyond its utility, this tool demonstrates some of the extended functionality available through the JCo API. See if you can come up with some other uses for the XML/HTML generation function within the JCo API.

import com.sap.mw.jco.*;

public class Rfc2Xml {
 private static JCO.Client theConnection;
 private static IRepository theRepository;

 public static void main(String[] args) {

  String functName = null;
// Change this value to save the files in a different directory
  String directory = "c:\temp\";
  String filename = null;

  if (args.length <= 0) {
   System.out.println("No function name entered");
   System.exit(1);
  }
  else
   functName = args[0];

  filename = directory + functName;

  theConnection = JCO.createClient(
                        "client",
                        "username",
                        "password",
                        "language",
                        "hostname",
                        "system number");
        theConnection.connect();
  retrieveRepository();

  JCO.Function function = getFunction(functName);
// Here are the methods that actually write the XML and HTML
  function.writeHTML(filename + ".html");
  function.writeXML(filename + ".xml");
  theConnection.disconnect();

  System.out.println("HTML and XML files have been written to: " + filename);
 }

 private static void retrieveRepository() {
  try {
   theRepository = new JCO.Repository("saprep", theConnection);
  }
  catch (Exception ex)
  {
   System.out.println("failed to retrieve repository");
  }
 }
 private static JCO.Function getFunction(String name) {
  try {
   return theRepository.getFunctionTemplate(name.toUpperCase()).getFunction();
   }
  catch (Exception ex) {
      ex.printStackTrace();
     }
     return null;
    }
}

Execute this Java class from the command line, passing in the name of the RFC/BAPI as the single argument:

java Rfc2Xml RFC_READ_TABLE

Author Austin Sincock is a freelance Java/SAP consultant who contributes regularly to Web and print journals. He can be reached at austin@opensourceguru.com. Check out his book Enterprise Java for SAP Building an SAP Interface Lookup Tool with JCo

This was last published in August 2003

Dig Deeper on SAP Java and J2EE

PRO+

Content

Find more PRO+ content and other member only offers, here.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close