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
Requires Free Membership to View
This was first published in August 2003

Join the conversationComment
Share
Comments
Results
Contribute to the conversation