Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime.getRuntime().exec()

 
daniel fuertes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
dani
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

This article may help.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Maki Jav
Ranch Hand
Posts: 447
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
 
daniel fuertes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if there is a way on the Java side to send data as ascii.

Maki Jav
 
Joshua Kaplan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic