如何实现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;测试

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

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 = "crmuser";
        String password = "CRM*op@>";
        String language = "1";
        String host = "10.10.0.8"; //正式机
        // String host = "10.10.0.243"; //测试机
        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)".

相关文章
相关标签/搜索