1

sorry if I ask a simple question but I would like to ask whether it is possible when executing a java source to have code in it which makes an independent Matlab programme run (not only to execute Matlab code in java) ? I think this is also general question whether you can start other programmes in the process of execution of your code in Java.

Thank you.

Best,

M

M S
  • 11
  • 1

1 Answers1

1

I know you can run external Programmes like this:

import java.io.*;
public class CommandExection {
public CommandExection(String commandline) {
try {
    String line;
    Process p = Runtime.getRuntime().exec(commandline);
    BufferedReader input =
    new BufferedReader
    (new InputStreamReader(p.getInputStream()));
    while ((line = input.readLine()) != null) {
        System.out.println(line);
    }
    input.close();
} catch (Exception err) {
    err.printStackTrace();
}
}

public static void main(String argv[]) {
    new CommandExection("c:\\Yourprogram.exe");
}
Stanley Mungai
  • 4,044
  • 30
  • 100
  • 168
  • All this code for `Runtime.getRuntime().exec(commandline);`? :) – Eitan T May 09 '13 at 12:59
  • This is an example an executable one you do not need to write the entire code if you figure out how to Place it in your code. :-) – Stanley Mungai May 09 '13 at 13:01
  • Obviously. My point was that everything other than getRuntime().exec is irrelevant. I agree that it may be useful to redirect the output of the external progam to stdout, but it's not what is asked here. Instead, it might create unnecessary confusion. – Eitan T May 09 '13 at 13:35
  • 2
    @EitanT: I disagree. If you do not swallow the input and error streams, your external process will likely choke its buffer and die. I think that this example actually needs more including gobbling the error stream and using ProcessBuilder to get the Process. If you don't tell the newbie to do this, they won't. – Hovercraft Full Of Eels May 09 '13 at 13:43
  • @HovercraftFullOfEels Upvote for Clarifying My Point. – Stanley Mungai May 09 '13 at 14:03
  • @HovercraftFullOfEels This is indeed a pitfall if the MATLAB process does write something to the output stream. I agree that extra explantion may be helpful, but instead of replicating code, I'd rather keep the answer consice and add a link to [this answer](http://stackoverflow.com/a/13991125/1336150), for example. – Eitan T May 09 '13 at 14:52