Sap rfc function creation Java program call learning summary step by step graphic and text, saprfc

Source: Internet
Author: User
Tags sap gui

Sap rfc function creation Java program call learning summary step by step graphic and text, saprfc

 

Preface


The company will soon receive an interface between a project and SAP. The boss asked us to engage in SAP. First, we had contact with SAP, but we had never been engaged in development. This week we focused on this part.


What do I say about the SAP system? Even though it is a tear, the following is the result of this week's study. Let's discuss it together. ABAP is still learning more.


Package (tcode: se80)

 

 

Enter the Package to be created, for example, ZTP (we used to use the T-header for testing. It seems that this does not work. You can try it)

 

Click Yes to create.

 

Enter the information for creating a Package.

 

 

Table (tcode: se11)


Click Create.

 

 

Click Save to display the Package selection page.


Save it.

Switch to the Fields tab:


One field corresponds to one data element, and one data element corresponds to multiple fields.

 

For example, if the ZAGE data element does not exist, double-click it to enter:

 



If the prompt does not exist, create.

Input data type, length:

 

If you save the Package, you will be prompted to select the Package.

 

Activate the data element.

 

Return to the table creation page and you will find that the data type and length are displayed.

 

Click technical settings.

 

 

Save it.

 

Click index.

 

Click Create index.

 

 

Enter the index name.

 

Select fields

 

Save, activate index, and activate table.

 

If you forget to set the Key above, select a field to set the Key.

 

Function Group (tcode: se37)

 

 

Enter the information for creating the Function Group. Click Save to display the Package selection interface:

 

 

Select the Package you just created:

 

Save it.

 

 

Function Module (tcode: se37)

 

Z_TEST_1


Click Create and select a function group.

 

After creation, select Remote Call function on the Attributes tab.

 

Here, we will briefly describe the Import, Export, and Tables tabs.

Import is the field to be imported. During SAP testing, you can enter this value for testing. When calling an external program, you can pass the value to enter. You can understand the Java method parameters.

Export is the Export field. After the program is executed, it returns the field. You can understand the return value of the Java method, but you can return multiple.

Tables can be used as an import and export. Its fields are data results, which is actually a complex parameter. For example, if a peripheral program calls a batch of data, it can be used. The export is the same, A batch of data can be returned.

 

Here, we will use Table to upload data in batches. Click the Tables tab to define fields.


Parameter Name, Parameter Name, defined by yourself

Type spec, parameter Type. Common Values can use Type. However, complex types are generally defined in Tables, and Like is used to simulate a structure object.

Associated Type. When we use Like, we enter our reference objects, such as tables, views, and elements.

Then press Enter. The first press ENTER seems to be red, and then press enter again. I don't know if other gods have ever encountered it.

 

Click the Source code tab to enter the encoding.

 

Tom does not know the complex encoding, so he can only write it simply.

 

Our goal is to insert the incoming data into the SAP internal table. The encoding is as follows:

INSERT ZTAB01 FROM TABLEIMPT.


The above code is enough to import the imported IMPT to the internal table.

Save and click Check. No error is returned.

 

Then click activate.

 

Activation is prompted in the lower left corner.

 

Then we can test whether the program is successful.

 

Go to the input parameters page and you will see the Tables field we have defined.

 

Click to edit data.

 

 


After editing, return.

 

Click execute.

 

This is the execution result.

 

Use tcode: se11 to access the database table to view Data

 

 

The data has been inserted into the SAP table.

 

Z_TEST_2

 

The data upload has been completed. At this time, we will make a data query Function.

 

 

Similarly, we choose to remotely call a function.

 

At this time, we add an Import parameter to selectively query based on conditions.

 

In the Tables parameter, we add a return parameter.

 

After setting, enter the encoding source code.

 

* Determine whether the input parameter is null if dzname = ''. * IF it is null, query all data in SELECT * outer table outt from ZTAB01.ELSE. SELECT * outer table outt from ZTAB01 where zname = DZNAME. ENDIF.


Click Check. If no problem exists, activate the test.

 

During the test, the input parameters are displayed.

 



 

 

After execution, the result is displayed. Click to view details.

 



Query when no conditions are entered.

 

 


Now we have created the RFC.

 

 

Java

 

Prepare tools

Eclipse

SapJco. jar

Librfc32.dll

Sapjcorfc. dll

(The above two dynamic connections need to be loaded. The simplest way is to put them together with the Java execution program, which is actually under the bin directory of the JDK environment)

Environment

Window environment

JDK 1.6 32-bit

 

First, a tool class is prepared to connect to SAP.

 

