• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Executing Perl script from Java program

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try escaping the \
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should try "ProcessBuilder"
It looks like this for example:

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic