如何从Matlab程序中调用java代码 [英] How do I call java code from a Matlab program

查看:765
本文介绍了如何从Matlab程序中调用java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Matlab调用我自己的Java程序。

I would like to calling my own Java program from Matlab.

这是我的java程序:

This is my java program:

public class TestArgu{
    public static void main(String[] args){
        System.out.println("Test passing arguments!");
    }

    public void addNumber(int aNumber){
        ansNumber = aNumber+5;
        chk = aNumber;
        System.out.println("input number = " + chk + ".\n");
        System.out.println("ans = " + ansNumber + ".\n");
    }

    public int ansChk(){
        return ansNumber;
    }

    private int ansNumber;
    private int chk;
}

我从这个链接一步一步地做了
http://www.mathworks.nl/support/ solutions / en / data / 1-URS0E /?... 1 ...

I did step by step from this link http://www.mathworks.nl/support/solutions/en/data/1-URS0E/?...1...

但它不能用于我的程序。
我在服务器计算机上运行Matlab程序。
所以我无法编辑 classpath.txt

but it is not working with my program. I'm running Matlab program from the Server computer. So I cannot edit the classpath.txt.

如何解决这个问题?

推荐答案

首先,从班级中删除main函数。然后添加行

First, delete the main function from your class. Then add the line

package mypackage.release;

。然后使用命令编译它

javac -verbose -cp /home/javaclasses -d /home/javaclasses /home/javasource/TestArgu.java

在matlab类型中

javaaddpath('/home/javaclasses');
clear java;
import mypackage.release.*;
test=TestArgu;
test.addNumber(6);
test.ansChk();

请记住,每次进行更改并编译java类时,都必须调用在更改可用之前,在matlab中清除java 。这也有清除工作区中所有变量的不幸副作用,因此请确保在调用之前没有任何重要的保存。

Remember that everytime you make changes and compile the java class, you must call clear java in matlab before the changes are available. This also has the unfortunate side effect of clearing all the variables in your workspace so make sure you don't have anything important to save before calling it.

这篇关于如何从Matlab程序中调用java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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