{"id":320,"date":"2009-07-06T11:33:46","date_gmt":"2009-07-06T11:33:46","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2009\/07\/06\/calling-java-from-matlab\/"},"modified":"2009-07-06T13:56:31","modified_gmt":"2009-07-06T13:56:31","slug":"calling-java-from-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2009\/07\/06\/calling-java-from-matlab\/","title":{"rendered":"Calling Java from MATLAB"},"content":{"rendered":"<p>So far no one has taken me up on the extra credit from the <a href=\"https:\/\/blogs.mathworks.com\/community\/2009\/06\/22\/interactive-web-pages-in-matlab-part-4\/\">Interactive Web Pages<\/a> post. I thought the problem of displaying a figure without an image file was too interesting to pass up, so I figured out the solution myself and put it up on the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24514-base64-image-encoder\">file exchange<\/a>. The second challenge involving ActiveX will remain open until I get chance to write up its solution. Or, if you don't have the time to do that, you can still win a t-shirt by <a href=\"https:\/\/blogs.mathworks.com\/community\/2009\/06\/29\/what-does-your-matlab-desktop-look-like-again\/\">sending us your desktop<\/a>. <\/p>\n<p>My solution to the image challenge involves encoding binary information from an image file as text through the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Base64\">base64 scheme<\/a>. At first I thought to implement the base64 algorithm myself in MATLAB, but I learned my lesson the hard way a few years ago writing my own md5 hash-computing program (don't ask). This time I wanted to use a readily-available solution. Since there is no toolbox function for this, I decided to take advantage of a Java library since (a) I could use the same library on every platform (instead of needing a different pre-compiled binary) and (b) I am pretty comfortable with Java. My solution takes advantage of the freely available <a href=\"http:\/\/commons.apache.org\/codec\/\">Apache Commons Codec<\/a> package. <\/p>\n<p>Using Java from a MATLAB program is as simple as using a MATLAB function, particularly if you are used to using MATLAB package functions. You can use any public method in the <a href=\"http:\/\/java.sun.com\/javase\/6\/docs\/api\/\">Java SE API<\/a> from the command line. The running version of the JRE depends on your version of MATLAB and operating system and determines what API functionality is available from MATLAB. To determine the Java version use the command <tt>version(<span style=\"color: #A020F0\">'-java'<\/span>)<\/tt>. To change the Java version (not recommended) you can follow <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/techdoc\/matlab_external\/f98533.html&https:\/\/www.mathworks.com\/access\/helpdesk\/help\/releases\/R2009a\/techdoc\/matlab_external\/f98533.html#f122001\">these instructions<\/a>.<\/p>\n<p>For instance instead of MATLAB's <tt>str2num<\/tt> you could use: Double's <tt>parseDouble<\/tt>.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">str2num(<span style=\"color: #A020F0\">'2.3'<\/span>)\r\njava.lang.Double.parseDouble(<span style=\"color: #A020F0\">'2.3'<\/span>)<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n    2.3000\r\n\r\n\r\nans =\r\n\r\n    2.3000\r\n\r\n<\/pre>\n<p>Of course, this is a trivial example. Many of our functions such as <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/techdoc\/ref\/xmlread.html&https:\/\/www.mathworks.com\/cgi-bin\/texis\/webinator\/search\/\"><tt>xmlread<\/tt><\/a> make use of more complicated series of calls to java objects. The payoff in the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/techdoc\/ref\/sendmail.html&https:\/\/www.mathworks.com\/cgi-bin\/texis\/webinator\/search\/\"><tt>sendmail<\/tt><\/a> function comes at the end using the Transport object to send a pre-constructed MimeMessage.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">javax.mail.Transport.send(msg)\r\n<\/pre>\n<p>My base64 program does not use a class from the standard JRE (in fact, neither does sendmail) but instead uses a third party library (Apache Commons Codec) that I happen to know ships with MATLAB and is already available on the classpath. <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">\r\nencoder = org.apache.commons.codec.binary.Base64;\r\nbase64string = char(encoder.encode(bytes))';\r\n<\/pre>\n<p>These examples has three things about using Java that I want to address so you can use other Java libraries in MATLAB: (1) the class path, (2) calling syntax, and (3) data types.<\/p>\n<p><strong>Java Class Path<\/strong><br \/>\nMATLAB maintains a path for Java classes separate from the search path. That means even if you have a .class or .jar file on the MATLAB path, unless you use <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/techdoc\/ref\/javaaddpath.html\"><tt>javaaddpath<\/tt><\/a> you will not be able to use it. To see what is currently on the path use <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/techdoc\/ref\/javaclasspath.html\"><tt>javaclasspath<\/tt><\/a>. Running this command you will show you a long list of files that ship with matlab called the Static Class Path and then you'll see the Dynamic Class Path. The dynamic class path is where classes added to the path with <tt>javaaddpath<\/tt> will be placed. They can be removed with <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/index.html?\/access\/helpdesk\/help\/releases\/R2009a\/techdoc\/ref\/javarmpath.html\"><tt>javarmpath<\/tt><\/a> and have to actively reloaded each session of matlab. The static class path comes from $matlabroot\\toolbox\\local\\classpath.txt and are only added to the class path at MATLAB startup. The ones listed here by default are all the Java classes we write (everything in the <tt>com.mathworks<\/tt> package). Calling any of these classes directly is not supported and likely to break on a release-to-release basis, but there are resources on the web that make use of these effectively. <\/p>\n<p><strong>Calling Java Functions<\/strong><br \/>\nOnce you add a class to the classpath and it shows up in the <tt>javaclasspath<\/tt>, you can use it from the Command Line or from a MATLAB-file. To do that you can use the fully-qualified name (e.g. <tt>javax.swing.JTable<\/tt>) or like with MATLAB packages, you can use the import statement to just call the class name (e.g. <tt>import('javax.swing.*'),JTable<\/tt> ).<\/p>\n<p><strong>Data Types<\/strong><br \/>\nLooking at my string to double example you'll see that I passed a MATLAB char array to the java function and got back a MATLAB double, even though <tt>methodsview(<span style=\"color: #A020F0\">'java.lang.Double'<\/span>)<\/tt> shows that <tt>parseDouble<\/tt> takes a <tt>java.lang.String<\/tt> as its input. MATLAB will automatically convert primitive types when calling Java methods, as well as convert <tt>char arrays<\/tt> to <tt>java.lang.String<\/tt> objects when used as inputs. As you can see in my base64 example, I have to explicitly convert the <tt>java.lang.String<\/tt> returned by <tt>encoder.encode()<\/tt> to a MATLAB char array.<\/p>\n<p>Those are the basics of calling Java. In future posts I hope to discuss memory management with Java (a topic of some recent technical support queries) as well as threading issues, as related to GUIs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So far no one has taken me up on the extra credit from the Interactive Web Pages post. I thought the problem of displaying a figure without an image file was too interesting to pass up, so I figured... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2009\/07\/06\/calling-java-from-matlab\/\">read more >><\/a><\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[242,245],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/320"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=320"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/320\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}