从Matlab创建的jar文件返回值 [英] Return value from jar file created from Matlab

查看:214
本文介绍了从Matlab创建的jar文件返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Matlab代码,最后计算索引向量。我使用库编译器将matlab代码编译为java包.jar文件。我导出了jar文件,以便为我的主Java项目运行它。包类的名称是Epidemic。我导入了jar文件(将其添加为外部jar)。在主代码中,我尝试创建我的类的对象(在jar文件中)。我已经将类的名称定义为Epidemic。因此,我的代码:

I have got a Matlab code which at last calculates a vector of indexes. I used library compiler in order to compile matlab code to a java package .jar file. I exported the jar file in order to run it for my main Java project. The name of the package class is Epidemic. I imported the jar file (add it as an external jar). In main code I tried to create an object of my class (in jar file). I have already defined the name of the class as Epidemic. Thus, my code:

import epidemic.Epidemic;
...
public static void main(String[] args) throws IOException {

    List<Double> list1 = new ArrayList<Double>();
    List<Double> list2 = new ArrayList<Double>();

    Epidemic object = new Epidemic(); 
    object.epidemic(list1, list2);
    System.out.println(list1);
}

我使用project-> Libraries右键单击将.jar文件添加到java项目添加外部罐子。 Netbeans自动检测对象的方法。但是我收到以下错误:

I add the .jar file to java project using project->Libraries right click add external jars. Netbeans automatically detects object's methods. However I am getting the following errors:

Exception in thread "main" java.lang.NoClassDefFoundError:  
com/mathworks/toolbox/javabuilder/internal/MWComponentInstance
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at twittertrendsetters.TwitterTrendSetters.main(TwitterTrendSetters.java:70)
Caused by: java.lang.ClassNotFoundException:
com.mathworks.toolbox.javabuilder.internal.MWComponentInstance
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 13 more

这是什么问题?我应该添加其他东西以使jar正常工作吗?

What is the issue here? Am I supposed to add something else in order the jar to work properly?

编辑:我添加位于MATLABROOT / toolbox / javabuild / jar / javabuild中的.jar文件。 jar到我的classpath和它似乎工作的类。现在我面临另一个问题。当我打印基于html docs的list1获取matlab .m文件的输出时,我得到一个空的arrayList。 Matlab函数返回一个双精度数组Nx1。如何正确解析它到java arrayList。

I add the .jar file located in MATLABROOT/toolbox/javabuild/jar/javabuild.jar to my classpath and the class it seems to work. Now I am facing another problem. When I print the list1 which based on html docs takes the output of matlab .m file I got an empty arrayList. Matlab function returns an array Nx1 of doubles. How can I parse it correctly to the java arrayList.

我的matlab代码:

My matlab code:

function eP = epidemic() // zero input

graph = dlmread('graph.edges'); //a graph
graph_ids=importdata('cms_V3_id.txt');  // indexes of the graph

for index = 1:length(graph)
 grph(index,1) = find(graph_ids == graph(index,1));
 grph(index,2) = find(graph_ids == graph(index,2));
end
grph(:,3)= graph(:,3);

grph(end + 1, :, :) = [max(max(grph)) max(max(grph)) 1 ];
grph =  spconvert(grph);

[S, prev] = brutte_topk2(grph, 3707); //function approximate pagerank result
eP = graph_ids(S); // returning a list of indexes

我试图使用你的方法。我创建一个OBject表并将结果解析为它。

I tried to use your approach. I create a table of OBject and parse the result into it.

    Epidemic object = new Epidemic(); 
    Object[] result;
    result = object.epidemic(1);
    System.out.println((Double)result[0]);

但是我得到javabuilder.MWNumericArray不能转换为java.lang.Double。当我只打印reuslt

However I am getting javabuilder.MWNumericArray cannot be cast to java.lang.Double. When I print just the reuslt

推荐答案

您需要向Java项目类路径添加两件事:

There are two things you need to add to the Java project classpath:


  • 部署的JAR文件创建

  • Java Builder 拥有自己的JAR文件。如果你在目标机器上安装了MATLAB,你可以在 $ MATLABROOT \toolbox \ javabuilder \ jar \ javabuilder.jar 中找到它们,否则安装相应的< a href =http://www.mathworks.com/products/compiler/mcr/> MCR 运行时(免费提供),并在类似的路径中找到JAR文件。

  • the deployed JAR file you created from the MATLAB code.
  • Java Builder's own JAR files. If you have MATLAB installed on the target machine, you can find those inside $MATLABROOT\toolbox\javabuilder\jar\javabuilder.jar, otherwise install the appropriate MCR runtime (available for free), and locate the JAR file in a similar path.

参见这里获取完整说明。

为了完整起见,下面是一个工作示例。

For the sake of completeness, below is a working example.


  1. 假设我们有下面的MATLAB函数返回一个数字数组。

  1. Assume we have the following MATLAB function that returns a array of numbers.

function list = epidemic()
    list = randi(100, [1, 10]);
end


  • 使用 applicationCompiler MATLAB app,创建一个新项目来构建Java包。将上述函数添加到项目中,设置类和方法名称,然后构建包。我们应该得到一个JAR文件,比如说: Epidemic.jar

  • Using the applicationCompiler MATLAB app, create a new project to build a "Java Package". Add the above function to the project, set class and method names, then build the package. We should get a JAR file, say: Epidemic.jar

    接下来我们创建一个Java程序测试上面的包。例如:

    Next we create a Java program to test the above package. For example:

    import java.util.*;
    import com.mathworks.toolbox.javabuilder.*;  // MATLAB Java Builder
    import Epidemic.*;                           // our compiled package
    
    public class TestEpidemic {
        public double[] getArray() throws MWException {
            Epidemic obj = null;
            Object[] out = null;
            double [] arr = null;
            try {
                obj = new Epidemic();
                out = obj.epidemic(1);  // request one output
                arr = (double[]) ((MWArray)out[0]).getData();
            } catch (MWException e) {
                System.out.println("Exception: " + e.toString());
            } finally {
                MWArray.disposeArray(out);
                obj.dispose();
            }
            return arr;
        }
    
        public static void main (String[] args) {
            try {
                TestEpidemic e = new TestEpidemic();
                double[] arr = e.getArray();
                for(double x : arr) {
                    System.out.println(x);
                }
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }
    


  • 最后我们编译并运行测试程序:

  • Finally we compile and run the test program:

    javac.exe -classpath "%MATLABROOT%\toolbox\javabuilder\jar\javabuilder.jar";.\Epidemic.jar TestEpidemic.java
    
    java.exe -classpath .;"%MATLABROOT%\toolbox\javabuilder\jar\javabuilder.jar";.\Epidemic.jar TestEpidemic
    

    您应该会看到一个包含10个双数字的数组。

    you should see an array of 10 double numbers printed.

    这篇关于从Matlab创建的jar文件返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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