views:

572

answers:

1

I want to pass the string "!changeme!" to a java program on the command line like:

java -cp "!AXIS2_CLASS_PATH!" ClientJava --userid admin --passwd "!changeme!"

Using Windows XP, Java jdk 1.6.0_07.

The AXIS2_CLASS_PATH gets replaced as normal, I assume by the java runtime. However the password of !changeme! also seems to be replaced with an empty string. I assume that this replacement is some sort of JVM feature.

Using the following program:

static int Run(String[] aArgs) {
    for (String s: aArgs) {
        System.out.println("arg: " + s);
    }
    return 0;
}

I get the following results:

"C:\Program Files\Java\jdk1.6.0_07\bin\java" -cp "!AXIS2_CLASS_PATH!"
    ClientJava --userid admin --passwd "!changeme!"
arg: --userid
arg: admin
arg: --passwd
arg:

I need the password to be passed through as is. I've tried all sorts of escaping but I haven't found what I need to use.

Can anyone supply a hint on how to do this?

Solution as provided by Zach Scrivena is:

Use the caret to escape the exclamation mark.

java -cp "!AXIS2_CLASS_PATH!" ClientJava --xxx "^!changeme^!"

arg: --userid
arg: admin
arg: --passwd
arg: !changeme!
+3  A: 

The problem is caused by the command-line interpreter --- it's replacing the term enclosed in exclamation marks ! with the corresponding environment variable.

Simply precede each escaped character by a caret ^. This works for both the command-line and batch files:

java -cp "!AXIS2_CLASS_PATH!" ClientJava --xxx "^!changeme^!"

On the command-line, you can also enclose each escaped character in double quotes "

java -cp "!AXIS2_CLASS_PATH!" ClientJava --xxx ""!"changeme"!""
Zach Scrivena
--passwd ""!"changeme"!"" doesn't work (= " a single double quote), but --passwd "^!changeme^!" does result in the desired string. Thank you.
brofield
What is the difference between %FOO% and !FOO! ?
Thorbjørn Ravn Andersen