在Octave中导入Java类 [英] Importing Java Classes in Octave

查看:182
本文介绍了在Octave中导入Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直很难弄清楚该怎么做.从 Octave网站,似乎Java类是通过类路径找到的. 此堆栈溢出答案表示静态Java路径"是动态Java路径".但是我不确定如何设置静态java路径.

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.

在我感兴趣的特定情况下,我试图将javaplex包与Octave一起使用-我已经联系了Github上javaplex的作者,他们说,如果Octave可以加载Java类,那么我可以使用它.据我所知,八度可以做到这一点.我的困难如下.

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.

我正在使用为Matlab编写的代码,Octave和Matlab与Java交互的方式的不同给我带来了麻烦.我必须将Octave的目录设置为

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

在这里,我在命令窗口中运行命令"load_javaplex",该窗口运行脚本"load_javaplex.m".但是,在此脚本中的行是

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.*;

其中"edu.stanford.math.plex4.*"是一个Java类(教程还建议明确运行第二行.

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

我发现这在Octave中不起作用(对于Java接口,我不能只使用Matlab代码),因此我需要将其添加到我的Java类路径中才能访问它.但是我不知道如何在Octave中做到这一点.我应该在标识静态类路径的目录中保存某种.txt文件吗?关于如何在Octave中加载Java类的任何常规信息?我在网上获取信息时遇到困难,无法确定如何执行此操作.将一些内容添加到类路径后该怎么办?

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?

推荐答案

好消息是,将Java指令从matlab语法转换为八度语法非常容易.

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

坏消息是,您将不得不将matlab语法转换为八度语法.尽管这很简单,但这确实意味着您可能还必须在提供的m-文件 中查找所有Java调用(而不仅仅是在您自己的代码中)并调整语法. (显然,您可能想出了一种自动完成该过程的好方法.)

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:

  • 我下载了matlab_examples zip文件并按照指示解压缩(我解压缩了桌面上的文件夹,即计算机上的文件夹,这导致了文件夹/home/tasos/Desktop/matlab_examples
  • 我打开八度,然后cd进入该目录
  • 打开load_javaplex.m文件并删除所有import语句,然后运行它以初始化" javaplex.
  • 现在您已准备好按照教程BUT中的指示运行命令api.Plex4.createExplicitSimplexStream(),首先您需要注意两件事:

  • 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没有提供从包中导入Java类的方法,因此,您的所有类调用都必须由包完全限定. IE. api软件包的Plex4类实际上将需要完全限定为edu.stanford.math.plex4.api.Plex4.您可以通过打开.jar文件并探索其文件夹结构来确认Plex4api包的类,它本身是edu.stanford.math.plex4包的(子)包.

  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.

用于创建Java对象,调用Java方法等的语法在八度音阶中与在matlab中有所不同.有关详细信息,请参见八度音阶手册中的相关页面.

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.

因此,旨在调用(无参数)edu.stanford.math.plex4.api包中的Plex4类的createExplicitSimplexStream方法的api.Plex4.createExplicitSimplexStream()将以八度调用,如下所示:

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.

说了这么多,请注意,虽然您不能直接import类或(sub)包来使您不必一直重写长的包字符串,但octave的java接口确实大量依赖字符串,这意味着将这样的长字符串存储为变量并在必须访问类时重用它们是相当容易的.因此,例如您可以将字符串'edu.stanford.math.plex4.'保存到名为plex4的变量中,而只需在代码中调用javaMethod('createExplicitSimplexStream', [plex4, 'api.Plex4']),依此类推,这样就稍微麻烦些.

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.

玩得开心.

这篇关于在Octave中导入Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