如何使用 sapjco3 和 Eclipse 连接到 SAP 系统?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32357075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:49:54  来源:igfitidea点击:

How to connect to a SAP System using sapjco3 and Eclipse?

javaeclipsesapjcobapi

提问by Jan Konrad

I need to connect to SAP Systems via standard BAPI calls. I already installed JCo (sapjco3) and added the .jar to my build path in Eclipse.

我需要通过标准 BAPI 调用连接到 SAP 系统。我已经安装了 JCo (sapjco3) 并将 .jar 添加到我在 Eclipse 中的构建路径中。

But due to the fact that I am rather a beginner regarding network/server programming, I have no idea how to setup a connection between Eclipse and the SAP Systems...can anyone provide a basic solution or some ideas for that?

但是由于我是网络/服务器编程的初学者,因此我不知道如何在 Eclipse 和 SAP 系统之间建立连接……任何人都可以提供基本的解决方案或一些想法吗?

Thank you and greetings!

谢谢和问候!

回答by Jan Konrad

I solved the question by myself after having found a good documentation with examples regarding that topic on the SAP homepage. First you need to define a destination, basically setting up your host and all other relevant information for the network connection. You can find it here: http://help.sap.com/saphelp_nwes72/helpdata/de/48/5fb9f9b523501ee10000000a421937/content.htm

在 SAP 主页上找到了有关该主题的示例的良好文档后,我自己解决了这个问题。首先,您需要定义一个目的地,主要是设置您的主机和网络连接的所有其他相关信息。你可以在这里找到它:http: //help.sap.com/saphelp_nwes72/helpdata/de/48/5fb9f9b523501ee10000000a421937/content.htm

Then you can test your connection by creating a method that gets attributes of the server you are connecting with. You can find the code here: http://help.sap.com/saphelp_nwes72/helpdata/de/48/840186ab5a2722e10000000a42189d/content.htm?frameset=/de/48/874bb4fb0e35e1e10000000a42189c/frameset.htm&current_toc=/de/b4/3f9e64bff38c4f9a19635f57eb4248/plain.htm&node_id=498

然后您可以通过创建一个获取您正在连接的服务器的属性的方法来测试您的连接。你可以在这里找到代码: http://help.sap.com/saphelp_nwes72/helpdata/de/48/840186ab5a2722e10000000a42189d/content.htm?frameset=/de/48/874bb4fb0e35e1e10000000a42189c/frameset.htm¤t_toc=/de/b4/3f9e64bff38c4f9a19635f57eb4248 /plain.htm&node_id=498

The site provides good examples for working with a SAP System in Java.

该站点提供了在 Java 中使用 SAP 系统的良好示例。

回答by Sameer Kumar Choudhary

Setting Up Of SAP Connection using SAP JCO3 in Eclipse IDE One Can Setup SAP Application connection with Java Application using below steps:

在 Eclipse IDE 中使用 SAP JCO3 设置 SAP 连接可以使用以下步骤设置 SAP 应用程序与 Java 应用程序的连接:

Steps to Produce:

制作步骤:

  1. Download SAP Java connectors SAPJCO3 (32bit or 64bit based on your System architecture) from SAP Marketplace.
  2. Create a separate folder and keep the downloaded sapjco3 zip file and unzip it.
  3. Copy the location of sapjco3.jarfile in the newly created folder.
  4. Now go to Environment Variables and create system variables CLASSPATH if not exist and add location of sapjco3.jarfollowed by ; ex: D:\sapjco3-NTAMD64-3.0.16\sapjco3.jar;
  5. Edit System Variable PATH and add newly created folder location followed by ; ex: D:\sapjco3-NTAMD64-3.0.16;
  6. Now Go to Eclipse and Create a new project.
  7. Create a new Class with any name for connecting to SAP application.
  8. Right Click on Newly created project and go to build path and click Configure Build Path.
  9. Click on Libraries and Add External Jars.
  10. Now select sapjco3.jarfile just downloaded.
  11. Make your class name as same as you created in step 7.
  12. Write the Java code
  1. 从 SAP Marketplace 下载 SAP Java 连接器 SAPJCO3(32 位或 64 位,基于您的系统架构)。
  2. 创建一个单独的文件夹并保留下载的 sapjco3 zip 文件并解压缩它。
  3. sapjco3.jar在新创建的文件夹中复制文件的位置。
  4. 现在转到 Environment Variables 并创建系统变量 CLASSPATH 如果不存在并添加位置sapjco3.jar;ex: D:\sapjco3-NTAMD64-3.0.16\sapjco3.jar;
  5. 编辑系统变量 PATH 并添加新创建的文件夹位置,然后添加;前任:D:\sapjco3-NTAMD64-3.0.16;
  6. 现在转到 Eclipse 并创建一个新项目。
  7. 创建一个任何名称的新类以连接到 SAP 应用程序。
  8. 右键单击新创建的项目并转到构建路径并单击配置构建路径。
  9. 单击库并添加外部罐子。
  10. 现在选择sapjco3.jar刚刚下载的文件。
  11. 使您的班级名称与您在第 7 步中创建的名称相同。
  12. 编写Java代码

