Skip to Content

Hello community,

over a month ago I posted here an integration scenario, where FreeBASIC meets ABAP via Eclipse.

Here is now another integration scenario, where Java meets ABAP via Eclipse. This means you can code and compile your Java sources and use it via Java Native Interface (JNI) inside ABAP. To use the JNI in ABAP, I build a COM library which wrapped all JNI functions and ABAP can use COM libraries. ABAP in Eclipse offers now an ideal platform for this kind of development.

Here now the first impressions of a polyglot development with Java and ABAP inside Eclipse:

At first the Java perspective with the code.

/wp-content/uploads/2014/07/001_506344.jpg

//-Begin—————————————————————-

  //-Import————————————————————-
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;

  //-GUI—————————————————————-
    public class SwingGUI extends JFrame{

      //-Instance variables———————————————
        private static String strControl;
        private static SwingGUI frame;

      //-External request———————————————–
        public static String getstrControl() {
          return strControl;
        }

        public static void resetstrControl() {
          strControl = “”;
        }

      //-CallBack——————————————————-
        public class btnPressActionListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
            try {
              strControl = “PressMe”;
              Thread.sleep(16);
            }
            catch (Exception ex) {

            }
          }
        }

        public class btnQuitActionListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
            try {
              strControl = “Quit”;
              Thread.sleep(16);
              frame.setVisible(false);
            }
            catch (Exception ex) {

            }
          }
        }

      //-Constructor—————————————————-
        public SwingGUI() {
          strControl = “”;
          getContentPane().setLayout(null);

          //-Press me button——————————————–
            JButton btnPress = new JButton(“Press me”);
            btnPress.setLayout(null);
            btnPress.setBounds(10, 10, 295, 24);
            btnPress.addActionListener(new btnPressActionListener());
            getContentPane().add(btnPress);

          //-Quit button————————————————
            JButton btnQuit = new JButton(“Quit”);
            btnQuit.setLayout(null);
            btnQuit.setBounds(10, 34, 295, 24);
            btnQuit.addActionListener(new btnQuitActionListener());
            getContentPane().add(btnQuit);

          setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        }

      //-Main———————————————————–
        public static void main(String[] args) {
          frame = new SwingGUI();
          frame.setLocation(100, 50);
          frame.setSize(320, 200);
          frame.setTitle(“GUI test”);
          frame.setResizable(false);
          frame.setVisible(true);
        }

    }

//-End——————————————————————

As you can see, nothing special – only a window with two buttons.

At second the ABAP perspective with the code.

/wp-content/uploads/2014/07/002_506417.jpg

“-Begin—————————————————————–
  Report zJavaTest.

  “-Includes————————————————————
    Include zCOMJavaIncl.

  “-Main—————————————————————-
    PerForm RunJava.

  “-Includes————————————————————
    Include zCOMJavaInclSubs.

“-End——————————————————————-

“-Subroutines begin—————————————————–

  “-MyJavaProc———————————————————-
    Form MyJavaProc.

      “-Variables——————————————————-
        Data strClazz Type i.
        Data Args Type i.
        Data Clazz Type i.
        Data Mid Type i.
        Data Control Type i.
        Data ResetControl Type i.
        Data strRes Type i.
        Data ResStr Type String.

      “-Call Java main method——————————————-
        PerForm JNI_FindClass Using ‘java.lang.String’ Changing strClazz.
        PerForm JNI_NewObjectArray Using 1 strClazz 0 Changing Args.
        PerForm JNI_FindClass Using ‘SwingGUI’ Changing Clazz.
        PerForm JNI_GetStaticMethodID Using Clazz ‘main’
          ‘([Ljava.lang.String;)V’ Changing Mid.
        PerForm JNI_CallStaticVoidMethodA Using Clazz Mid Args.

      “-Get methods for event loop————————————–
        PerForm JNI_GetStaticMethodID Using Clazz ‘getstrControl’
          ‘()Ljava.lang.String;’ Changing Control.
        PerForm JNI_GetStaticMethodID Using Clazz ‘resetstrControl’
          ‘()V’ Changing ResetControl.

      “-Event loop——————————————————
        While ResStr <> ‘Quit’.
          PerForm JNI_CallStaticObjectMethodA Using Clazz Control 0
            Changing strRes.
          PerForm Sleep Using 128.
          PerForm GetString Using strRes Changing ResStr.

          “-Events——————————————————
            Case ResStr.

              When ‘PressMe’.

                “-Your PressMe routine here—————————–
                  Call Function ‘POPUP_TO_INFORM’ Exporting
                    titel = ‘PressMe’ txt1 = ‘Button pressed’ txt2 = ”.
                  PerForm JNI_CallStaticVoidMethodA Using Clazz
                    ResetControl 0.

            EndCase.

        EndWhile.

    EndForm.

  “-RunJava————————————————————-
    Form RunJava.

      “-Variables——————————————————-
        Data JVMDLL Type String.
        Data hJVM Type i Value 0.
        Data rc Type i Value 0.

      Create Object oJava ‘COMJAVA’.
      If sy-subrc = 0 And oJava-Type = ‘OLE2’.

        Call Method Of oJava ‘Is64WOW’ = rc.
        If rc <> 0.
          JVMDLL = ‘C:\\Program Files (x86)\\Java\\jre7\\bin\\client\\jvm.dll’.
        Else.
          JVMDLL = ‘C:\\Program Files\\Java\\jre7\\bin\\client\\jvm.dll’.
        EndIf.

        PerForm JVM_SetInitArgsOption Using 1
          ‘-Djava.class.path=C:/Schnell/Dummy’.

        PerForm OpenJavaVM Using JVMDLL Changing rc.
        If rc = JNI_OK.

          “-Call your Java routine————————————–
            PerForm MyJavaProc.

          PerForm CloseJavaVM Changing rc.

        EndIf.

        Free Object oJava.
        System-Call ObjMgr PerForm Garbage Collection.

      EndIf.

    EndForm.

“-Subroutines end——————————————————-

Okay, the coding technique is not really state of the art, but this example show us, how easy it is possible to combine the Java world on the presentation server and the ABAP world on the application server – and all in one IDE.

Last but not least the result, a Java Swing window with different elements, here two buttons, controls the ABAP program.

/wp-content/uploads/2014/07/003_506419.jpg

And with this method it is possible to include the Java source and build the class file on the fly, with the method CreateJavaFile, the property set JavaCompiler and the method CompileJava.

The JNI is not an easy to understand Interface, but if you get used the ways of JNI thinking, it is no more difficult than other interfaces.

Even at the risk of repeating myself, ABAP in Eclipse offers a wide horizont of polyglot programming in one IDE. Now it is not longer necessary to switch between different IDEs. I like it very much 🙂

Cheers

Stefan

To report this post you need to login first.

1 Comment

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

  1. Daniel Ruiz

    Hi Stefan,

    would you have the JNI code in a github or something or willing to share it? – I’d like to try using and testing the stuff of this post at home in my trial system.

    quite cool man.

    Cheers,

    D.

    (0) 

Leave a Reply