Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Calling perl commands from java apps

Calling perl commands from java apps
Thread Tools
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Mar 3, 2001, 12:05 PM
 
Okay, all I want to do is call a stupid little perl command from my java app, and it refuses to behave.

Code:
Runtime r = Runtime.getRuntime(); String hello = "perl -e \'open(INFO, \">>info.txt\");\'"; Process p = r.exec(hello); p.waitFor(); BufferedReader in2 = new BufferedReader(new InputStreamReader (p.getErrorStream())); String emsg="",output=""; while ((emsg = in2.readLine()) != null) output += " "+emsg; p.destroy(); return ouput;
That's my code, mius all the throws clause and such. The returned output (the error message) is: "Can't find string terminator "'" anywhere before EOF at -e line 1." And yet if I return the variable hello instead, print that out, and then run it manually, it works fine. Does anyone know what's up? (I've tried it with ' and \', it doesn't make a difference)

Thanks,
F-bacher
     
Dedicated MacNNer
Join Date: Oct 2000
Location: Pasadena, CA, USA
Status: Offline
Reply With Quote
Mar 4, 2001, 10:07 AM
 
Two problems:

1. Runtime.exec(String) attempts to parse the parameter in the command to be run and it's arguments; it is performing additional escapes on your "-e" parameter. Do this instead:

> String[] hello = {"perl", "-e \"open(INFO, '>>info.txt');\""};
> Process p = r.exec(hello);

You should get the desired results.

2. Make output a StringBuffer instead of a String, and do this:

> while ((emsg = in2.readLine()) != null)
> output.append(emsg);

The code as you wrote it is inefficient - a new String object gets created for every line read from in2.readLine(). Switching to a StringBuffer will yield measurable improvements in speed and memory usage, especially if the output from the perl script is many lines long.

Erik
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:30 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2