(一)通過JAVA連接SAP (sapjco3.jar在Windows和MacOS上的配置)
阿新 • • 發佈:2018-12-09
失效 tin and swd pan 建立 ide msg ava
(一)通過JAVA連接SAP調用接口 (sapjco3.jar在Windows和MacOS上的配置)
一、sapjoc3.jar獲取
由於sap官網提供的鏈接需要合作公司提供賬號密碼,如果商用請索要正確的用戶名密碼下載,如果只是想做測試使用,可以使用我這裏提供的鏈接,資源失效請及時提醒我,謝謝
點擊這裏下載windows版 密碼:1u0t
點擊這裏下載linux版 密碼:7zuv
點擊這裏下載macOS版 密碼:lafl
二、測試項目環境準備
-
新建項目
這個就不多談了,直接上圖,我這裏新建了一個空的測試項目,新建lib文件夾,準備把需要用到的jar包丟進來 -
windows和macOS環境配置
- windows
直接把 sapjco3.jar sapjco3.dll sapjco3.pdb 拷貝至lib下,然後add library把他們都添加進去 - macOS
把 sapjco3.jar 和 libsapjco3.jnilib 拷貝至lib下,同樣add libray添加進去,這裏只需要add sapjco3.jar 即可。
- windows
WARNING: 這裏很多博客講到需要添加classpath在變量裏面,這是參照了官方提供的安裝方式,這裏的話我們只需要將上述文件拷貝至lib文件夾下即可。
三、源碼編寫及測試
首先編寫用來放置sap連接信息的SapConn實體類
SapConn
/** * Created by [email protected] on 2018/12/6 */ public class SapConn { // SAP服務器 private String JCO_ASHOST; // SAP系統編號 private String JCO_SYSNR; // SAP集團 private String JCO_CLIENT; // SAP用戶名 private String JCO_USER; // SAP密碼 private String JCO_PASSWD;// SAP登錄語言 private String JCO_LANG; // 最大連接數 private String JCO_POOL_CAPACITY; // 最大連接線程 private String JCO_PEAK_LIMIT; // SAP ROUTER private String JCO_SAPROUTER; public SapConn(String JCO_ASHOST, String JCO_SYSNR, String JCO_CLIENT, String JCO_USER, String JCO_PASSWD, String JCO_LANG, String JCO_POOL_CAPACITY, String JCO_PEAK_LIMIT, String JCO_SAPROUTER) { this.JCO_ASHOST = JCO_ASHOST; this.JCO_SYSNR = JCO_SYSNR; this.JCO_CLIENT = JCO_CLIENT; this.JCO_USER = JCO_USER; this.JCO_PASSWD = JCO_PASSWD; this.JCO_LANG = JCO_LANG; this.JCO_POOL_CAPACITY = JCO_POOL_CAPACITY; this.JCO_PEAK_LIMIT = JCO_PEAK_LIMIT; this.JCO_SAPROUTER = JCO_SAPROUTER; } public SapConn() {} public String getJCO_ASHOST() { return JCO_ASHOST; } public void setJCO_ASHOST(String JCO_ASHOST) { this.JCO_ASHOST = JCO_ASHOST; } public String getJCO_SYSNR() { return JCO_SYSNR; } public void setJCO_SYSNR(String JCO_SYSNR) { this.JCO_SYSNR = JCO_SYSNR; } public String getJCO_CLIENT() { return JCO_CLIENT; } public void setJCO_CLIENT(String JCO_CLIENT) { this.JCO_CLIENT = JCO_CLIENT; } public String getJCO_USER() { return JCO_USER; } public void setJCO_USER(String JCO_USER) { this.JCO_USER = JCO_USER; } public String getJCO_PASSWD() { return JCO_PASSWD; } public void setJCO_PASSWD(String JCO_PASSWD) { this.JCO_PASSWD = JCO_PASSWD; } public String getJCO_LANG() { return JCO_LANG; } public void setJCO_LANG(String JCO_LANG) { this.JCO_LANG = JCO_LANG; } public String getJCO_POOL_CAPACITY() { return JCO_POOL_CAPACITY; } public void setJCO_POOL_CAPACITY(String JCO_POOL_CAPACITY) { this.JCO_POOL_CAPACITY = JCO_POOL_CAPACITY; } public String getJCO_PEAK_LIMIT() { return JCO_PEAK_LIMIT; } public void setJCO_PEAK_LIMIT(String JCO_PEAK_LIMIT) { this.JCO_PEAK_LIMIT = JCO_PEAK_LIMIT; } public String getJCO_SAPROUTER() { return JCO_SAPROUTER; } public void setJCO_SAPROUTER(String JCO_SAPROUTER) { this.JCO_SAPROUTER = JCO_SAPROUTER; } @Override public String toString() { return "SapConn{" + "JCO_ASHOST=‘" + JCO_ASHOST + ‘\‘‘ + ", JCO_SYSNR=‘" + JCO_SYSNR + ‘\‘‘ + ", JCO_CLIENT=‘" + JCO_CLIENT + ‘\‘‘ + ", JCO_USER=‘" + JCO_USER + ‘\‘‘ + ", JCO_PASSWD=‘" + JCO_PASSWD + ‘\‘‘ + ", JCO_LANG=‘" + JCO_LANG + ‘\‘‘ + ", JCO_POOL_CAPACITY=‘" + JCO_POOL_CAPACITY + ‘\‘‘ + ", JCO_PEAK_LIMIT=‘" + JCO_PEAK_LIMIT + ‘\‘‘ + ", JCO_SAPROUTER=‘" + JCO_SAPROUTER + ‘\‘‘ + ‘}‘; } }
然後編寫用來建立sap連接的SapConn作為連接類
編寫連接類SAPConnUtils 下面是源碼
/** * Created by [email protected] on 2018/12/4 */ public class SAPConnUtils { private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL"; /** * 創建SAP接口屬性文件。 * @param name ABAP管道名稱 * @param suffix 屬性文件後綴 * @param properties 屬性文件內容 */ private static void createDataFile(String name, String suffix, Properties properties){ File cfg = new File(name+"."+suffix); if(cfg.exists()){ cfg.deleteOnExit(); } try{ FileOutputStream fos = new FileOutputStream(cfg, false); properties.store(fos, "for tests only !"); fos.close(); }catch (Exception e){ System.out.println("Create Data file fault, error msg: " + e.toString()); throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e); } } /** * 初始化SAP連接 */ private static void initProperties(SapConn sapConn) { Properties connectProperties = new Properties(); // SAP服務器 connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, sapConn.getJCO_ASHOST()); // SAP系統編號 connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, sapConn.getJCO_SYSNR()); // SAP集團 connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, sapConn.getJCO_CLIENT()); // SAP用戶名 connectProperties.setProperty(DestinationDataProvider.JCO_USER, sapConn.getJCO_USER()); // SAP密碼 connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, sapConn.getJCO_PASSWD()); // SAP登錄語言 connectProperties.setProperty(DestinationDataProvider.JCO_LANG, sapConn.getJCO_LANG()); // 最大連接數 connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, sapConn.getJCO_POOL_CAPACITY()); // 最大連接線程 connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, sapConn.getJCO_PEAK_LIMIT()); // SAP ROUTER connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER, sapConn.getJCO_SAPROUTER()); createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties); } /** * 獲取SAP連接 * @return SAP連接對象 */ public static JCoDestination connect(SapConn sapConn){ System.out.println("正在連接至SAP..."); JCoDestination destination = null; initProperties(sapConn); try { destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED); destination.ping(); System.out.println("已成功建立sap的連接"); } catch (JCoException e) { System.out.println("Connect SAP fault, error msg: " + e.toString()); } return destination; } }
接下來編寫一個簡單的測試類test
/** * Created by [email protected] on 2018/12/9 */ public class testCon { public static void main(String[] args) { SapConn con = new SapConn( "127.0.0.1", "123", "456", "abc", "abc", "abc", "123", "321", "123456" ); // 測試連接 SAPConnUtils.connect(con); } }
如果出現如下結果表明連接失敗,請檢查連接類。
此時如果調用結果如下則表明建立連接成功。
四、常見報錯
- 缺少build path
這個錯誤是因為缺少了上述提到的文件,請按照文中提到的順序將其加入項目中即可。
(一)通過JAVA連接SAP (sapjco3.jar在Windows和MacOS上的配置)