File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Runtime.getRuntime().exec() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime.getRuntime().exec()" Watch "Runtime.getRuntime().exec()" New topic


daniel fuertes

Joined: Jan 15, 2008
Posts: 3
hi there,
i need to call some matlab functions from java and i've found useful tips on this site, but i have got some problems when trying them on a simple example :S
i just want to pass a number as an argument for the function i'm calling, but can't get a good result!

Process p= Runtime.getRuntime().exec("C:/Documents and Settings/DANIEL/workspace/ProbandoMatlab/src/pvt.exe 100" );

i have tried with a string array and a lot of different ways as well... but no idea

cmd[0]= "C:/Documents and Settings/DANIEL/workspace/ProbandoMatlab/src/pvt.exe";
cmd[1]= "100";

Process p= Runtime.getRuntime().exec(cmd);

any ideas? could someone help me??
thanks in advance
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41062
Welcome to JavaRanch.

This article may help.

Ping & DNS - my free Android networking tools app
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
If you're on Windows, you should probably use backslash \ rather than forward slash /. Since it's in a Java literal you will have to double each backslash. Also, you may need to put quotes around the whole path, because of the spaces inside "Documents and Settings". Those quotes will also need to be escaped with backslashes. So:

"I'm not back." - Bill Harding, Twister
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435

Please first of all see whether you can pass argument to matlab from command prompt
You may see this post of mine at Other Java APIs forum for help as you are on windows.
I have posted a code to run bat file which in turns execute command.

Maki Jav

Help gets you when you need it!
daniel fuertes

Joined: Jan 15, 2008
Posts: 3
thank you very much for your answers.
as i can see, it's not a problem of the slashes, because i can call matlab functions with no problem and read the sentences displayed when sending no arguments, the problem comes if i send an argument...

Process p= Runtime.getRuntime().exec("C:/Documents and Settings/DANIEL/workspace/ProbandoMatlab/src/pvt.exe 100");

String [] parametros = new String[3];
parametros[0]= "C:/Documents and Settings/DANIEL/workspace/ProbandoMatlab/src/pvt.exe" ;
parametros[1]= "";
parametros[2]= "100";

Process p= Runtime.getRuntime().exec( parametros );
String [] parametros = new String[2];
parametros[0]= "C:/Documents and Settings/DANIEL/workspace/ProbandoMatlab/src/pvt.exe" ;
parametros[1]= "100";

Process p= Runtime.getRuntime().exec( parametros );

if i do any these posibilities, "100" as an argument, i get three numbers as a result (trying with different numbers, i get as many result numbers as digits in my argument), when i'm just expecting one!!!

hope somebody knows what's going on here....
thank you very much
daniel fuertes

Joined: Jan 15, 2008
Posts: 3
i found out the problem is not when passing arguments, the problem is the matlab .exe.
i dont know why but it converts in ascii code all the inputs, so i get strange numbers at the output :S

thanks for the answers
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435
See if there is a way on the Java side to send data as ascii.

Maki Jav
Joshua Kaplan

Joined: Jul 08, 2011
Posts: 5
An easier way than using Runtime.exec() is to make use of the matlabcontrol Java library. You can use feval to directly call MATLAB functions and provide any number of arguments directly. To get started, take a look at the walkthrough.
Don't get me started about those stupid light bulbs.
subject: Runtime.getRuntime().exec()
Similar Threads
running external program from Java
Runtime getRuntime() exec(String command) - How does this work?
Runtime.getRuntime().exec doesnt execute some files
running external commands in java
spaces in file name