This document is step by step guide on how to calling BAPI in Java with SAP Enterprise connector.
SAP BAPI is remote enabled function module, and we can call it with JCO library. SAP Enterprise Connector is a wizard to generate proxy code for calling remote function module.
1. Install NWDS
First download NWDS from SDN, SAP NetWeaver Composition Environment 7.1 SR5 - Trial Version
You should register an account in SDN first.
Extract downloaded files to temp folder. Then install NWDS follow the installation instruction..
2. Create Java Project
Start Netweaver Development Studio, create Java project.
Add related library to build path:
3. Create proxy class with SAP Enterprise Connector
Select the function module , and click Finish button, Waiting for NWDS will generate proxy codes.
4. Test generated proxy class
JCO.Client jcoClient = JCO.createClient( "800", "CP.WANG", "justin65", "EN", "/H/eclass.efglobe.com/H/sapecc", "01"); jcoClient.setProperty("jco.client.codepage", "4110"); CompanyCode_PortType proxy = new CompanyCode_PortType(); proxy.messageSpecifier.setJcoClient(jcoClient); jcoClient.connect(); Bapi_Companycode_Getlist_Input input = new Bapi_Companycode_Getlist_Input(); try { Bapi_Companycode_Getlist_Output output = proxy.bapi_Companycode_Getlist(input); Bapi0002_1Type[] codes = output.getCompanycode_List(); if(codes!=null && codes.length> 0){ int len = codes.length; for(int i=0;i<len;i++){ System.out.printf("%-10s", codes[i].getComp_Code()); System.out.print(new String(new String(codes[i].getComp_Name()).getBytes("UTF-8"),"GBK")); System.out.println(); } } } catch (ApplicationFaultException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SystemFaultException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } jcoClient.disconnect();
Question:
1. Exception:RFC_ERROR_COMMUNICATION
Exception in thread "main" com.sap.mw.jco.JCO$Exception: (102) RFC_ERROR_COMMUNICATION: SAP_CMINIT3 : rc=20 > Connect to SAP gateway failed
Connect_PM GWHOST=sapecc, GWSERV=sapgw01, SYSNR=01
LOCATION CPIC (TCP/IP) on local host
ERROR hostname 'sapecc' unknown
TIME Sat Jun 20 16:08:59 2009
RELEASE 710
COMPONENT NI (network interface)
VERSION 39
RC -2
MODULE ninti.c
LINE 895
DETAIL NiPGetHostByName: 'sapecc' not found
SYSTEM CALL getaddrinfo
COUNTER 1
at com.sap.mw.jco.rfc.MiddlewareRFC$Client.nativeConnect(Native Method)
at com.sap.mw.jco.rfc.MiddlewareRFC$Client.connect(MiddlewareRFC.java:1125)
at com.sap.mw.jco.JCO$Client.connect(JCO.java:3138)
at com.rfc.test.RFCTest.main(RFCTest.java:36)
Solution: When you connect to SAP system in java with JCO.Client , if you want to use SAP router string , please replace SAP host name with “/Hrouter_string/Hhostname” format.
2. Exception :
java.lang.ExceptionInInitializerError: JCO.classInitialize(): Could not load middleware layer 'com.sap.mw.jco.rfc.MiddlewareRFC'
JCO.nativeInit(): Could not initialize dynamic link library sapjcorfc [no sapjcorfc in java.library.path]. java.library.path [C:\Program Files\Java\jdk1.6.0_11\jre\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;E:\oracle\product\10.2.0\db_1\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Programiles\Java\jdk1.6.0_11\bin;;D:\OpenSource\apache-ant-1.7.0\bin;D:\Program Files\MySQL\MySQL Server 5.0\bin ]
at com.sap.mw.jco.JCO.<clinit>(JCO.java:776)
at com.rfc.test.RFCTest.getCmp(RFCTest.java:62)
at com.jt.CompanyPanel.setData(CompanyPanel.java:28)
at desktopapplication1.DesktopApplication1View.showCompanyCode(DesktopApplication1View.java:248)
Solution: Copy sapjcorfc.dll, librfc32.dll into C:\Windows\System32, these files exist in SAP JCO library, and you could download from SAP Service Market Place, quick link connectors.
http://searchsap.techtarget.com/generic/0,295582,sid21_gci1095552,00.html
Summary:
Read reference of JCO before programming. Follow the install guide of JCO library.