my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Executing Perl script from Java program 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 » Beginning Java
Bookmark "Executing Perl script from Java program" Watch "Executing Perl script from Java program" New topic
Author

Executing Perl script from Java program

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 364
I have following code
process = Runtime.getRuntime().exec("perl C:\Users\cigniti\Desktop\All text files\hello.pl");
at this line I am getting error "Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )"

So how do I provide path for perl script ? How can I run this script here ?

Thanks

[Edit: I got tired of looking at "Wxecuting" so I fixed the title]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Try escaping the \
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 364
I am escaping as follows. But still I am getting "command failure" error
process = Runtime.getRuntime().exec("perl C:\Users\cigniti\Desktop\All text files\hello.pl");

Any idea why ?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

You're not escaping the backslashes correctly. The backslash has a special meaning in Java string literals. If you want to put a backslash in a string literal, you have to double it:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 364
I forgot to mention. I tried escaping with \\ before, but that again gave same result "command failure"
Then I tried other way, but that did not work either.

what can this be ?

I changed dir from "All Text Files" to "test", now command is successful. If I have a dir name with spaces in it, can it be run or do I need to have dir name without spaces only ?

Thanks
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
If you lump together the whole command, you risk the file's being taken as several arguments if it contains one or more space characters. Try a String array like this:


nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 364
Now, this command runs successfully, but I can not see the output. If I want output of the script printed, what shall I do ?
I have tried following

I am getting following output with this code
"Command Successful
java.lang.ProcessImpl@5f186fab"

How can I get what testFile2.pl outputs upon being executed ?

Thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

My guess - and it is only a guess - is that you need to get the output stream of the process, read it, and then print that.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
I hope you haven’t tried Runtime.exec() without reading the classic Michael Daconta article “When Runtime.exec() won’t”. The ProcessBuilder class makes the process slightly easier.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7526
    
  18

Campbell Ritchie wrote:I hope you haven’t tried Runtime.exec() without reading the classic Michael Daconta article “When Runtime.exec() won’t”. The ProcessBuilder class makes the process slightly easier.

@nirjari: Or indeed, find another way of doing this. I'm sure others will jump all over me here, but what you're doing just doesn't sound right.

Either this a Java program whose sole purpose is to process the output of this perl script, or it's part of a larger system of which this is only a tiny part.

If it's the first, why not simply pipe the output of your perl script to your Java program?
If it's the latter, it might be worth considering whether you should either:
(a) Re-write this perl script in Java.
(b) Put the output of this script in a known file.

Maybe I'm just behind the times; but I get very leery about things like Runtime.exec()'s and ProcessBuilder's.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Executing Perl script from Java program
 
Similar Threads
using java to run a perl script
Using java configuration file to call a python script
calling a perl script from java program
waitFor method of a process object not working
problem with waiting for a process