This example demonstrates how to display a plot created by a Java® servlet
calling a class created with the MATLAB®
Compiler SDK™ product over
a Web interface. This example uses MATLAB varargin
and varargout
for
optional input and output to the varargexample.m
function.
For more information about varargin
and varargout
,
see Specify Optional Arguments.
This section describes what you need to know and do before you create the Web deployment example.
The following products must be installed at their recommended release levels.
MATLAB, MATLAB Compiler™, MATLAB Compiler SDK
Java Development Kit (JDK™)
Ensure you have a JDK installed on your system. You can download it from Oracle, Inc.
You should be using the same version of Java that ships with MATLAB. To find out what version of Java MATLAB is running, enter the following MATLAB command:
version -java
Caution:MathWorks® only supports the Oracle® JDK and JRE™. A certain measure of cross-version compatibility resides in the Oracle software and it may be possible to run applications with compiled MATLAB functions with non-Oracle JDKs under some circumstances—however, compatibility is not guaranteed.
In order to run this example, your Web server must be capable of running accepted Java frameworks like J2EE. Running the WebFigures example (Implement a WebFigure) also requires the ability to run servlets in WARs (Web Archives).
Ensure that the javabuilder.jar
library (
)
has been installed into your Web server’s common library folder.matlabroot
/toolbox/javabuilder/jar/javabuilder.jar
The example files are located in the
folder.matlabroot
\toolbox\javabuilder\Examples\java_web_vararg_demo
The example files contain the following three folders:
mcode
— Contains all of
the MATLAB source code.
JavaCode
— Contains the
required Java files and libraries.
compile
— Contains some
helpful MATLAB functions to compile and clean up the example.
As an alternative to compiling the example code manually and
creating the application WAR (Web Archive) manually, you can run compileVarArgServletDemo.m
in
the compile folder. If you choose this option and want to change the
locations of the output files, edit the values in getVarArgServletDemoSettings.m
.
If you choose to run compileVarArgServletDemo.m
,
consult the readme
file in the download for additional
information and then skip to Running the Web Deployment Example.
Copy the folder java_web_vararg_demo
containing
example files to a local folder. Failure to do so may lead to errors.
Build your Java package by compiling your code into a deployable .jar
file.
Open the Library Compiler app.
Create the Java package using the Library Compiler app to build a Java class that wraps around your MATLAB code.
To compile the Java package using the Library Compiler app, use the following information as you work through this example in Compile Java Packages with Library Compiler App:
Project Name | vararg_java |
Class Name | vararg_javaclass |
File to compile | varargexample.m |
Use javac
to compile the Java source
file VarArgServletClass.java
from example folder JavaCode\src\VarArg
.
javac.exe
should be located in the bin folder
of your JDK installation.
Ensure your classpath
is set to include:
javabuilder.jar
— included
with MATLAB
Compiler SDK
vararg_java.jar
— the JAR
file you just built
servlet-api.jar
— included
as part of the servlet container
For more details about using javac
, see the Oracle website.
Web archive or WAR files are a type of Java Archive used
to deploy J2EE and JSP servlets. To run this example you will need
to use the jar
command to generate the final WAR
file that runs the application. To do this, follow these steps:
Copy the JAR file created using the MATLAB
Compiler SDK product
into the JavaCode\build\WEB-INF\classes\VarArg
example
folder.
Copy the compiled Java class to the JavaCode\build\WEB-INF\classes\VarArg
example
folder.
From the folder JavaCode
, use the jar
command
to generate the final WAR as follows:
jar cf VarArgServlet.war -C build .
Don't omit the .
parameter above, which denotes
the current working folder.
jar
command,
refer to the Oracle Web
site.
When you're ready to run the application, do the following:
Install the VarArgServlet.war
file
into your Web server’s webapps
folder.
Run the application by entering http://localhost:
in
the address field of your Web browser, where port_number
/VarArgServlet
is
the port that your Web server is configured to use (usually port_number
8080
).
To use the application, do the following on the http://localhost/VarArgServlet
Web
page:
Enter any amount of numbers to plot in the Data to Plot field.
Select Line Color and Border
Color using the Optional Input drop-down
lists. Note that these optional inputs are passed as varargin
to
the compiled MATLAB code.
Select additional information you want to output,
such as mean and standard deviation, by clicking an option in the Optional
Output area. Note that these optional outputs are set as varargout
from
the compiled MATLAB code.
Click Display Plot. Example output is shown below using the Mean optional output.