Java内部的SAP GUI脚本

Java内部的SAP GUI脚本,java,sap-gui,Java,Sap Gui,我按照下面的示例从Java编写SAPGUI脚本 即使我传递了参数username和password,我仍然无法从java启动SAP应用程序。我还需要修改什么才能使其正常工作?要从头开始完成登录过程,您需要添加两个操作: 作为新进程启动saplogon.exe 打开与所需sap服务器的连接。对于此步骤,需要在saplogon中配置所需的连接 现在您可以运行代码来执行登录 1的代码: //Opening the SAP Logon //this string should point

我按照下面的示例从Java编写SAPGUI脚本


即使我传递了参数
username
password
,我仍然无法从java启动SAP应用程序。我还需要修改什么才能使其正常工作?

要从头开始完成登录过程,您需要添加两个操作:

  • 作为新进程启动saplogon.exe

  • 打开与所需sap服务器的连接。对于此步骤,需要在saplogon中配置所需的连接

  • 现在您可以运行代码来执行登录

  • 1的代码:

        //Opening the SAP Logon
        //this string should point to the saplogon.exe on your system.
        String sapLogonPath = "C:\\Program Files (x86)\\SAP\\FrontEnd\\SAPgui\\saplogon.exe";
        Process p;
        try {
            p = Runtime.getRuntime().exec(sapLogonPath);
            //the sleep is to let the system wait until the sap logon has fully loaded. You might reduce the time of the sleep depending on the performance of your system
            Thread.sleep(7000);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    代码为2:

            //SAP Connection Name
            //this string is the name of the connection configured in SAP Logon
            String sapConnectionName = "SAPServer"; 
            Connection = new ActiveXComponent(
            GUIApp.invoke("OpenConnection",sapConnectionName).toDispatch());
    
    完整独立工作示例的代码:

    下面是链接中的代码+启动SAP登录、打开连接和用户登录所需的所有修改

    package com.sapguiscripting;
    
    import java.io.IOException;
    
    //-Begin----------------------------------------------------------------
    //-
    //- How to use SAP GUI Scripting inside Java
    //- Example: Logon to an SAP system
    //-
    //-
    //----------------------------------------------------------------------
    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();
    
    
    
        //Opening the SAP Logon
        String sapLogonPath = "C:\\Program Files (x86)\\SAP\\FrontEnd\\SAPgui\\saplogon.exe";
        Process p;
        try {
            p = Runtime.getRuntime().exec(sapLogonPath);
            Thread.sleep(7000);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    
        //-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());
    
    
            //SAP Connection Name
            String sapConnectionName = "SAPServer"; //this is the name of the connection in SAP Logon
            Connection = new ActiveXComponent(
            GUIApp.invoke("OpenConnection",sapConnectionName).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", "SAP*");
    
            //-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", "Down1oad");
    
            //-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", "EN");
    
            //-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------------------------------------------------------------------
    
    package com.sapguiscripting;
    
    import java.io.IOException;
    
    //-Begin----------------------------------------------------------------
    //-
    //- How to use SAP GUI Scripting inside Java
    //- Example: Logon to an SAP system
    //-
    //-
    //----------------------------------------------------------------------
    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();
    
    
    
        //Opening the SAP Logon
        String sapLogonPath = "C:\\Program Files (x86)\\SAP\\FrontEnd\\SAPgui\\saplogon.exe";
        Process p;
        try {
            p = Runtime.getRuntime().exec(sapLogonPath);
            Thread.sleep(7000);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    
        //-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());
    
    
            //SAP Connection Name
            String sapConnectionName = "SAPServer"; //this is the name of the connection in SAP Logon
            Connection = new ActiveXComponent(
            GUIApp.invoke("OpenConnection",sapConnectionName).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", "SAP*");
    
            //-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", "Down1oad");
    
            //-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", "EN");
    
            //-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------------------------------------------------------------------