<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to MatlabJavaInterface</title><link>https://sourceforge.net/p/odtbx/wiki/MatlabJavaInterface/</link><description>Recent changes to MatlabJavaInterface</description><atom:link href="https://sourceforge.net/p/odtbx/wiki/MatlabJavaInterface/feed" rel="self"/><language>en</language><lastBuildDate>Tue, 08 Jul 2014 17:34:12 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/odtbx/wiki/MatlabJavaInterface/feed" rel="self" type="application/rss+xml"/><item><title>MatlabJavaInterface modified by &lt;REDACTED&gt;</title><link>https://sourceforge.net/p/odtbx/wiki/MatlabJavaInterface/</link><description>&lt;div class="markdown_content"&gt;&lt;h1 id="details-on-the-matlab-java-interface"&gt;Details on the MATLAB-Java Interface&lt;/h1&gt;
&lt;p&gt;This page describes interesting tidbits when working across the MATLAB-java interface, including the steps needed to debug Java code from a running MATLAB session. &lt;/p&gt;
&lt;h2 id="setting-up-a-debugging-session-with-eclipse"&gt;Setting Up A Debugging Session With Eclipse&lt;/h2&gt;
&lt;p&gt;(Borrowed from: &lt;a href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/155261" rel="nofollow"&gt;http://www.mathworks.com/matlabcentral/newsreader/view_thread/155261&lt;/a&gt; ) &lt;/p&gt;
&lt;p&gt;&lt;em&gt;These notes apply to MATLAB R2008a, R2009b and Eclipse 3.4.0 (Ganymede).&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;1. Create a Eclipse project for JAT. Its should include the source code of your java classes. The .class files should be created next to the source files. &lt;/p&gt;
&lt;p&gt;2. Build JAT .class files and make sure those locations are in the MATLAB path. Be sure that when you build your .class files that all the debugging information is included in your build options. (ex: &lt;em&gt;javac -g ...&lt;/em&gt;) &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The compiler compliance level settings in Eclipse should match the version of the JRE supplied with the target versions of MATLAB or an earlier version JRE that is common to all platforms. The version of the JRE used by Matlab can be determined by using the following Matlab command: &lt;br /&gt;
&amp;gt;&amp;gt; version - java &lt;br /&gt;
For example, not all MATLAB R2008a pltforms come with same JVM version. Vista can have 1.6 but Macs currently have only 1.5. Set the Eclipse compiler compliance level to 1.5 so that all platforms are supported and debuggable. &lt;/p&gt;
&lt;p&gt;3. Create the java.opts file in your "startup" directory. Alternatively you could place java.opts in the "%MATLAB_ROOT%\bin\arch" directory but this has been seen not to work in all cases. This java.opts file is a standard text file. Choose a port number, e.g. 5678, that won't collide with any other application on your machine. You may have to open or allow this port to be used with your machine's firewall. &lt;/p&gt;
&lt;p&gt;java.opts: &lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;agentlib&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;jdwp&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;transport&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;dt_socket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5678&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;suspend&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;Djava&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compiler&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;NONE&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note, the "-Djava.compiler=NONE" is optional. It turns off the Just-In-Time (JIT) compiler which translates the Java bytecode into native machine code. Generally the JIT offers significant speed improvement but it may reduce the amount of debugging information. Try it without first but if you see "strangeness" with your debugger place it back into the java.opts file and restart MATLAB to see if it fixes debugger quirks. Also remember to remove this line if you aren't debugging since it affects performance. &lt;/p&gt;
&lt;p&gt;4. In your eclipse project, define a new debug configuration. Create a new &lt;em&gt;Remote Java Application&lt;/em&gt;. Give it a good name, like &lt;em&gt;MATLAB&lt;/em&gt; or something useful. Make sure the appropriate Project is set. The &lt;em&gt;Connection Type&lt;/em&gt; should be &lt;em&gt;Standard (Socket Attach)&lt;/em&gt;. The &lt;em&gt;Host&lt;/em&gt; should be &lt;em&gt;localhost&lt;/em&gt; and the port should match the java.opts file above, e.g. &lt;em&gt;5678&lt;/em&gt;. &lt;/p&gt;
&lt;p&gt;5. Launch MATLAB. Check, and set if required, the java classpath for the code in the Eclipse project to be debugged. MATLAB's java static or dynamic path can be used. &lt;/p&gt;
&lt;p&gt;6. Then debug the "Remote Java Application" on the Eclipse side. Put a break point in your java code and instantiate or execute your java code from within MATLAB. You should see the breakpoint popping up when MATLAB calls java. If you see &lt;strong&gt;connection refused&lt;/strong&gt; then there are a couple of problems: &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;MATLAB didn't find the java.opts file on startup, &lt;/li&gt;
&lt;li&gt;The JVM couldn't read or parse the java.opts file, &lt;/li&gt;
&lt;li&gt;Your machine's firewall is preventing the socket from being created, &lt;/li&gt;
&lt;li&gt;Your machine's firewall is preventing the socket from being accessed. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You can even place breakpoints in Eclipse and MATLAB at the same time. To stop the java debugging just detach the Eclipse debugger from the MATLAB JVM. In Eclipse, highlight the &lt;em&gt;&lt;span&gt;&lt;span&gt;[Remove Java Application]&lt;/span&gt;&lt;/span&gt;&lt;/em&gt; in the Debug processes/threads view and either use &lt;em&gt;Terminate&lt;/em&gt; or the &lt;em&gt;Disconnect&lt;/em&gt; from the Run menu or debug controls. &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Parallel Processing Toolbox Limitation:&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;
This can intefere with the &lt;strong&gt;matlabpool&lt;/strong&gt; and other parallel processing toolbox functions. When the multiple labs are spawned each could use the same java.otps file and subsequent labs attempt to open the same port. Setting the &lt;strong&gt;address=0&lt;/strong&gt; will get around this problem by allowing each MATLAB instance (lab) to open its own port. The downside is that you will have to externally determine ('netcat'?) what ports these MATLAB instances are listening on in order to connect via Eclipse and adjust the Eclipse debug parameters accordingly. &lt;/p&gt;
&lt;h2 id="saving-and-loading-java-objects-from-matlab"&gt;Saving and Loading Java Objects from MATLAB&lt;/h2&gt;
&lt;p&gt;When running JAT, if you attempt to save your MATLAB workspace you will likely get an error such as:&lt;br /&gt;
&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;whos&lt;/span&gt;
  &lt;span class="n"&gt;Name&lt;/span&gt;           &lt;span class="n"&gt;Size&lt;/span&gt;            &lt;span class="n"&gt;Bytes&lt;/span&gt;  &lt;span class="n"&gt;Class&lt;/span&gt;                                    &lt;span class="n"&gt;Attributes&lt;/span&gt;

  &lt;span class="n"&gt;o&lt;/span&gt;              &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="n"&gt;x1&lt;/span&gt;              &lt;span class="mi"&gt;5462&lt;/span&gt;  &lt;span class="k"&gt;struct&lt;/span&gt;                                             
  &lt;span class="n"&gt;y&lt;/span&gt;              &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="n"&gt;x1&lt;/span&gt;                    &lt;span class="n"&gt;jat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;matlabInterface&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ODToolboxJATModel&lt;/span&gt;              
  &lt;span class="n"&gt;ytostirng&lt;/span&gt;      &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="n"&gt;x45&lt;/span&gt;               &lt;span class="mi"&gt;90&lt;/span&gt;  &lt;span class="kt"&gt;char&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;save&lt;/span&gt; &lt;span class="n"&gt;wstest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mat&lt;/span&gt;
&lt;span class="nl"&gt;Warning:&lt;/span&gt; &lt;span class="n"&gt;jat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;matlabInterface&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ODToolboxJATModel&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="n"&gt;c6ed8&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;serializable&lt;/span&gt;
&lt;span class="nl"&gt;Warning:&lt;/span&gt; &lt;span class="n"&gt;Variable&lt;/span&gt; &lt;span class="sc"&gt;'y'&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;jat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;matlabInterface&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ODToolboxJATModel&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;cannot&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;saved&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;MAT&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="n"&gt;or&lt;/span&gt; &lt;span class="n"&gt;contains&lt;/span&gt; &lt;span class="n"&gt;nonsaveable&lt;/span&gt; &lt;span class="n"&gt;members&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;Skipping&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is because MATLAB is attempting to serialize and save the Java objects themselves. This &lt;strong&gt;may&lt;/strong&gt; work with JAT (JAT /branches/ODTBX rev 114, Jan 2010) because some JAT classes have been made Serializable. However, not every single JAT class has been modified to implement java.io.Serializable. Mantis issue &lt;a href="https://fftbwall.gsfc.nasa.gov/odtbx/mantis/view.php?id=169" class="" rel="nofollow"&gt;169&lt;/a&gt; is the first issue where JAT classes were made serializable to support the use of JAT and ODTBX in a parallel computing environment. &lt;/p&gt;
&lt;p&gt;Note that serialization is a deep topic that will bring its own set of issues that should be addressed (such as properly coputing and handling the &lt;strong&gt;&lt;em&gt;static final long serialVersionUID&lt;/em&gt;&lt;/strong&gt; in each class). Here are a couple of handy links for reference: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://java.sun.com/javase/6/docs/technotes/guides/serialization/index.html" rel="nofollow"&gt;http://java.sun.com/javase/6/docs/technotes/guides/serialization/index.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://java.sun.com/javase/6/docs/api/java/io/Serializable.html" rel="nofollow"&gt;http://java.sun.com/javase/6/docs/api/java/io/Serializable.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://java.sys-con.com/node/36635" rel="nofollow"&gt;http://java.sys-con.com/node/36635&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://frequal.com/java/PracticalSerialVersionIdGuidelines.html" rel="nofollow"&gt;http://frequal.com/java/PracticalSerialVersionIdGuidelines.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://java.sun.com/developer/technicalArticles/Programming/serialization" rel="nofollow"&gt;http://java.sun.com/developer/technicalArticles/Programming/serialization/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.devx.com/Java/Article/9931/1954" rel="nofollow"&gt;http://www.devx.com/Java/Article/9931/1954&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="problems-with-the-matlab-java-class-path"&gt;Problems with the Matlab Java Class Path&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note: This section is outdated. Most of this classpath setup is now handled in the startup.m provided with ODTBX.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;1. The Java classpath can be set in the $matlab/toolboxes/local/classpath.txt file or in any .m file. We are setting it in startup.m, which assumes that JAT is located at: ../Jat. The ../Jat directory should contain the following directories: external_jar, external_dll, and jat. You must also add the individual external jar files to the classpath, just as you would do in eclipse. In classpath.txt add the location to the jar file as well as the jar file name and the .jar extension. &lt;/p&gt;
&lt;p&gt;2. Matching versions of the JRE. The version of the JRE used by Matlab must match the one used to compile Jat. The version of the JRE used by Matlab can be determined by using the following Matlab command: &lt;br /&gt;
&amp;gt;&amp;gt; version - java &lt;br /&gt;
The version of the JRE used by Matlab can be set by setting the MATLAB_JAVA environment variable. &lt;/p&gt;
&lt;p&gt;3. You also need to import the java package or class you will be accessing. This is best done in the piece of code which actually calls the method in Jat you are trying to use. To import a jat package simply put this at the top of your script:&lt;br /&gt;
&amp;gt;&amp;gt; import jat.subdirect.* &lt;br /&gt;
This will actually include all of the classes that are contained within a subdirectory under jat. If you want to access only one class type: &lt;br /&gt;
&amp;gt;&amp;gt; import classname &lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;4. You also need to add Jat and all of the subfolders to the Matlab path by going into: &lt;br /&gt;
&amp;gt;&amp;gt; File-setPath-Add Folder with Subfolders- select Jat top directory which should include jat, external_jar &amp;amp; external_dll &lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;5. To determine if the classpath has been set correctly, use the following Matlab command: &lt;br /&gt;
&amp;gt;&amp;gt; methodsview className &lt;br /&gt;
If you get an error message, the classpath is not set up correctly. className is the name of the class you import or in the case of a package one of the classes in the package. &lt;/p&gt;
&lt;p&gt;6. In order to see changes that are made in Jat while Matlab is still open you need to add the /Jat directory described in step 1 to the dynamic path, along with the static path (what is done in step 1). To add /Jat to the dynamic path just use the command: &lt;br /&gt;
&amp;gt;&amp;gt; javaaddpath('location to /Jat')&lt;br /&gt;
When changes are made to Jat simply type: &lt;br /&gt;
&amp;gt;&amp;gt; clear java &lt;br /&gt;
at the command prompt in Matlab to see the changes in Matlab. If you make changes to Jat and have only set the static path you will need to close matlab and reopen it to see the changes. However you still want the Jat directory to be on the static path because the dynamic path is not set permanently using the javaaddpath command and therefore you will need to add the /Jat directory to the dynamic path each time you reopen Matlab if you will be making changes to Jat. &lt;/p&gt;
&lt;h1 id="navigation"&gt;Navigation&lt;/h1&gt;
&lt;h3 id="od-toolbox-home"&gt;OD Toolbox Home&lt;/h3&gt;
&lt;p&gt;&lt;a href="/p/odtbx/wiki/Home/" class=""&gt;WikiStart&lt;/a&gt; - Go back to OD Toolbox Home &lt;/p&gt;&lt;/div&gt;</description><pubDate>Tue, 08 Jul 2014 17:34:12 -0000</pubDate><guid>https://sourceforge.netfdb15f576a2b33f7167bd81a91119b73bbd0cf29</guid></item></channel></rss>