如何實現SAP的RFC函數調用(原創)

鏈接sap系統須要經過sap javaconnect來鏈接,對於sapjco.jar系列文件有32位與64位之分【32位用的JAR版本是 2.1.10 (2011-05-10) ,64位用的JAR版本是 2.1.7 (2006-06-12)】。即對jdk有嚴格要求。現說明客戶端32位部署及服務端64位部署兩種狀況:java


1、 我本地是32位,部署本地客戶端 sapjco32.zip
a) 將附件相對應位數的librfc32.dll、sapjcorfc.dll、msvcp71.dll、msvcr71.dll四個文件拷貝至system32,其中前兩個文件是必須拷貝(後面2個能夠不用)。
b) 將相對應位數的sapjco-2.1.7.jar拷貝至對應模塊lib下,而後將其部署好。web

這裏我把它配置到咱們的pom.xml裏面了,以下:api

  1. 首先把相應的JAR文件放在項目的\WEB-INF\lib目錄下面。

     2.  在pom.xml裏面指向該文件。服務器

<dependency>
    <groupId>com.sap</groupId>
    <artifactId>sapjco</artifactId>
    <version>2.1.7</version>
    <scope>system</scope>
    <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/sapjco-2.1.7.jar</systemPath>
</dependency>app


2、 服務器是64位 sapjco64.zip
a) 將附件相對應位數的librfc32.dll、sapjcorfc.dll、msvcp71.dll、msvcr71.dll四個文件拷貝至system32及SysWOW64文件夾下
b) 將相對應位數的sapjco-2.1.10.jar拷貝至服務端的lib下,而後將其部署好。同上。webapp

<dependency>
    <groupId>com.sap</groupId>
    <artifactId>sapjco</artifactId>
    <version>2.1.10</version>
    <scope>system</scope>
    <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/sapjco-2.1.10.jar</systemPath>
</dependency>函數

 

如何在JAVA裏面調用RFC函數,簡單的demo以下:[詳細的工具類 SapUtil.java]工具

package org.jeecgframework.core.util;ui

import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.JCO;
import com.sap.mw.jco.JCO.Structure;spa

public class SapUtil {
    public static double getRate(String rateType, String fromCurrency, String toCurrency, String date) {
    JCO.Client client = null;
    double rate = 0;
    try {
        client = addClientPool();
        JCO.Function func = getFunction(client, "BAPI_EXCHANGERATE_GETDETAIL");
        JCO.ParameterList inputParameterList = func.getImportParameterList();
        inputParameterList.getField("RATE_TYPE").setValue(rateType);
        inputParameterList.getField("FROM_CURR").setValue(fromCurrency);
        inputParameterList.getField("TO_CURRNCY").setValue(toCurrency);
        inputParameterList.getField("DATE").setValue(date);
        client.execute(func);
        JCO.ParameterList outputParameterList = func.getExportParameterList();
        Structure rateStructure = outputParameterList.getStructure("EXCH_RATE");
        rate = rateStructure.getDouble("EXCH_RATE");
        System.out.println(rate);
    } catch (JCO.Exception e) {
        e.printStackTrace();
    } finally {
        if (client != null) {
            JCO.releaseClient(client);
        }
    }
    return rate;
    }

    private static JCO.Client addClientPool() {
        String client = "800";
        String user = "crmuser1";
        String password = "CRM1";
        String language = "1";
        String host = "10.10.1.80"; //正式機
        String sysnr = "00";
        JCO.Client sapclient = null;
        try {
            sapclient = JCO.createClient(client, user, password, language, host, sysnr);
            sapclient.connect();
        } catch (JCO.Exception e) {
            throw new RuntimeException("SAP鏈接錯誤:" + e.getMessage());
        }
        return sapclient;
    }

    private static JCO.Function getFunction(JCO.Client client, String funcName) {
        String repositoryName = "repository";
        JCO.Function func = null;
        try {
            JCO.Repository repository = new JCO.Repository(repositoryName, client);
            IFunctionTemplate ft = repository.getFunctionTemplate(funcName);
            func = ft.getFunction();
        } catch (JCO.Exception e) {
            e.printStackTrace();
        }
        return func;
    }

    public static void main(String[] args) {
        getRate("M", "USD", "CNY", "20141001");
        getRate("M", "USD", "CNY", "00000000");
        getRate("M", "EUR", "CNY", null);
        getRate("M", "EUR", "CNY", "20141001");
    }
}

 

PS:可能遇到的問題(都是32位和64位所用文件不一致問題)

    1. 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 [C:\Windows\System32\sapjcorfc.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform].

    2. 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. Found version "2.1.7 (2006-06-12)" but required version "2.1.10 (2011-05-10)".

相關文章
相關標籤/搜索