Java and SAP Business One SDK DI API Tutorial (Part Three)

In this part we will finish our first simple project with a program that connects to SAP Business One through DI API.

Once we’ve got our IDE set up to use DI API libraries, we can start coding. The first thing that we need is to import the DI API library for use in our code:

Next, we need to declare Company object that we’ll be using to connect to server and create a new Company instance.

So far, we’ve got this:

Now we can start setting up all the connection parameters required to successfully connect to SAP Business One server.

Once we have that ready, we can attempt to connect.

If the connection is successful, the server will return 0.  We will then use getCompanyName() method to display the name of the company we just connected to, together with a little message that everything went all right. Once that’s done, we will disconnect from the server.

If the connection fails, the server will return non-zero integer. In that case, we will use SBOErrorMessage object to retrieve the error code and the error message.

That’s it. You should now have a working Java program that can connect to SAP Business One Server.

Here’s the full program:

You may also like...

19 Responses

  1. Michal says:

    Nice beginning. Thanks a lot. I am looking forward for more!

  2. Diego Gutierrez says:

    Great tutorial for beginners, thanks!

  3. Larry says:

    can you give me some document of DI API and like jdk api

    • Rafał Adam Rozmus says:

      Hi Larry, all the documentation for DI API can be found in the SDK package on your installation media.
      Once installed, go to: C:\Program Files (x86)\SAP\SAP Business One SDK\Help

  4. W.M. says:

    I have followed the tutorial but I am getting this strange error message when compiling the application:


    com.sap.smb.sbo.wrapper.com.ComFailException: Can't map name to dispid: Server
    at com.sap.smb.sbo.wrapper.com.Dispatch.invokev(Native Method)
    at com.sap.smb.sbo.wrapper.com.Dispatch.invokev(Unknown Source)
    at com.sap.smb.sbo.wrapper.com.Dispatch.invoke(Unknown Source)
    at com.sap.smb.sbo.wrapper.com.Dispatch.put(Unknown Source)
    at com.sap.smb.sbo.api.Company.setServer(Unknown Source)

    Caused by this line: company.setServer(“(local)”);

    • Rafał Adam Rozmus says:

      Instead of company.setServer(“(local)”); try the actual computer name of the machine with the SAP Server. Alternatively, try the IP address.

      Something like:
      company.setServer(“MY_PC_NAME”);
      or if you ‘re trying to run this code on the same machine as your SAP server:
      company.setServer(“localhost”);
      company.setServer(“127.0.0.1”);

      I hope that helps.

      • W.M. says:

        Thansk Rafal. I have already tried all address options. I am trying to connect to a remote SAP server using the public IP. I have already set port forwarding to it on 1433, 30000, 30001. The strange thing there on the local network it’s working but here from a distance doesn’t work. Keep getting server type not supported. Installed the SQL server client library 2008 on my W10 machine but that doesn’t work either. Disabled firewall locally and remotely. Noting is working. Even if I change the ip to something else completely I get the same error message so I believe it is not connecting to whatsoever. The error is raised before connect attempt only due to this “BoDataServerTypes.dst_MSSQL2008”.

        • Rafał Adam Rozmus says:

          If the code is running fine on the local network, then it looks like you might have some configuration problems on the server / firewall. Check out the link below on how to configure SQL Server for accessing it remotely: https://blog.citrix24.com/configure-sql-express-to-accept-remote-connections/

          You should try if you can connect with SQL Management Studio. Also it would be worth to check if the ports are actually open and accessible from outside. You can do it with telnet:

          telnet SERVERNAME PORTNUMBER

          • W.M. says:

            On server machine or other machines on the same local network I need to specify the MS network PC NAME as server address “SAP-SERVER” in both setLicenseServer and setServer. Using local IP address or 127.0.0.1 does not work, only server name. So, I guess when connecting from a remote machine (from public internet) the “SAP-SERVER” name does not translate to something meaningful for SAP.

  5. cristian says:

    hello, how can I invite an object of the api

  6. Rohit Raj says:

    Hi Rafat thanks for the tutorial. i have every thing according to steps you showed us but after running java class i am getting this error please help me in thi issue.

    Exception in thread “main” java.lang.UnsatisfiedLinkError: no sbojni in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)
    at com.sap.smb.sbo.wrapper.util.WrapperUtil.loadSBOJNILibrary(Unknown Source)
    at com.sap.smb.sbo.wrapper.com.Dispatch.(Unknown Source)
    at com.sap.smb.sbo.api.SBOCOMUtil.newCompany(Unknown Source)
    at main.SAPBusinessOneTutorial.main(SAPBusinessOneTutorial.java:15)

    • Rafał Adam Rozmus says:

      Hi,

      You can try reinstalling SAP B1 DI API. Please also check that the architectures for DI API and JDK are the same. What I mean is, if you have 32-bit DI API, you need to use 32-bit JDK.

      You can also try adding
      -Djava.library.path=”C:\Program Files\SAP\SAP Business One DI API\JCO\LIB” in your VM Options in project properties.

  7. Rohit Raj says:

    Thanks Rafat for the reply, we haven’t install SAP B1 DI API i only got LIB file in which all dll file and jar File was there so i have used it directly into My IDE, so my question is that will it work or not as My machine is 64- bit and DI API is of 32 -bit which is not installed as i am using it directly. Is it necessary to install DI API.

    • Rafał Adam Rozmus says:

      Hi,

      You need SAP B1 DI API installed on the machine. Just the LIB folder will not be enough. You can install 32-bit API on 64-bit machine, that’s not a problem. If you use 32-bit DI API, you also need 32-bit JDK.

  8. Saud Khan says:

    Hi Rafal , i have configured all these step by step but after running the class i am getting error: “Exception in thread “main” com.sap.smb.sbo.wrapper.com.ComFailException: Can’t find moniker”

    on checking didn’t got any appropriate answer can you help me with this like what is this issue and how to resolve this?

  9. Saud Khan says:

    Hi Rafal , i have done each and every step also configured all the libraries still after running the class getting error “Exception in thread “main” com.sap.smb.sbo.wrapper.com.ComFailException: Can’t find moniker” , dont know what it is ? and how to resolve it researched every where but didnt got any answer , please help me with this.

    • Rafał Adam Rozmus says:

      Hi,
      First, uninstall all SAP DI API versions from your machine and remove the SAP DI API folders from Program Files. If you use 64-bit JDK, install 64-bit DI API. If you use 32-bit JDK, install 32-bit DI API. Don’t put both 32 and 64-bit DI API on same machine. For some reason it creates problems.

      You can also try adding the path to your Java libraries in the project’s VM Options:
      -Djava.library.path=”C:\Program Files\SAP\SAP Business One DI API\JCO\LIB”

      I’m not 100% sure that it’s going to solve your problem, but it’s worth trying.

Leave a Reply

Your email address will not be published. Required fields are marked *