Create a Java Client

This example shows how to write a MATLAB® Production Server™ client using the Java® client API. In your Java code, you will:

  • Define a Java interface that represents the MATLAB function.

  • Instantiate a proxy object to communicate with the server.

  • Call the deployed function in your Java code.

To create a Java MATLAB Production Server client application:

  1. Create a new file called MPSClientExample.java.

  2. Using a text editor, open MPSClientExample.java.

  3. Add the following import statements to the file:

    import java.net.URL;
    import java.io.IOException;
    import com.mathworks.mps.client.MWClient;
    import com.mathworks.mps.client.MWHttpClient;
    import com.mathworks.mps.client.MATLABException;
    
  4. Add a Java interface that represents the deployed MATLAB function.

    The interface for the addmatrix function

    function a = addmatrix(a1, a2)
    
    a = a1 + a2;

    looks like this:

    interface MATLABAddMatrix {
           double[][] addmatrix(double[][] a1, double[][] a2)
             throws MATLABException, IOException;
       } 
    

    When creating the interface, note the following:

    • You can give the interface any valid Java name.

    • You must give the method defined by this interface the same name as the deployed MATLAB function.

    • The Java method must support the same inputs and outputs supported by the MATLAB function, in both type and number. For more information about data type conversions and how to handle more complex MATLAB function signatures, see Java Client Programming.

    • The Java method must handle MATLAB exceptions and I/O exceptions.

  5. Add the following class definition:

    public class MPSClientExample
    {
    }

    This class now has a single main method that calls the generated class.

  6. Add the main() method to the application.

    public static void main(String[] args)
    {
    } 
  7. Add the following code to the top of the main() method:

    double[][] a1={{1,2,3},{3,2,1}};
    double[][] a2={{4,5,6},{6,5,4}};

    These statements initialize the variables used by the application.

  8. Instantiate a client object using the MWHttpClient constructor.

    MWClient client = new MWHttpClient();

    This class establishes an HTTP connection between the application and the server instance.

  9. Call the client object's createProxy method to create a dynamic proxy.

    You must specify the URL of the deployable archive and the name of your interface class as arguments:

    MATLABAddMatrix m = client.createProxy(new URL("http://localhost:9910/addmatrix"),
                                           MATLABAddMatrix.class);
    

    The URL value ("http://localhost:9910/addmatrix") used to create the proxy contains three parts:

    • the server address (localhost).

    • the port number (9910).

    • the archive name (addmatrix)

    For more information about the createProxy method, see the Javadoc included in the $MPS_INSTALL/client folder, where $MPS_INSTALL is the name of your MATLAB Production Server installation folder.

  10. Call the deployed MATLAB function in your Java application by calling the public method of the interface.

      double[][] result = m.addmatrix(a1,a2);
  11. Call the client object's close() method to free system resources.

    client.close();
  12. Save the Java file.

    The completed Java file should resemble the following:

    import java.net.URL;
    import java.io.IOException;
    import com.mathworks.mps.client.MWClient;
    import com.mathworks.mps.client.MWHttpClient;
    import com.mathworks.mps.client.MATLABException;
    
    interface MATLABAddMatrix 
      {
        double[][] addmatrix(double[][] a1, double[][] a2)
             throws MATLABException, IOException;
      }
    
    public class MPSClientExample {
        
        public static void main(String[] args){
        
            double[][] a1={{1,2,3},{3,2,1}};
            double[][] a2={{4,5,6},{6,5,4}};
            
            MWClient client = new MWHttpClient();
            
            try{
                MATLABAddMatrix m = client.createProxy(new URL("http://localhost:9910/addmatrix"),
                                                    MATLABAddMatrix.class);
                double[][] result = m.addmatrix(a1,a2);
                
                // Print the magic square
    
                printResult(result);
                
            }catch(MATLABException ex){
            
                // This exception represents errors in MATLAB 
                   System.out.println(ex);            
            }catch(IOException ex){
            
                // This exception represents network issues. 
                   System.out.println(ex);                    
            }finally{
            
                client.close();        
            }
        }
        
        private static void printResult(double[][] result){
            for(double[] row : result){
                for(double element : row){
                    System.out.print(element + " ");
                }
                System.out.println();
            }        
        }
    }
  13. Compile the Java application, using the javac command or use the build capability of your Java IDE.

    For example, enter the following:

    javac -classpath "MPS_INSTALL_ROOT\client\java\mps_client.jar" MPSClientExample.java
    
  14. Run the application using the java command or your IDE.

    For example, enter the following:

    java -classpath .;"MPS_INSTALL_ROOT\client\java\mps_client.jar" MPSClientExample
    

    The application returns the following at the console:

    5.0 7.0 9.0
    9.0 7.0 5.0

Was this topic helpful?