I'm no java whiz, so maybe that's why I find your java syntax to be pretty confusing:
Process p = Runtime.getRuntime().exec("c:/perl.exe" C:/CreateER.pl "Please \n ignore \n my friend");
Why no comma after "c:/perl.exe", or after C:/CreateER.pl? For that matter, why does the java code use C:/CreateER.perl, whereas your working shell command ("running it directly") has C:/scripts2/CreateER.pl ?

I gather that java is interpreting the "\n" in the last arg, and outputting actual line-feed characters in their place, which I would expect to make a mess of some sort.

I don't know if this is doable for you, but the next thing I would try would be to open the perl process as a pipeline file handle, and print to that file handle the string that contains any given number of line-feed characters, so that perl script reads this from its STDIN (rather than getting it in @ARGV).

I don't know how that would look in java (or whether you can do it at all) -- it would be done like this in perl:

my $pid = open( PROC, "|-", "c:/perl.exe c:/scripts2/CreateER.pl" ) or die "cannot launch perl process: $!"; print PROC "anything\nyou\nwant\nto\ndo\n"; close PROC;
Another possibility: print the multi-line string in question to a file (surely java can do that), and then have the "exec()" call run the perl script with the name of the file, so perl can read the lines with while(<>) or whatever.

Or, maybe try just making the string look like this in java:

Please \\n ignore \\n my friend
So that the string ends up being a single line with literal "backslash""n" sequences in it, which perl can then interpret as intended.

UPDATE: It sounds like you haven't tried one of the other ideas in my initial reply: before passing your multi-line string to the exec call as a command-line arg for your perl script, convert the "\n" characters (or literal "backslash""n" sequences) to something safe and innocuous (like "=:=" or anything that won't ever be confused with other stuff in the string), and modify the perl script so it knows to convert this back to "\n" when it gets the string out of @ARGV.


In reply to Re^3: How to keep the line breaks in the arguments when I invoke Perl? by graff
in thread How to keep the line breaks in the arguments when I invoke Perl? by henrycui

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.