How do I create a Java component with MATLAB Builder for Java 1.0 (R2006b) that can be used in an Eclipse Java project?

2 views (last 30 days)
I would like to convert MATLAB files into a Java component that can be called from my Eclipse Java project. I would like a complete example that lists the steps necessary to do this.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 28 Jan 2010
This solution explains how to generate a Java component with the MATLAB Builder for Java 1.0 (R2006b) and call it from an Eclipse Java project.
First, create a Java component and call it from an Eclipse Java project:
1. In MATLAB, configure your environment variables as shown here:
web([docroot,'/toolbox/javabuilder/ug/bqp6omv-2.html#bqp8hfx-1'])
2. Compile your MATLAB files into a Java component with DEPLOYTOOL or the MCC command.
3. Add references to mycomponent.jar and $MATLAB\toolbox\javabuilder\jar\javabuilder.jar to your Eclipse project.
4. Instantiate your Java component in your project code.
5. Build and run your project in Eclipse.
Create an example Java component that is called from a Eclipse Java project using these steps. The example is the Magic Square example that appears in the MATLAB Builder for Java documentation:
1. In MATLAB, configure your environment variables and copy the example as shown here:
web([docroot,'/toolbox/javabuilder/ug/bq2ttpe-3.html'])
(The rest of this procedure assumes your working directory is D:\javabuilder_examples, and the Magic Square example files are in D:\javabuilder_examples\magic_square\MagicSquareExample.)
2. Build the Java component as shown here:
web([docroot,'/toolbox/javabuilder/ug/bq2ttpe-5.html'])
This will create a component magicsquare.jar. In our example working directory, it will be located in D:\javabuilder_examples\magic_square\magicsquare\distrib\magicsquare.jar .
3. In Eclipse, go to File->New->Project. In the Project Types pane, select Java->Java Project. Click Next.
4. For Project Name, type MagicSquareExample. In the Contents pane, select Create project from existing source. For directory, choose WORKDIR/MagicSquareExample/MagicDemoJavaApp, where WORKDIR is your working directory. This will include the example driver application provided with the Magic Square example. Click Next.
5. Switch to the Libraries tab. Every project requires references to two JAR-files: the component built using DEPLOYTOOL, and a support library provided by MATLAB Builder for Java.
A. First, click Add External JARs, and select your Builder-generated Java component, for example:
D:\javabuilder_examples\magic_square\magicsquare\distrib\magicsquare.jar
B. Next, click Add External JARs again, and select
$MATLABROOT/toolbox/javabuilder/jar/javabuilder.jar, $MATLABROOT is the MATLAB root directory on your machine, as returned by typing
matlabroot
at the MATLAB Command Prompt.
C. Click Finish.
6. In the Navigator, select your project MagicSquareExample. Select Run->Debug...
7. From the left pane, select Java Application and press the New button to create a new debug configuration. In the right pane, change the Name to MagicSquareExample. Next to Main class, click Search and select getmagic. Click OK, then Apply.
8. The MagicSquare example requires a command-line argument; if you are building an application that does not take command-line arguments, skip this step.
To request a Magic Square of size 3, switch to the Arguments tab, and enter the number "3" in Program Arguments. Click Apply.
9. Click Debug. You should now see the Magic Square output in the Console window:
Magic square of order 3
8 1 6
3 5 7
4 9 2

More Answers (0)

Categories

Find more on Java Package Integration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!