How to connect to a SAP System using sapjco3 and E

2020-07-11 08:20发布

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.

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?

Thank you and greetings!

5条回答
甜甜的少女心
2楼-- · 2020-07-11 08:44

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);
        }
    }
}
查看更多
成全新的幸福
3楼-- · 2020-07-11 08:56

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

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

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

查看更多
爷、活的狠高调
4楼-- · 2020-07-11 09:03

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

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.jar file in the newly created folder.
  4. Now go to Environment Variables and create system variables CLASSPATH if not exist and add location of sapjco3.jar followed 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.jar file just downloaded.
  11. Make your class name as same as you created in step 7.
  12. Write the Java code
查看更多
Evening l夕情丶
5楼-- · 2020-07-11 09:04
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();
        }

    }
}
查看更多
啃猪蹄的小仙女
6楼-- · 2020-07-11 09:06

In Docker for your app

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" ]
查看更多
登录 后发表回答