1

I have written a command line script and I am testing that using java.

In command line I write like this: script -html="anchor bold" so while testing the same using java code, how exactly should my string be outlined?

-html=anchor bold" it doesn't work as double quotes dont get included so my test fails

-html=\"anchor bold\" this escapes double quotes but I want it.

-html=\\\"anchor bold\\\" this results in -html=\"anchor bold\" but I don't want any slashes.

I am using a String[] array to execute the command.

new String[] { "--path=" + p,
               "--user=" + user,
               "--html=\"anchor bold\"",
               "--port=8081"
            }

The command line argument is:

path/executeScript --path=path1 --user=testUser --html="anchor bold" --port=8081
Unihedron
  • 10,902
  • 13
  • 62
  • 72
fscore
  • 2,567
  • 7
  • 40
  • 74
  • Try: -html=' "anchor bold" ' Without the spaces between the double and single quotes. I just spaced it out so you could tell what was going on. – jordaniac89 Jan 09 '15 at 18:01
  • Could we have more details ? How is `-html="anchor bold"` used in your class ? Show us your script detail. – ToYonos Jan 12 '15 at 15:24
  • @ToYonos added more details please check – fscore Jan 12 '15 at 16:00
  • The code you have in the question looks right. – mbomb007 Jan 12 '15 at 16:11
  • I'm sure we can help with this, but we need to know: A little more context to the code - how are you trying to run the script? Using Runtime.getRuntime().exec(yourCommandString)? Is the first command line the one you want in yourCommandString? Is the second the way you want to invoke your Java? What is wrong with your solution that uses "--html=\"anchor bold\""? Why doesn't that work for you? – J Richard Snape Jan 14 '15 at 13:02
  • Note (Jan. 2018), raw string literals might be coming for Java (JDK 10 or more): see [In Java, is there a way to write a string literal without having to escape quotes?](https://stackoverflow.com/a/48481601/6309). – VonC Jan 27 '18 at 23:28

4 Answers4

11

If you want to to test your script with Java, with parameters containing quotes, you don't have any choice, you'll have to escape it.

String[] command  = new String[] {
    "path/executeScript",
    "--path=" + p,
    "--user=" + user,
    "--html=\"anchor bold\"",
    "--port=8081"
}
Runtime.getRuntime().exec(command);

Technical explanation : https://stackoverflow.com/a/3034195/2003986

Community
  • 1
  • 1
ToYonos
  • 16,469
  • 2
  • 54
  • 70
4

To declare a String literal, the only way to do it is with double quotes surrounding characters in the String. Like this:

"String Characters"
... String Characters

From the Java Language Specifications §3.10.5:

A string literal consists of zero or more characters enclosed in double quotes. Characters may be represented by escape sequences (§3.10.6) - one escape sequence for characters in the range U+0000 to U+FFFF, two escape sequences for the UTF-16 surrogate code units of characters in the range U+010000 to U+10FFFF.

StringLiteral:
  " {StringCharacter} "
StringCharacter:
  InputCharacter but not " or \ 
  EscapeSequence
See §3.10.6 for the definition of EscapeSequence.

[...]

The following are examples of string literals:
""                    // the empty string
"\""                  // a string containing " alone
"This is a string"    // a string containing 16 characters
"This is a " +        // actually a string-valued constant expression,
    "two-line string"    // formed from two string literals

Because escaping the double quotes is a compile-time syntax requirement, you must escape the double quotes in a String literal.

You can try for a Unicode literal, but I highly doubt (spoilers: it doesn't) the compiler would accept it:

"--html=\u0022anchor bold\u0022" — Wrong

Use an escape:

"--html=\"anchor bold\"" — Right

Of which is treated by the compiler as:

... --html="anchor bold"

See also:

Community
  • 1
  • 1
Unihedron
  • 10,902
  • 13
  • 62
  • 72
2

Use the escape character in front of anything you want to use as a literal character, then put the double quotes around it as usual.

script -html="\"anchor bold\""
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

A way I could think of.. Using characters.

"--html=" + '"' + "anchor bold" + '"'
Bubletan
  • 3,833
  • 6
  • 25
  • 33
  • Eh, unfortunately `String + char` concatenation process will implicitly downcast the `char` into an `int`. In this case, `"` becomes `22`, so you'll get `--html=22anchor bold22`. – Unihedron Jan 19 '15 at 09:44
  • If you write `System.out.println('"' + "string" + '"');` It prints out: `"string"`... Not `22string22`. – Bubletan Jan 19 '15 at 18:02