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

2
  • 1
    Do you want to run an m-file or a MATLAB executable from Java? Commented May 9, 2013 at 12:55
  • 1
    Please have a look at this SO question. Commented May 9, 2013 at 13:57

1 Answer 1

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");
}
Sign up to request clarification or add additional context in comments.

6 Comments

All this code for Runtime.getRuntime().exec(commandline);? :)
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. :-)
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.
@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.
@HovercraftFullOfEels Upvote for Clarifying My Point.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.