Skip to Content
Technical Articles
Author's profile photo Stefan Schnell

How to use SAP GUI Scripting inside Java

The SAP GUI Scripting bases on Windows OLE resp. ActiveX technic. In a normal case Java do not work with this platform dependent technic. Under this circumstances it is not possible to use SAP GUI Scripting with Java.

But there is a very good project on sourceforge.net which offers this possibility, it call JACOB – this means Java-COM Bridge. You find JACOB here.

With JACOB it is very easy to use SAP GUI Scripting inside Java, as the following examples shows.

The first example SAPGUIScriptingVersion.java shows a message box with the SAP GUI Scripting version. The second example SAPGUIScriptingLogon.java shows a complete automatic logon on an SAP system. Both sources includes the equivalent VBScript commands, from the SAP GUI Script Recorder, as comments.

Now you can easiely integrate your SAP GUI Scripting code inside your Java sources.

//-Begin----------------------------------------------------------------
//-
//- How to use SAP GUI Scripting inside Java
//- Example: Get version of SAP GUI Scripting
//-
//- Author: Stefan Schnell
//-
//----------------------------------------------------------------------

  //-Import-------------------------------------------------------------
    import com.jacob.activeX.ActiveXComponent;
    import com.jacob.com.ComThread;
    import com.jacob.com.Dispatch;
    import com.jacob.com.Variant;
    import javax.swing.*;

  public class SAPGUIScriptingVersion {

    public static void main(String[] args) {

      //-Variables------------------------------------------------------
        ActiveXComponent SAPROTWr, GUIApp;
        Dispatch ROTEntry;
        String MajorVersion, MinorVersion;
        Variant ScriptEngine;
         
      ComThread.InitSTA();

      //-Set SapGuiAuto = GetObject("SAPGUI")---------------------------
        SAPROTWr = new ActiveXComponent("SapROTWr.SapROTWrapper");
        try {
          ROTEntry = SAPROTWr.invoke("GetROTEntry", "SAPGUI").toDispatch();
          //-Set application = SapGuiAuto.GetScriptingEngine------------
            ScriptEngine = Dispatch.call(ROTEntry, "GetScriptingEngine");
            GUIApp = new ActiveXComponent(ScriptEngine.toDispatch());

          //-Get version of SAP GUI Scripting---------------------------

            //-MajorVersion = application.MajorVersion()----------------
              MajorVersion = GUIApp.getProperty("MajorVersion").toString();
            //-MinorVersion = application.MinorVersion()----------------
              MinorVersion = GUIApp.getProperty("MinorVersion").toString();

            //-MsgBox "SAP GUI Scripting " & MajorVersion & "." & _
            //   MinorVersion-------------------------------------------
              JOptionPane.showMessageDialog(null, "SAP GUI Scripting " +
                MajorVersion + "." + MinorVersion);
      }

      catch (Exception e) {

      }

      finally {
        ComThread.Release();
        System.exit(0);
      }

    }

  }

//-End------------------------------------------------------------------

SAPGUIScripting.jpg

//-Begin----------------------------------------------------------------
//-
//- How to use SAP GUI Scripting inside Java
//- Example: Logon to an SAP system
//-
//- Author: Stefan Schnell
//-
//----------------------------------------------------------------------

  //-Import-------------------------------------------------------------
    import com.jacob.activeX.ActiveXComponent;
    import com.jacob.com.ComThread;
    import com.jacob.com.Dispatch;
    import com.jacob.com.Variant;

  public class SAPGUIScriptingLogon {

    public static void main(String[] args) {

      //-Variables------------------------------------------------------
        ActiveXComponent SAPROTWr, GUIApp, Connection, Session, Obj;
        Dispatch ROTEntry;
        Variant ScriptEngine;
         
      ComThread.InitSTA();

      //-Set SapGuiAuto = GetObject("SAPGUI")---------------------------
        SAPROTWr = new ActiveXComponent("SapROTWr.SapROTWrapper");
        try {
          ROTEntry = SAPROTWr.invoke("GetROTEntry", "SAPGUI").toDispatch();
          //-Set application = SapGuiAuto.GetScriptingEngine------------
            ScriptEngine = Dispatch.call(ROTEntry, "GetScriptingEngine");
            GUIApp = new ActiveXComponent(ScriptEngine.toDispatch());

          //-Set connection = application.Children(0)-------------------
            Connection = new ActiveXComponent(
              GUIApp.invoke("Children", 0).toDispatch());
          //-Set session = connection.Children(0)-----------------------
            Session = new ActiveXComponent(
              Connection.invoke("Children", 0).toDispatch());

          //-Set GUITextField Client------------------------------------
          //-
          //- session.findById("wnd[0]/usr/txtRSYST-MANDT").text = "000"
          //-
          //------------------------------------------------------------
            Obj = new ActiveXComponent(Session.invoke("FindById",
              "wnd[0]/usr/txtRSYST-MANDT").toDispatch());
            Obj.setProperty("Text", "000");

          //-Set GUITextField User--------------------------------------
          //-
          //- session.findById("wnd[0]/usr/txtRSYST-BNAME").text = _
          //-   "BCUSER"
          //-
          //------------------------------------------------------------
            Obj = new ActiveXComponent(Session.invoke("FindById",
              "wnd[0]/usr/txtRSYST-BNAME").toDispatch());
            Obj.setProperty("Text", "BCUSER");

          //-Set GUIPasswordField Password------------------------------
          //-
          //- session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = _
          //-   "minisap"
          //-
          //------------------------------------------------------------
            Obj = new ActiveXComponent(Session.invoke("FindById",
              "wnd[0]/usr/pwdRSYST-BCODE").toDispatch());
            Obj.setProperty("Text", "minisap");

          //-Set GUITextField Language----------------------------------
          //-
          //- session.findById("wnd[0]/usr/txtRSYST-LANGU").text = "DE"
          //-
          //------------------------------------------------------------
            Obj = new ActiveXComponent(Session.invoke("FindById",
              "wnd[0]/usr/txtRSYST-LANGU").toDispatch());
            Obj.setProperty("Text", "DE");

          //-Press enter------------------------------------------------
          //-
          //- session.findById("wnd[0]").sendVKey 0
          //-
          //------------------------------------------------------------
            Obj = new ActiveXComponent(Session.invoke("FindById",
              "wnd[0]").toDispatch());
            Obj.invoke("sendVKey", 0);

      }

      catch (Exception e) {

      }

      finally {
        ComThread.Release();
        System.exit(0);
      }

    }

  }

//-End------------------------------------------------------------------