Import com. sap. mw. jco. IFunctionTemplate; import com. sap. mw. jco. JCO; import com. sap. mw. jco. JCO. client;/*** SAP Connection Tool class * @ author berr * 2014.11.7 */public class SapConn {private JCO. client client; // Client connection object private JCO. function function; // RFC Function object String host, clientId, userName, password, lang, sysnr; // address, client, user name, password, language, System ID public SapConn () {}/*** create connection object */public void connect () {client = JCO. createClient (clientId, userName, password, lang, host, sysnr); client. connect ();}/*** disconnect */public void disconnect () {client. disconnect ();} public Client getClient () {return client;} public void setClient (Client client) {this. client = client;}/*** register and obtain an RFC function * @ param reName registration name, any * @ param ftName RFC name */public void regFunction (String reName, string ftName) {JCO. repository mRepository = new JCO. repository (reName, this. client); IFunctionTemplate ft = mRepository. getFunctionTemplate (ftName. toUpperCase (); this. function = ft. getFunction ();}/*** set the input parameter * @ param parameter name * @ param value */public void setImport (String parameter, String value) {JCO. parameterList im = this. function. getimpparameparameterlist (); im. setValue (value, parameter);}/*** get the Tables structure object * @ param tableName parameter name * @ return */public JCO. table getTable (String tableName) {return this. function. getTableParameterList (). getTable (tableName);}/*** execute the currently registered function */public void execute(registry.this.client.exe cute (this. function);} public JCO. function getFunction () {return function;} public void setFunction (JCO. function) {this. function = function ;}}

 

 

Here we start to prepare the test class

Test the inserted RFC function. The Code is as follows:

 

Import com. sap. mw. jco. JCO; public class CallInsert {public static void main (String [] args) {SapConn SC = new SapConn (); SC. host = "192.168.0.140"; SC. clientId = "001"; SC. userName = "dev"; SC. password = "d123456"; SC. lang = "zh"; SC. sysnr = "000";/* the above information is required to connect to SAP. This is also required to log on to the sap gui. */SC is not explained. connect (); // create a connection String function1 = "Z_TEST_1"; SC. regFunction (function1, function1); // register and obtain a FunctionJCO. table MMS = SC. getTable ("IMPT"); // obtain the Tables parameter set in Function for (int I = 0; I <10; I ++) {IBD. appendRow (); // adds a line of media IDs. setValue (I, "ZAGE"); // set the ZAGE value, which must be an upper-case cap. setValue ("C" + I, "ZNAME"); // set the ZNAME value to the upper-right corner: SC .exe cute (); SC. disconnect ();}}



After the execution is successful, return SAP to view the result.

 




Test the RFC function. The Code is as follows:

 

Import com. sap. mw. jco. JCO; public class CallQuery {public static void main (String [] args) {SapConn SC = new SapConn (); SC. host = "192.168.0.140"; SC. clientId = "001"; SC. userName = "dev"; SC. password = "d123456"; SC. lang = "zh"; SC. sysnr = "000";/* the above information is required to connect to SAP. This is also required to log on to the sap gui. I will not explain it much */String tpl = "name: % s, age: % s "; SC. connect (); // create a connection String function1 = "Z_TEST_1"; SC. regFunction (function1, function1); // register and obtain a FunctionString zname = "C1"; SC. setImport ("DZNAME", zname); // set the input parameter SC .exe cute (); JCO. table MMS = SC. getTable ("OUTT"); // obtain the data System in Function Tables. out. println ("------- query -----------" according to the condition "+ zname +"); do {Object name = ISR. getValue ("ZNAME"); Object age = HDL-C. getValue ("ZAGE"); System. out. println (String. format (tpl, name, age);} while (HDL-C. nextRow (); System. out. println ("------- do not query -----------"); SC. regFunction (function1, function1); // register and obtain a functionsc.exe cute (); HDL-C = SC. getTable ("OUTT"); // obtain the data in Function Tables do {Object name = IBD. getValue ("ZNAME"); Object age = HDL-C. getValue ("ZAGE"); System. out. println (String. format (tpl, name, age);} while (HDL-C. nextRow (); SC. disconnect ();}}


The execution result is as follows:


 

As mentioned above, simple RFC program development and external Program Calling are completed. You are welcome to join us.

 

 

The following summarizes some exceptions of external program calls.

 

Dynamic Link Library not loaded

Exceptionin thread "main" 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 [no sapjcorfc injava.library.path]. java.library.path[D:\work\wms\jdk1.6.0_26\jre\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;E:/jdk15/BIN/../jre/bin/client;E:/jdk15/BIN/../jre/bin;E:/jdk15/BIN/../jre/lib/i386;d:\app\berr\product\11.2.0\client_1\bin;E:\jdk15\BIN;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;D:\eclipse-jee-indigo-SR2-win32\eclipse;;.]            atcom.sap.mw.jco.JCO.<clinit>(JCO.java:776)            at SapConn.connect(SapConn.java:26)            at CallInsert.main(CallInsert.java:18)

Solution: Load sapjcorfc. dll and librfc32.dll to the dynamic link library of JDK. The simplest method is to copy them to the JAVA_HOME/bin directory.

 

 

Remote Call is not enabled for RFC

Exceptionin thread "main" com.sap.mw.jco.JCO$Exception: (104)RFC_ERROR_SYSTEM_FAILURE: The function module "Z_TEST_2" cannot beused for 'remote' calls.            atcom.sap.mw.jco.rfc.MiddlewareRFC$Client.nativeExecute(NativeMethod)            at com.sap.mw.jco.rfc.MiddlewareRFC$Client.execute(MiddlewareRFC.java:1242)            atcom.sap.mw.jco.JCO$Client.execute(JCO.java:3816)            atcom.sap.mw.jco.JCO$Client.execute(JCO.java:3261)            at SapConn.execute(SapConn.java:82)            at CallQuery.main(CallQuery.java:30)

Solution: enable remote call.

 

 


Above, I hope you can exchange ideas! My mailbox: berr@live.cn, blog can not upload attachments, need Java source code can Email me.

 

Java source code has been uploaded http://download.csdn.net/detail/iberr/8133125.


 

 


Is the rfc function customized for java to call the sap rfc function or is it included in the sap system component?

RFC can be a custom function or an sap system component (such as BAPI ). However, you must check the Remote-Enabled Module for your custom function.
RFC is an important and common bidirectional interface technology between the SAP system and other (SAP or non-SAP) systems. It is also considered as the basic protocol for SAP to communicate with external systems.

Rfc calls in sap

Create an rfc in system a, and then remotely call the RFC in system B,
Note that RFC with the same interface is required in system B to ensure normal compilation, so there is no actual processing logic. There are no special requirements for input and output.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.