This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 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: 386
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: 386
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: 15002
    
  27

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 8 API documentation
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 386
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: 969
    
    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: 386
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: 11884
    
  18

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: 44616
    
  34
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: 8663
    
  24

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
Roban Ionut-Dragos
Greenhorn

Joined: Aug 27, 2015
Posts: 2
You should try "ProcessBuilder"
It looks like this for example:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44616
    
  34
Welcome to the Ranch

Always use code tags when you post code. I shall add them this time, and doesn't it look better
If you comment on old discussions, you may not get a response from the original poster (OP).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Executing Perl script from Java program
 
It's not a secret anymore!