简体   繁体   中英

Data truncation when '$' or '&' is used as argument in java command line

I am getting data truncation when I pass '$' or '&' as part of data in my java arguments. I know that issue can be resolved by using escape character '\\' before '$' but I don't understand why we are seeing this only for '$' or '&' and not for any other special characters like '@','#','%','^' or'*'

Following is my standalone test case to reproduce this error

import java.util.Arrays;
public class Main{

    public static void main(String [] args)
    {
        System.out.println(Arrays.toString(args));
        System.out.println(args[0]);
    }
}

Input

java Main name=Daniel$Burgers

Output

[name=Daniel]
name=Daniel

This isn't a Java issue - it's your shell; the command line from which you're running the code. $ is usually used for variable substitution in shells; & is usually used for background processes. You'd see the same using echo:

$ echo name=Daniel$Burgers

Output:

name=Daniel

If you put the value in single quotes, that tells the shell not to do anything special until the end of the string (another single quote):

$ echo 'name=Daniel$Burgers'

Output:

name=Daniel$Burgers

The same change should fix your Java invocation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM