Importing Java Classes in Octave

删除回忆录丶 提交于 2019-12-02 03:33:01

问题


I've been having difficulty figuring out how to do this. From the Octave website, it seems that java classes are found via a class path. This Stack Overflow answer indicates that the "static java path" is to the "dynamic java path". Yet I'm not sure how to set up the static java path.

In my particular situation of interest, I'm trying to use the javaplex package with Octave - I've contacted the authors of javaplex on Github and they said if Octave can load java classes, then I can use it. Octave can do this, as far as I know. My difficulty is the following.

I am using code written for Matlab, and the differences in how Octave and Matlab interact with Java is throwing me some trouble. I have to set the directory for Octave to be

C:\...\javaplex-master\javaplex-master\dist\matlab-examples-4.3.4\matlab_examples

From here, I run the command "load_javaplex" in the command window, which runs the script "load_javaplex.m". Within this script however are the lines

javaaddpath('./lib/javaplex.jar');

import edu.stanford.math.plex4.*;

where "edu.stanford.math.plex4.*" is a java class (the tutorial also suggests running the second line explicitly).

I've figured out this doesn't work in Octave (as for Java interfacing, I can't just use Matlab code), and so I need to add it to my java classpath to access it. Yet I don't know how to do this in Octave. Should I save some sort of .txt file in the directory that identifies the static class path? Any general info on how I can load java classes in Octave? I am having difficulty with the information available online figuring out how to do this. What do I do after I add something to the classpath?


回答1:


The good news is, it is very easy to translate the java instructions from matlab syntax to octave syntax.

The bad news is, you will have to translate the matlab syntax to octave syntax. While this is straightforward, it does mean you may have to hunt any java calls in the provided m-files as well (rather than just in your own code) and adapt the syntax. (Obviously you might come up with a nice way to automate the process instead.)

Here is how I got the tutorial to work on octave:

  • I downloaded the matlab_examples zipfile and unzipped as instructed (I unzipped the folder on my desktop, i.e. on my machine this resulted in the folder /home/tasos/Desktop/matlab_examples
  • I open octave and cd into that directory
  • Open the load_javaplex.m file and remove all import statements, and then run it to "initialize" javaplex.
  • You are now ready to run the command api.Plex4.createExplicitSimplexStream() as indicated in the tutorial, BUT, first you need to note two things:

    1. Octave does not provide a way to import java classes from packages, therefore all your class calls need to be fully qualified by package. I.e. the Plex4 class of the api package will actually need to be fully qualified as edu.stanford.math.plex4.api.Plex4. You can confirm Plex4 is a class of the api package, which is itself a (sub)package of the edu.stanford.math.plex4 package by opening the .jar file and exploring its folder structure.

    2. The syntax for creating java objects, calling java methods, etc, is different in octave than in matlab. See the relevant page in the octave manual for details.

Therefore the api.Plex4.createExplicitSimplexStream(), which is intended to call (with no arguments) the createExplicitSimplexStream method of the Plex4 class in the edu.stanford.math.plex4.api package, will be called in octave as follows:

javaMethod( 'createExplicitSimplexStream', 'edu.stanford.math.plex4.api.Plex4')

which then outputs as the answer desribed in the tutorial.

Having said all that, note that, while you cannot import classes or (sub)packages directly to save you from having to rewrite long package strings all the time, octave's java interface does seem to rely on strings a lot, which means it is fairly easy to store such long strings as variables and reuse them at the point of having to access a class. So, e.g. you could save the string 'edu.stanford.math.plex4.' to a variable called plex4 and simply call javaMethod('createExplicitSimplexStream', [plex4, 'api.Plex4']) in your code instead, etc, which makes it slightly less cumbersome.

Have fun.



来源:https://stackoverflow.com/questions/51802175/importing-java-classes-in-octave

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!