• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executing Perl script from Java program

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try escaping the \
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12102
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10277
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roban Ionut-Dragos
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should try "ProcessBuilder"
It looks like this for example:

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic