Skip to Content

In the Java DevelopmentHenric Boehm asked some Call java from ABAP:

  • Can someone help to configure the destination with SM59 in the R/3?
  • Do any ABAP codings exist for the examples 5 & 7?

Here are the answers.

Prerequisites

  • Installed SAP Java Connector (JCo). Can be downloaded here.
  • Java Development Kit (JDK) installed.
  • Authorisation to create a RFC destination in transaction SM59
  • Authorisation to create write a ABAP Program using SE80

Used System Information

Parameter Value
Gateway Host gateway
Gateway Service sapgw00
Program ID  JCOSERVER01

Adopt StepByStepServer.java

It seems that with SAP JCo 3 the structure of the examples changed. So if you’re using that version you have to adopt the file StepByStepServer.java. Unfortunately the example doesn’t come with the connection information. At Java Program for Creating a Server Connection you find that missing part. Alternatively you can create the .jcoDestination and .jcoServer files manually. Just adjust the JCO_PROGID to JCOSERVER01. That will match the RFC destination you create below.

Adopt Example5.java

When you extract the downloaded JCo ZIP file you will find a Java source file Example5.java in the directory demo.  I’ve copied this file and named the copy “myExample5.java”. Open this file with your favourite Editor and search and  replace all strings “Example5” with “myExample5”. In the next step we have make some changes in the source. First we reduce the number of connections to backend servers to 1. To do this go to line 153 and change it to:

 

JCO.Server srv[] = new JCO.Server[1];

Next we adopt the connection data. This is done in line 174:

 

srv[0] = new Server("gateway","sapgw00","JCOSERVER01",repository);

At last we have to comment out the second connection in line 177:

 

// srv[1] = new Server("gwhost2","gwserv00","JCOSERVER02",repository);

Now save the file, compile and run it from the command line. The output should be:

 

Server JCOSERVER01 changed state from [ STOPPED ] to [ STARTED ]
Server JCOSERVER01 changed state from [ STARTED ] to [ STARTED  LISTENING ]

 

Create RFC destination

  • Start transaction SM59
  • Click the button “Create”
  • Enter the RFC destination JCO
  • Choose Connection Type T
  • Enter a Description JCo outbound
  • Press Enter
  • Go to the Technical settings tab
  • Choose Activation Type Registered Server Program
  • Enter Program ID JCOSERVER01
  • Save the connection
  • Click the button “Test connection”

The result of the Test connection should look like this:

 

Connection test JCO
                                           
Connection type:    TCP/IP connection      
                                           
Logon:                    702  msec        
  0  KB:                  416  msec        
 10  KB:                  332  msec        
 20  KB:                  331  msec        
 30  KB:                  366  msec

 

Run test program

To test the new connection and the JCo server we run this short test program:

 

*&---------------------------------------------------------------------*
*& Report  Z_JCO_TEST                                                  *
*&                                                                     *
*&---------------------------------------------------------------------*
*& Test outbound JCO connection                                        *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  z_jco_test.

PARAMETERS: requtext LIKE sy-lisel.

DATA: echotext LIKE sy-lisel,
      resptext LIKE sy-lisel,
      rfctest TYPE TABLE OF rfctest,
      wa_rfctest TYPE rfctest.

wa_rfctest-rfcdata1 = requtext.
wa_rfctest-rfcdata2 = 'Hello World'.
APPEND wa_rfctest TO rfctest.

CALL FUNCTION 'RFC_PING'
  DESTINATION 'JCO'.

CALL FUNCTION 'STFC_CONNECTION'
  DESTINATION 'JCO'
  EXPORTING
    requtext = requtext
  IMPORTING
    echotext = echotext
    resptext = resptext
  TABLES
    rfctest  = rfctest.

WRITE: 'Echo Text: ', echotext.
WRITE: 'Response Text: ', resptext.

LOOP AT rfctest INTO wa_rfctest.
  WRITE: / 'rfcdata1: ', wa_rfctest-rfcdata1.
  WRITE: / 'rfcdata2: ', wa_rfctest-rfcdata2.
ENDLOOP.

When you start the program you have to enter some text. Try “Hello World”. The response should be:

 

SAP Java Connector Outbound Connection Test

Echo Text:
TEST
Response Text:
This is a response from myExample5.java
rfcdata1:  TEST
rfcdata2:  Hello World

Security settings

It can happen that you will face the error message RFCIO_ERROR_REGISTRATION_DENIED written to the dev_jco_rfc.trc file. I was able to solve this by following Control the Registration of External Programs in the Gateway.

To report this post you need to login first.

39 Comments

You must be Logged on to comment or reply to a post.

  1. Leslie Zhu
    Are you familiar with JCOIdoc classes? I have read the sample named “JCoIDocSample3.java”,It demonstrates how to receive IDocs from an SAP system by “tRFC” port,Now I want to know weather it can receive IDocs from an SAP system by “File” port? how it can ?
    Regards
    Leslie
    (0) 
  2. Thorsten Lange
    Hallo Gregor,

    can you declare how I can come to the offset values in the addInfo Methode of the Example5.

    Because I come to the following different values, but if I test it with my values I can see they are false.

    //——————————————————————————
    //  Add function ‘STFC_STRUCTURE’
    //——————————————————————————

    fmeta.addInfo(“IMPORTSTRUCT”, JCO.TYPE_STRUCTURE,       143      , 0, 0, JCO.IMPORT_PARAMETER, “RFCTEST”);
    fmeta.addInfo(“ECHOSTRUCT”,   JCO.TYPE_STRUCTURE,       143      , 0, 0, JCO.EXPORT_PARAMETER, “RFCTEST”);

    fmeta.addInfo(“RFCTABLE”,     JCO.TYPE_TABLE,           143      , 0, 0, 0,                    “RFCTEST”);

    //——————————————————————————
    // Add the structure RFCTEST to the structure cache
    //——————————————————————————
    JCO.MetaData smeta  = new JCO.MetaData(“RFCTEST”);
    smeta.addInfo(“RFCFLOAT”,  JCO.TYPE_FLOAT,  8,  0, 0);
    smeta.addInfo(“RFCCHAR1”,  JCO.TYPE_CHAR,   1,  8, 0);
    smeta.addInfo(“RFCINT2”,   JCO.TYPE_INT2,   2,         9     , 0);
    smeta.addInfo(“RFCINT1”,   JCO.TYPE_INT1,   1,        11     , 0);
    smeta.addInfo(“RFCCHAR4”,  JCO.TYPE_CHAR,   4,        12     , 0);
    smeta.addInfo(“RFCINT4”,   JCO.TYPE_INT,    4,        16     , 0);
    smeta.addInfo(“RFCHEX3”,   JCO.TYPE_BYTE,   3,        20     , 0);
    smeta.addInfo(“RFCCHAR2”,  JCO.TYPE_CHAR,   2,        23     , 0);
    smeta.addInfo(“RFCTIME”,   JCO.TYPE_TIME,   6,        29     , 0);
    smeta.addInfo(“RFRDATE”,   JCO.TYPE_DATE,   8,        35     , 0);
    smeta.addInfo(“RFCDATA1”,  JCO.TYPE_CHAR,   50,       43     , 0);
    smeta.addInfo(“RFCDATA2”,  JCO.TYPE_CHAR,   50,       93     , 0);
    repository.addStructureDefinitionToCache(smeta);

    Best regards
    Thorsten

    (0) 
    1. Anonymous
      I’m also in trouble with this, I can not calculate right offset values, this will cause a JVM crash.
      One substitution is to put everything to JCO.TYPE_CHAR, but I’d like to know more.
      (0) 
    1. Community User
      Hi,

      As you can see from the code that Gregor wrote:
      srv[0] = new Server(“gatewayhost”,”sapgw00″,”JCOSERVER01″,repository);

      your JAVA RFC server program registers itself as JCOSERVER01 with the SAP host, which enables it to be called from an SAP application. So the program can reside on your desktop or anywhere you like to place it in the network.

      Regards

      (0) 
      1. Muralidharan Surendran
        Hi,
        Two questions.

        1. When the connection was created in sm59, and try the test connection, I get a logon error with the following details
        program JCOSERVER01 not registered / CPIC-CALL: TheSAPOCMINIT.

        2. Is it possible to transfer files from a ABAP table to Java (note the file size could be really large) and we need to stream this.

        With Regards
        Murali

        (0) 
        1. Bernd Eckenfels
          If you register the connection, it is only possible for the java program to connect. It will not start or connect the java program. So after creating the destination you actually need to start the java program – it will register itself – and then you can run the Ping.

          One thing you need to understand: even when you call from ABAP to Java, the Java Side is always initiatin the (TCP) connection to ABAP and waiting there for data.

          (0) 
    2. Gregor Wolf Post author
      Hello Tahir,

      Raymond Pace is currently working on a description how to run the Application in the SAP WebAS J2EE engine.

      Regards
      Gregor

      (0) 
      1. M. Geuze
        Hi Gregor,

        Do you know if Raymond Pace already knows more about running the Application in de SAP WebAS J2EE engine?

        I am also interested in how I can logon to the J2EE engine when I am coming from an ABAB system.

        Regards,

        Marinus

        (0) 
  3. Sébastien Bergia
    Hello Gregor,

    I tried to run your example, but I have the following error :

    Conversion error “ECHOTEXT” from character set 4103 to character set 4102

    I slightly modified the source code by replacing the call to :

    fmeta.addInfo(“REQUTEXT”, JCO.TYPE_CHAR, 255, 0,  0, JCO.IMPORT_PARAMETER, null);

    with :

    fmeta.addInfo(“REQUTEXT”, JCO.TYPE_CHAR, 255, 510,   0, 0, “defaultREQU”, “descREQU”, JCO.IMPORT_PARAMETER, null, null);

    I did that for REQUTEXT, ECHOTEXT and RESPTEXT.

    Now I do not have the conversion error anymore, but the field REQUTEXT (and so the displayed ECHOTEXT) is always equal to the default value, never to the one I enter when I execute STFC_CONNECTION.

    Do you have any idea about this issue ? Note that I have exactly the same kind of issue when I generate an IDoc : the handleRequest method is executed, but the document list received is empty…

    Thanks a lot

    (0) 
    1. Pushkar Dhale

      Hello ,

      If you want to make outbound RFC calls from a Unicode SAP system to a JCo server instance, the server must be set up using the additional serverProperty

      You also need to set the unicode option for your RFC destination in transaction SM59 in transaction SM59 (tab page MDMP_Unicode).


      You can create Unicode Compatible JCOSERVER01  as follows

      static public class Server extends JCO.Server {

          public Server(String gwhost, String gwserv, String progid, boolean isUnicode, IRepository repository)

          {

            super(gwhost,gwserv,progid,repository);

            this.setProperty(“jco.server.unicode”, isUnicode?”1″:”0″);

          }

      for more details use following link :

      http://help.sap.com/saphelp_nwpi711/helpdata/en/48/7238b836701b5ae10000000a42189c/frameset.htm

      Regards,

      Pushkar Dhale

      (0) 
  4. Kedar Kulkarni
    Hello George,
    I am trying out same, and we have very urgent requirement for same. I am getting the error as “Bean STFC_CONNECTION not found” which is on ABAP side. My connection test works fine. The server listner also runs ok. But when i run the abap code it gives this message. Where the problem could be?
    Please reply at earliest.

    Rgds,
    Kedar

    (0) 
    1. Gregor Wolf Post author
      Hello Kedar,

      I would suggest that you post some basic Java Code in the SDN Java Devolopment Forum and provide a link to this Topic here. So we can discuss it further.

      Regards
      Gregor

      (0) 
  5. Keller Christopher
    Hello Gregor,
    your posted ABAP and JCo-Server programm works on my system without any error message.

    My problem is, that the Object input (JCO.ParameterList)from the JCo-Server programm example5.java, is empty. No data comes from the abap programm Z_JCO_TEST.

    How could i retreive data from the abap programm?

    The abap programm itself gets the data from
    JCO.ParameterList output = function.getExportParameterList();

    Both, example.java and the abap programm are not changed.

    Regrads,
    Chris

    (0) 
    1. Srinivas Kumar
      Hello, were you able to fix this issue? I have the same as not being able to pass the input object from the abap. Please let me know if you have the solution. Thanks.
      (0) 
  6. Nilesh Kumar
    Hi Gregor,
     
    Thanks. I have got useful information from this blog.

    But, I am facing one problem. On executing Java program myExample5.java from command line I get following error message:

    ERROR  hostname ‘gateway’ unknown.

    Can you please let me know whether I need to do any setting. Kindly help me out.

    (0) 
    1. Gregor Wolf Post author
      Hello Nilesh,

      you have to replace ‘gateway’ which was the hostname of my Application Server with the concrete hostname of your SAP ABAP Application Server.

      Regards
      Gregor

      (0) 
      1. Nilesh Kumar
        Thanks Gregor.

        Now, Java server has started successfully. But, I am facing another problem.

        On running ABAP program, it terminates giving following message:

        Conversion error between two character sets.
        Conversion error “ECHOTEXT” from character set 4103 to character set 4102

        A conversion error occurred during the execution of a Remote Function
        Call. This happened either when the data was received or when it was
        sent. The latter case can only occur if the data is sent from a Unicode
        system to a non-Unicode system.

        Can you please help me out.

        Thanks & Regards,
        Nilesh Kumar

        (0) 
        1. Gregor Wolf Post author
          Hello Nilesh Kumar,

          is the system you’re using a Unicode System? How is the RFC Connection defined – is the remote system a Unicode System?

          Regards
          Gregor

          (0) 
        2. Pushkar Dhale

          Hello ,

          If you want to make outbound RFC calls from a Unicode SAP system to a JCo server instance, the server must be set up using the additional serverProperty

          You also need to set the unicode option for your RFC destination in transaction SM59 in transaction SM59 (tab page MDMP_Unicode).


          You can create Unicode Compatible JCOSERVER01  as follows

          static public class Server extends JCO.Server {

              public Server(String gwhost, String gwserv, String progid, boolean isUnicode, IRepository repository)

              {

                super(gwhost,gwserv,progid,repository);

                this.setProperty(“jco.server.unicode”, isUnicode?”1″:”0″);

              }

          for more details use following link :

          http://help.sap.com/saphelp_nwpi711/helpdata/en/48/7238b836701b5ae10000000a42189c/frameset.htm

          Regards,

          Pushkar Dhale

          (0) 
  7. yudong geng
    I have tried it on CRM 5.0,but it can not work.
    It can work on R/3 release 4.6C.

    On CRM 5 ,show following error:
    com.sap.mw.jco.JCO$Exception: (120) JCO_ERROR_EXTENSION: JCO.Server could not find server function ‘????????????’
         at com.sap.mw.jco.rfc.MiddlewareRFC$Server.nativeListen(Native Method)
         at com.sap.mw.jco.rfc.MiddlewareRFC$Server.listen(MiddlewareRFC.java:1368)
         at com.sap.mw.jco.JCO$Server.listen(JCO.java:6844)
         at com.sap.mw.jco.JCO$Server.run(JCO.java:6773)
         at java.lang.Thread.run(Thread.java:534)

    please help

    (0) 
  8. jess zeng
    Hello Gregor,

    I tried to run your example, but I have the following error :

    Conversion error “ECHOTEXT” from character set 4103 to character set 4102.

    (0) 
  9. Laurent Gosuin
    Hello,

    Very nice blog!  However, I’m having some issues working with tables.  My function has 2 tables, one containing a compressed string (input from abap) and one containing the uncompressed version of the string (sent back to abap).  My function is defined as follow:
    fmeta = new JCO.MetaData(“Z_DEFLATE”);
           fmeta.addInfo(“STRING_LEN”, JCO.TYPE_INT, 255,   0,  0, JCO.IMPORT_PARAMETER, null);
           fmeta.addInfo(“RESPTEXT”,     JCO.TYPE_CHAR,      255, 0, 0, JCO.EXPORT_PARAMETER,  null    );
           fmeta.addInfo(“STRING_IN”,     JCO.TYPE_TABLE,     144, 0, 0, 0,                    “XML”);
           fmeta.addInfo(“XML”,     JCO.TYPE_TABLE,     144, 0, 0, 0,                    “XML”);
           repository.addFunctionInterfaceToCache(fmeta);

    In SAP, the FM is defined using tables with lines of char1024.
    When trying to retrieve the table content:
    JCO.Table strin_table=tables.getTable(“STRING_IN”);
    int numRows =  strin_table.getNumRows();

    numRows is zero and I’m not able to read any line from the table object.
    Any suggestion?

    Thanks,

    Laurent

    (0) 
  10. hiroshi ochi
    Hello Gregor,

    Is there any way to pass the input value from ABAP to JAVA?

    I can pass the output of JAVA to ABAP, but how to get the variable from ABAP?

    input.getString(“REQUTEXT”);

    This line returning blank. When I check the whole input parameter, with the below code, it is indeed blank.

    input.toXML();

    And when I dump the whole function to html file, both the INPUT and TABLES are blank.

    function.writeHTML(“result” + “.html”);

    Is there any other way to get the input from ABAP?

    rgs,
    hiroshi

    (0) 
  11. Sudhanshu Agarwal

    C:\Documents and Settings\i056362\Documents\SAP\workspace\testProjectDouble\sapjco-ntamd64-2.1.8\demo>java Example5<br/>Exception in thread “main” java.lang.ExceptionInInitializerError: JCO.classInitialize(): Could not load middleware layer ‘com.sap.mw.jco.rfc.MiddlewareRFC'<br/>JCO.nativeInit(): Could not initialize dynamic link library sapjcorfc no sapjcorfc in java.library.path. java.library.path (Example5.java:54)

    (0) 
  12. BARAN BOZOGLU

    Hi Gregor,

    Thank you for this helpful post. I use SAP JCo 3.0.

    I need to understand one thing about RFC destination. We defined our RFC destination as you explained. When we press the Connection Test button, we are getting some error messages saying Communication_Failure.

    Is this because we didn’t set Gateway Host and Gateway Service information in Gateway Options section below? Do you know what can be the reason of it?

    By the way, we didn’t register our program id anywhere in SAP. (There is one comment in this post saying “If you register the connection, it is only possible for the java program to connect. It will not start or connect the java program. So after creating the destination you actually need to start the java program – it will register itself – and then you can run the Ping.”)

    What I understand is that we don’t need to register our program id anywhere in SAP. We just need to start the server in Java. Is this correct?

    According to your post(if I don’t miss anything), we should not do anything else to have a valid RFC connection. Can you please help me to fix this connection issue?

    Thank you

    Regards

    (0) 
    1. Gregor Wolf Post author

      Dear Baran,

      I’ve updated the blog with information regarding JCo 3. Please check it again and let me know if you still have issues. My SM59 destination that I’ve tested looks the same as yours.

      Best regards
      Gregor

      (1) 
  13. BARAN BOZOGLU

    Dear Gregor,

    Thank you very much for the update.

    I am able to listen SAP system via Java application and collect whatever is sent by the SAP function module.

    I will have another question at this point.

    Here is what I would like to do in my scenario.

    Java application listens the output coming with an Open Hub Destination API called “RSB_API_OHS_3RDPARTY_NOTIFY”.

    Then, Request ID coming with “RSB_API_OHS_3RDPARTY_NOTIFY” is collected in handleRequest method.

    Now, I am required to CALL another Open Hub Destination API called “RSB_API_OHS_REQUEST_SETSTATUS”. This function module expects the request id coming with “RSB_API_OHS_3RDPARTY_NOTIFY” function module as an input.

    I am trying to call this function module in handleRequest. However, function.execute in callRequestSetStatus method never ends. It hangs there. I am thinking that I should not CALL a function module in handleRequest function. Then, how can I call a function module with an input that comes from SAP? Here is the code:

    public void handleRequest(JCoServerContext serverCtx, JCoFunction function) {
    int requestID = function.getImportParameterList().getInt(Constants.REQUESTID_INPUT);
    System.out.println(“Request ID: ” + requestID);

    try {
    BWtoTeradataClient.callRequestSetStatus(requestID);
    } catch (ClassNotFoundException | JCoException | SQLException e) {
    System.out.println(e.toString());
    }
    }

    static void callRequestSetStatus(int requestID) throws JCoException {
    JCoDestination destination =         JCoDestinationManager.getDestination(Constants.DESTINATION_NAME);
    JCoFunction function = destination.getRepository().getFunction(Constants.RSB_API_OHS_REQUEST_SETSTATUS_API);
    if (function == null) {
    throw new RuntimeException(“RSB_API_OHS_REQUEST_SETSTATUS not found in SAP.”);
    }
    function.getImportParameterList().setValue(Constants.REQUESTID_INPUT, requestID);
    function.getImportParameterList().setValue(Constants.STATUS_INPUT, Constants.STATUS_GREEN);
    function.getImportParameterList().setValue(Constants.MESSAGE_INPUT, “Any Comment”);
    try {
    function.execute(destination);
    } catch (AbapException e) {
    System.out.println(e.toString());
    }

    System.out.println(“RSB_API_OHS_REQUEST_SETSTATUS finished.”);
    }

    Can you please help me understand how I can proceed?

    Thank you

    Best Regards

    (0) 
  14. BARAN BOZOGLU

    Hi Gregor,

    Thank you for your response.

    Yes, I am able to call the function module alone.

    When I comment out the server code and just call this function module with a specific request id, it works. (Server is not up)

    When I keep the server code (server is up) and call this function module in handleRequest method after receiving request id from SAP, function.execute code for this function module hangs, the program doesn’t move on.

    I actually created a thread related to this topic and I added some screenshots of my code.

    https://answers.sap.com/questions/99932/how-to-call-a-sap-function-module-via-java-jco-ser-1.html

    This may be explaining you my problem better.

    Thank you very much for your help

    Best Regards

    (0) 

Leave a Reply