4

I'm working on a project and I wish to call java programs in Matlab. A sample structure of java programs are given as follows:

                 mainproj
                /    |   \
               src   lib  bin
               / \        / \ 
              a    b     ac   bd

Folder "lib" contains jar files. Folders ac and bd have .classes of java. Folders a and b have java files + some other class files. I wish to run a java file named "launcher.java" under folder a, it uses jar files and calls a java program available in folder b, which has launcher.class file in folder ac.

I'm confused how to call this program successfully. I found some solutions online but they are not working for me. Calling Java from MATLAB?, Calling Java from MATLAB, Is it worth to call java from matlab?, Calling Java from Matlab is very slow.

The launcher.java file is as follows:

package ac;
import....
public class launcher
{
....
....
public static void main(String[] args) { run();}
public static void run() {......}
}

What I performed is as follows:

javaaddpath('mainproj\lib\x.jar')
javaaddpath('mainproj\bin\ac')
import mainproj.*;
import mainproj.lib.*;
import mainproj.bin.*;
import mainproj.bin.ac.*;
import mainproj.bin.bd.*;

l = launcher;
javaMethod('main', l);

[I wish to call main of launcher.class in folder as that automatically calls run() method, and run() calls other class file in folder bd]

The output error I'm getting is : No class launcher can be located or no methods for class

Any suggestion/help is appreciated.

Community
  • 1
  • 1
user24094
  • 294
  • 1
  • 11
  • Have you verified that you can run your Java program directly from the command line, in the same directory and with the same settings you expect Matlab to use? – dimo414 Nov 25 '15 at 06:23
  • @dimo14 yeah! I have checked this all. I checked a simple program in a directory, is is working, but the above mentioned is not working. – user24094 Nov 25 '15 at 16:02
  • Can you add the command line you run to your question? I have a theory, but need to know how you're running it successfully. – dimo414 Nov 25 '15 at 17:02
  • Already added above, how I'm trying to run from command line in MATLAB. – user24094 Nov 25 '15 at 17:32
  • Right, I see the Matlab code. I'm talking about the command line (i.e. `$ java ... ac.launcher ...`). Have you verified it works on the command line? If so, what was the exact command? If not, please start by compiling (`javac`) and running (`java`) your Java program directly, without Matlab. – dimo414 Nov 25 '15 at 17:46
  • This project is working in Eclipse correctly. I compiled all the java files and generated class files. But I want to call these .class files from MATLAB. The problem I'm looking is related to some path or so. I'm able to call a .class file in a folder but not from further subfolders. – user24094 Nov 25 '15 at 19:15

1 Answers1

0

It would be very helpful if you provided specifics, rather than just claiming it works in Eclipse. Obviously something is different between your Eclipse setup and your Matlab code, but if you don't share your working setup we can only guess what's wrong with your Matlab setup.

Below is my guess; if it's wrong please include the exact commands used to build and run your Java code. If you cannot do so via the command line, please include the exact Eclipse Build Path and Run Configuration.


You're adding mainproj\bin\ac to the Java class path. This tells Java to treat mainproj\bin\ac as a root directory for class files. Since you're trying to launch the class ac.launcher there should be a launcher.class file inside an ac package directory inside the directory added to the Java class path.

In other words, if your java command looked like this:

java -cp mainproj\bin\ac ac.launcher

Java will look for a mainproj\bin\ac\ac\launcher.class file.

More likely your launcher.class file is located in mainproj\bin\ac (since ac is the package), and so mainproj\bin should be the directory added to the Java class path, so the command looks like this:

java -cp mainproj\bin ac.launcher

This will load a mainproj\bin\ac\launcher.class file.

tl;dr Try replacing javaaddpath('mainproj\bin\ac') with javaaddpath('mainproj\bin') and read up on how the Java class path works.

dimo414
  • 47,227
  • 18
  • 148
  • 244