Integrate a Java Package into an Application

This example shows how to invoke a MATLAB® generated method in a Java® application.

To create a Java application that calls a MATLAB generated method:

  1. Install the MATLAB Runtime and generated JAR files in one of the following ways:

    • Run the installer generated by MATLAB. It is located in the for_redistribution folder of the deployment project.

      Doing so automatically installs the MATLAB Runtime from the web and places the generated JAR files onto your computer.

    • Manually install the MATLAB Runtime and the generated JAR files onto your development system.

      You can download the MATLAB Runtime installer from http://www.mathworks.com/products/compiler/mcr. The generated JAR files are located in the MATLAB deployment project's for_testing folder.

  2. In the folder containing the generated JAR files, create a new file called getmagic.java.

  3. Using a text editor, open getmagic.java.

  4. Place the following as the first line in the file.

    import com.mathworks.toolbox.javabuilder.*;
    

    This statement imports the MATLAB support classes.

  5. Place the following line after the first import statement.

    import makesqr.*;
    

    This statement imports the classes generated by the compiler.

  6. Add the following class definition.

    class getmagic
    {
    }

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

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

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

    MWNumericArray n = null;
    Object[] result = null;
    Class1 theMagic = null;
    

    This initializes the variables used by the application.

    • n is an instance of the MATLAB MWNumericArray class that MATLAB uses for its internal data format.

    • result is a generic Java object that holds the results of the call to MATLAB.

    • theMagic is an instance class generated from the MATLAB function.

  9. Add the following code after the variable initialization.

    if (args.length == 0)
    {
      System.out.println("Error: must input a positive integer");
      return;
    }
    

    This is a simple check to ensure that the required command-line argument was passed to the application.

  10. Add a try/catch/finally block after the argument check.

  11. In the try section of the try/catch/finally block, add the following code.

    n = new MWNumericArray(Double.valueOf(args[0]), MWClassID.DOUBLE);
    

    The code instantiates an instance of MWNumericArray and populates it with a 1-by-1 array containing the integer passed to the application on the command line. The value is converted to a Double because that is the most direct mapping between the Java and MATLAB internal data representation.

  12. After the code instantiating the input parameter, add the following to instantiate the class generated from MATLAB.

    theMagic = new Class1();
    

    The constructor for the generated class handles all of the setup required to start the MATLAB Runtime and populate it with the required MATLAB code.

  13. Using the newly instantiated object, call the MATLAB function.

    result = theMagic.makesqr(1, n);
    System.out.println(result[0]);
    
  14. Add the following catch section to the try/catch/finally block to handle any exceptions that might be thrown.

    catch (Exception e)
    {
      System.out.println("Exception: " + e.toString());
    }
    
  15. Add the following finally section to the try/catch/finally block to clean up any resources.

    finally
    {
      MWArray.disposeArray(n);
      MWArray.disposeArray(result);
      theMagic.dispose();
    }
    

    The disposeArray() and dispose() methods clean up the resources used by the generated MATLAB code.

  16. Save the Java file.

    The completed Java file should resemble the following.

    import com.mathworks.toolbox.javabuilder.*;
    import makesqr.*;
    
    class getmagic
    {
       public static void main(String[] args)
       {
          MWNumericArray n = null;
          Object[] result = null;
          Class1 theMagic = null;
    
          if (args.length == 0)
          {
            System.out.println("Error: must input a positive integer");
            return;
          }
    
          try
          {
             n = new MWNumericArray(Double.valueOf(args[0]),
                                          MWClassID.DOUBLE);
    
             theMagic = new Class1();
    
             result = theMagic.makesqr(1, n);
             System.out.println(result[0]);
          }
          catch (Exception e)
          {
             System.out.println("Exception: " + e.toString());
          }
          finally
          {
             MWArray.disposeArray(n);
             MWArray.disposeArray(result);
             theMagic.dispose();
          }
       }
    }
    
  17. Use the system's command line to navigate to the folder where you installed the generated Java package and saved the new Java file.

  18. Compile the Java application using javac.

    javac -classpath "mcrroot\toolbox\javabuilder\jar\javabuilder.jar";
           .\makesqr.jar .\getmagic.java

      Note:   Enter the javac command on a single line.

      On UNIX® platforms, use colon (:) as the class path delimiter instead of semicolon (;).

    mcrroot is the path to where the MATLAB Runtime is installed on your system. If you have MATLAB installed on your system instead, you can use the path to your MATLAB installation.

  19. From the system's command prompt, run the application.

    java -classpath .;"mcrroot\toolbox\javabuilder\jar\javabuilder.jar";
         .\makesqr.jar getmagic 5
        17    24     1     8    15
        23     5     7    14    16
         4     6    13    20    22
        10    12    19    21     3
        11    18    25     2     9

    You must be sure to place a dot (.) in the first position of the class path. If it not, you get a message stating that Java cannot load the class.

      Note:   Enter the java command on a single line.

      On UNIX platforms, use colon (:) as the class path delimiter instead of semicolon (;).

    mcrroot is the path to where the MATLAB Runtime is installed on your system. If you have MATLAB installed on your system instead, you can use the path to your MATLAB installation.

To follow up on this example:

  • Try installing the new application on a different computer.

  • Try building an installer for the application.

  • Try integrating a package that consists of multiple functions.

Was this topic helpful?