Assigned Tags

      11 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hello Stefan,

      Great topic.

      Could you please help me about a little project. I want execute a script in tcode DB02 (to see tablespace) by JAVA, and I cant adapt the code (with Jacob), I need expand node.

      This code below is SAPGUI Scripting :

      If Not IsObject(application) Then

         Set SapGuiAuto  = GetObject("SAPGUI")

         Set application = SapGuiAuto.GetScriptingEngine

      End If

      If Not IsObject(connection) Then

         Set connection = application.Children(0)

      End If

      If Not IsObject(session) Then

         Set session    = connection.Children(0)

      End If

      If IsObject(WScript) Then

         WScript.ConnectObject session,     "on"

         WScript.ConnectObject application, "on"

      End If

      session.findById("wnd[0]").resizeWorkingPane 173,25,false

      session.findById("wnd[0]/shellcont[1]/shell/shellcont[1]/shell").expandNode "       1008"

      session.findById("wnd[0]/shellcont[1]/shell/shellcont[1]/shell").topNode = "       1006-"

      session.findById("wnd[0]/shellcont[1]/shell/shellcont[1]/shell").selectItem "         47","Task"

      session.findById("wnd[0]/shellcont[1]/shell/shellcont[1]/shell").ensureVisibleHorizontalItem "         47","Task"

      session.findById("wnd[0]/shellcont[1]/shell/shellcont[1]/shell").doubleClickItem "         47","Task"

      Regards

      Paulo Acácio

      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Paulo,

      sure, here the working code.

      Enjoy it.

      Cheers

      Stefan

      P.S. Be sure that you use the 32-bit version of Java. I try it with actual release 1.8.0_60.

      //-Begin----------------------------------------------------------------
      //-
      //- How to use SAP GUI Scripting inside Java
      //- Example: Use TAC DB02 and open a node
      //-
      //- Author: Stefan Schnell
      //-
      //----------------------------------------------------------------------
      
        //-Import-------------------------------------------------------------
          import com.jacob.activeX.ActiveXComponent;
          import com.jacob.com.ComThread;
          import com.jacob.com.Dispatch;
          import com.jacob.com.Variant;
      
        public class SAPGUIScriptingDB02 {
      
          public static void main(String[] args) {
      
            //-Variables------------------------------------------------------
              ActiveXComponent SAPROTWr, GUIApp, Connection, Session, Obj;
              Dispatch ROTEntry;
              Variant ScriptEngine;
      
            ComThread.InitSTA();
      
            //-Set SapGuiAuto = GetObject("SAPGUI")---------------------------
              SAPROTWr = new ActiveXComponent("SapROTWr.SapROTWrapper");
              try {
                ROTEntry = SAPROTWr.invoke("GetROTEntry", "SAPGUI").toDispatch();
                //-Set application = SapGuiAuto.GetScriptingEngine------------
                  ScriptEngine = Dispatch.call(ROTEntry, "GetScriptingEngine");
                  GUIApp = new ActiveXComponent(ScriptEngine.toDispatch());
      
                //-Set connection = application.Children(0)-------------------
                  Connection = new ActiveXComponent(
                    GUIApp.invoke("Children", 0).toDispatch());
                //-Set session = connection.Children(0)-----------------------
                  Session = new ActiveXComponent(
                    Connection.invoke("Children", 0).toDispatch());
      
                //- ssession.findById("wnd[0]/tbar[0]/okcd").text = "/ndb02"
                  Obj = new ActiveXComponent(Session.invoke("FindById",
                    "wnd[0]/tbar[0]/okcd").toDispatch());
                  Obj.setProperty("Text", "/ndb02");
      
                //- session.findById("wnd[0]").sendVKey 0
                  Obj = new ActiveXComponent(Session.invoke("FindById",
                    "wnd[0]").toDispatch());
                  Obj.invoke("sendVKey", 0);
      
                //- session.findById("wnd[0]/shellcont[1]/shell/shellcont[1]/shell").expandNode "       1008"
                  Obj = new ActiveXComponent(Session.invoke("FindById",
                    "wnd[0]/shellcont[1]/shell/shellcont[1]/shell").toDispatch());
                  Obj.invoke("expandNode", "       1008");
      
                //- session.findById("wnd[0]/shellcont[1]/shell/shellcont[1]/shell").topNode = "       1009-"
                  Obj.setProperty("topNode", "       1009-");
      
                //- session.findById("wnd[0]/shellcont[1]/shell/shellcont[1]/shell").selectItem "         47", "Task"
                  Variant[] arguments = {new Variant("         47"), new Variant("Task")};
                  Obj.invoke("selectItem", arguments);
      
                //- session.findById("wnd[0]/shellcont[1]/shell/shellcont[1]/shell").ensureVisibleHorizontalItem "         47", "Task"
                  Obj.invoke("ensureVisibleHorizontalItem", arguments);
      
                //- session.findById("wnd[0]/shellcont[1]/shell/shellcont[1]/shell").doubleClickItem "         47", "Task"
                  Obj.invoke("doubleClickItem", arguments);
      
            }
      
            catch (Exception e) {
      
            }
      
            finally {
              ComThread.Release();
              System.exit(0);
            }
      
          }
      
        }
      
      //-End------------------------------------------------------------------
      
      Author's profile photo Former Member
      Former Member

      Hello Stefan,

      Thank you about the code. We start a little project about a program, this program will check many SAP transaction and save in document word the printscreen each transaction and the status, like SMLG -> Response time OK, DB02-> Tablespace Critical Size. In Brazil customers like to have the evidence in all daily checklist. Our program needs to read the transaction to inform us  the status of OK or not OK and put printscreen.

      All development is being based on Java. We have something 50% ready in Visual Studio, but decided to migrate everything to JAVA.

      Your post helped us a lot because we're all SAP BASIS consultant and know little about programming.

      Regards

      Paulo Acácio

      T-Systems do Brasil.

      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Paulo,

      thanks for your reply and good luck to your project - it sounds very interesting.

      If you have further questions, let me know.

      Cheers

      Stefan

      Author's profile photo Hasmukh Ginoya
      Hasmukh Ginoya

      Hello Stefan,

      I want to integrate SAP Same thing using ComfyJ ( Java Access Bridge).

      have you provide guidance how can i do that ? Because in your sample you have done it with

      Jacob java com access bridge. May i use the same thing using ComfyJ ?
      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Hasmukh,

      sorry, I don't know ComfyJ, so it isn't possible for me to help you.

      Best regards
      Stefan

       

      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello community,

      I check the sources above with the actual Java release 1.8.0_92 and the actual SAP GUI for Windows and all works well.

      /wp-content/uploads/2016/06/001_971141.jpg

      Hint: If you are working in an x64 Windows environment with Eclipse you can add an x86 JRE in the preferences.

      /wp-content/uploads/2016/06/002_971142.jpg

      Now set in the properties of your Java project the alternative JRE.

      /wp-content/uploads/2016/06/003_971143.jpg

      Cheers

      Stefan

      Author's profile photo Former Member
      Former Member

      Hi,

      Is there any way to make a programmed SAP GUI for Java connection on Linux/Mac (without the ActiveX/OLE objects)?

      Thanks

      Author's profile photo Dhinesh Ram K
      Dhinesh Ram K

      Hi Stefan,

      Awesome solution, but sadly i am unable to solve my problem. Below is what i did

      1. Download & Unzip : jacob-1.19.zip
      2. Placed in my Project folder, Imported the jacob.jar into Eclipse
      3. Placed the jacob-1.19-x64.dll in C:\Program Files\Java\jre1.8.0_212\bin

      But I am getting below exception:

      Exception in thread "main" com.jacob.com.ComFailException: Can't co-create object
      at com.jacob.com.Dispatch.createInstanceNative(Native Method)
      at com.jacob.com.Dispatch.<init>(Dispatch.java:99)
      at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java:58)
      at Test1.main(Test1.java:26)

      Line #26 :

      SAPROTWr = new ActiveXComponent("SapROTWr.SapROTWrapper");

      Based on Googling i came to know that this Exception raises because of mixed usage of x86 and x64.

      • My System: Win 10 (x64)
      • Installed JRE 8 (x64)
      • Eclipse (x64)
      • SAP GUI for Windows (x32)  <- is the error causing because of this !? Am not sure, am unable to figure

      Kindly assist me on where am i going wrong.

      Email: k.dhineshram@gmail.com

      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Dhinesh,

      it is not possible in standard to call the SAP Running Object Table (ROT) wrapper in x64 environments. Use x86 JRE as I described above and it should work.

      Best regards
      Stefan

      Author's profile photo Tiago Gomes
      Tiago Gomes

      Hi!

       

      I followed your code but I'm getting a null pointer exception in the following line:

      GUIApp = new ActiveXComponent(ScriptEngine.toDispatch());
      

      Apparently, ScriptEngine is null. Any ideas what the problem might be?

       

      Thanks in advace.