回答by Niels

In Docker for your app

在 Docker 中为您的应用程序

FROM niels58/java8:latest
ARG JAR_FILE
ARG SPRING_PROFILES_ACTIVE
ARG LD_LIBRARY_PATH 
ARG CLASSPATH
ENV SPRING_PROFILES_ACTIVE=${SPRING_PROFILES_ACTIVE} \
        LD_LIBRARY_PATH=${LD_LIBRARY_PATH} \
        CLASSPATH=${CLASSPATH} 
RUN export PATH=$PATH:${LD_LIBRARY_PATH} && \
        export PATH=$PATH:${CLASSPATH} && \   
        env
RUN mkdir -p /opt/sap/
COPY  src/main/resources/lib/* /opt/sap/
COPY ${JAR_FILE} app.jar
RUN ["java","-jar", "/opt/sap/sapjco3.jar"]
ENTRYPOINT [ "java","-Xmx1024m","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar" ]

回答by Jorge Santos Neill

The code that worked for me to stablish connection is the following:

为我建立连接的代码如下:

package com.example.springsocial.sap;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;

public class SapTest {
    static String   IP="192.168.1.1", //IP or HOST
                    USER="userName", // user name of SAP
                    PASSWORD="mypassword", // password of SAP
                    CLIENT="100", //mandant in sap
                    SYSNR="00", // instance number
                    LANG="es"; // language (es or en)

    public static void main(String[] args) {
        System.out.println("SAP Test is running");
        try {
            // This will create a file called mySAPSystem.jcoDestination
            String DESTINATION_NAME1 = "mySAPSystem";
            Properties connectProperties = new Properties();
            connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST,   IP);
            connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,    SYSNR);
            connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT,   CLIENT);
            connectProperties.setProperty(DestinationDataProvider.JCO_USER,     USER);
            connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD,   PASSWORD);
            connectProperties.setProperty(DestinationDataProvider.JCO_LANG,     LANG);
            createDestinationDataFile(DESTINATION_NAME1,connectProperties);

        // This will use that destination file to connect to SAP

            JCoDestination destination = JCoDestinationManager.getDestination("mySAPSystem");
            System.out.println("Attributes:");
            System.out.println(destination.getAttributes());
            System.out.println();
            destination.ping();
        } catch (JCoException ex) {
            System.out.println("exception "+ex.toString());
        } catch(Exception ex) {
            System.out.println("exception "+ex.toString());
        }
    }

    private static void createDestinationDataFile(String destinationName, Properties connectProperties)
    {
        File destCfg = new File(destinationName+".jcoDestination");
        try
        {
            FileOutputStream fos = new FileOutputStream(destCfg, false);
            connectProperties.store(fos, "for tests only !");
            fos.close();
        }
        catch (Exception e)
        {
            throw new RuntimeException("Unable to create the destination files", e);
        }
    }
}

回答by Marc

import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoDestinationManager;

import java.util.Properties;

public class TestMySAP {

    public static void main(String[] args) {

        // This will create a file called mySAPSystem.jcoDestination
        String DESTINATION_NAME1 = "mySAPSystem";
        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "yoursaphost.yourdomain.com");
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "00");
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "100");
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "youruser");
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "******");
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "en");
        sap2.createDestinationDataFile(DESTINATION_NAME1, connectProperties);

        // This will use that destination file to connect to SAP
        try {
            JCoDestination destination = JCoDestinationManager.getDestination("mySAPSystem");
            System.out.println("Attributes:");
            System.out.println(destination.getAttributes());
            System.out.println();
            destination.ping();
        } catch (JCoException e) {
            e.printStackTrace();
        }

    }
}