We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Running outside programs
Page Index Toggle Pages: 1
Running outside programs (Read 1474 times)
Running outside programs
Jun 11th, 2009, 9:00am
 
I have a couple c++ command line apps that I need to run in order to generate input data for my Processing sketch.  I'd like to bundle these somehow or run them as the sketch starts.  They need to be run sequentially so that the output of one can be used for the next and then the file output is used in the sketch.  What would be the best way to accomplish this?  Would it just be to use open() commands or is there some easy way to wrap this?  Thanks
Re: Running outside programs
Reply #1 - Jun 11th, 2009, 2:01pm
 
The open() command doesn't seem to be working... I can get it to open a text document (just to test it as a string), but can't get it to run an application with a switch to output a file.  I'm following the instructions but not having any luck.

Code:

String[] params = { "/Users/Jeff/Documents/Processing/sketch/data/fastcommunityMH", "-f", "graph.pairs" };
open(params);
Re: Running outside programs
Reply #2 - Jun 11th, 2009, 2:13pm
 
Your question lit up some deep niche in my memory of some simple Java code that I used to launch applications and here it is dusted off from Jan 2005.

It does not handle input/output from the program but it might be a start. Undecided
Code:

void setup(){
 size(400,400);
 try {
   Runtime r = Runtime.getRuntime();
   if (r == null)
     System.out.println("Unable to get runtime");
   else
     System.out.println("Runtime aquired");
   System.out.println(r);

   Process p;

   System.out.println("Prepare process for start");
   System.out.flush();
   p = r.exec(new String[] {
     "c:/Program Files/JASC Software Inc/Paint Shop Pro 7/psp.exe"        }
   );
   // p = r.exec(new String[] {"java", "-classpath k:/eclipse/workspace/PathFinderMap", "pkl.PathFinderMapGUI.PathFinderGUI"});
   // p = r.exec(new String[] {"javaw", "-classpath k:/eclipse/workspace/ClockTest", "pkl.datetime.ClockTester"});

   System.out.println("Process running");
   System.out.flush();
 }
 catch (Exception e) {
   System.out.println("Error: " + e);
 }
}


I left in some comments which show how to launch another Java program.

I ran this application (PC/Windows) and bingo Paint Shop Pro opened up.Smiley  Unfortunately I don't have a Mac to try it on. Sad

Re: Running outside programs
Reply #3 - Jun 11th, 2009, 4:21pm
 
For input from (not sure about sending), I used this to read the output of a Python script:
Code:
import java.io.*;

try {
String line;

OutputStream stdin = null;

Process p = Runtime.getRuntime().exec("\"*pathtopython*\\python.exe\" \"*pathtoscript*\\try.py");


BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((line = input.readLine()) != null) {
println(line);
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}

exit();
PS: This was on Windows also...
Re: Running outside programs
Reply #4 - Jun 11th, 2009, 6:02pm
 
Thanks both of you!!! I was able to get it working using your examples.  

One remaining issue is that I'm having to use absolute paths, which will certainly be a problem and change as it moves from system to system and OS to OS.  Does anyone know how to get the path of the data directory so I can use it as a variable for the exec() path?
Re: Running outside programs
Reply #5 - Jun 11th, 2009, 7:50pm
 
Ok, I think I found the solution in this thread.
http://processing.org/discourse/yabb2/board_Syntax_3Baction_display_3Bnum_1210972905.html
Code:

String dir;

File dataDir = new File(sketchPath, "data");
dir = dataDir.getAbsolutePath();

or

dir = dataPath("");

Now I just need to test it out when I export it as an application. Smiley
Re: Running outside programs
Reply #6 - Sep 20th, 2009, 9:53pm
 
My apologies for opening this thread again, but I'm having an issue trying to get a bash script running and I don't know where to turn for help.

My bash script, "makeMovie.sh" (right now) is very simple and uses a qtcoffee command to scale a movie:
#!/bin/sh
catmovie -set-auto-play -self-contained -scaleMovieTo 0:50 /Volumes/artwork/vid/mov1.mov -o /Volumes/artwork/vid/mov2.mov

I have the appropriate +x file permissions set and I'm able to run this successfully from the command line: $> makeMovie.sh

Resulting in mov2.mov being created, and a playable file, etc.  No problems whatsoever.

This is also true when using the full path $> Users/sbearse/Documents/Processing/Scripts/makeMovie.sh

But when I try to launch it from within Processing I receive an Error code (127) from the Java exitvalue:
     try {
   Runtime r = Runtime.getRuntime();
   if (r == null)
   System.out.println("Unable to get runtime");
   else
     System.out.println("Runtime aquired");
   System.out.println(r);

   Process p;

   System.out.println("Prepare process for start");
   System.out.flush();
   
   cmd = "/Users/sbearse/Documents/Processing/Scripts/movieCreate.sh";
   command = new String[] {"/bin/bash", "-c", cmd};    
   p = r.exec(command);

    int exitVal = p.exitValue();    
   System.out.println("Process exitValue: " + exitVal);
   System.out.flush();
 }
 catch (Exception e) {
   System.out.println("Error: " + e);
 }
}

-----
Also, looking on the java helpgroups I thought I'd find some funny formatting on my *.sh script, but this is what od -c returns:

sarah-bearses-powerbook-g4-15:/volumes/artwork/vid sbearse$ od -c /Users/sbearse/Documents/Processing/Scripts/movieCreate.sh
0000000    #   !   /   b   i   n   /   s   h  \n   c   a   t   m   o   v
0000020    i   e       -   s   e   t   -   a   u   t   o   -   p   l   a
0000040    y       -   s   e   l   f   -   c   o   n   t   a   i   n   e
0000060    d       -   s   c   a   l   e   M   o   v   i   e   T   o    
0000100    0   :   5   0       /   V   o   l   u   m   e   s   /   a   r
0000120    t   w   o   r   k   /   v   i   d   /   m   o   v   1   .   m
0000140    o   v       -   o       /   V   o   l   u   m   e   s   /   a
0000160    r   t   w   o   r   k   /   v   i   d   /   m   o   v   2   .
0000200    m   o   v  \n                                                
0000204

----

Any help would be very, very, very (many more very's) appreciated.  I'm trying to pull together an installation piece this week, and this is the last major obstacle.

Thank you!
Sarah
Re: Running outside programs
Reply #7 - Sep 20th, 2009, 10:05pm
 
Wow. It's amazing.  All day I worked this over, top to bottom trying to find my issue.  Finally I resolve to post to you all with a plea for help ... and within moments of doing so?  I figured it out.  I'd love to just bow my head in shame, but in case others are reading this trying to get their bash script executed within Processing, here's what worked for me:


int exitVal = p.waitFor();

instead of:
int exitVal = p.exitValue();

....
So silly.  I just wasn't giving the process anytime to run.  As a disclaimer, I have no experience in Java, and barely any Processing. :-)

Cheers,
Sarah
Page Index Toggle Pages: 1