Results 1 to 12 of 12
Like Tree3Likes
  • 1 Post By kjkrum
  • 2 Post By Norm

Thread: Problem encountered When the char '*' is passed as command line argument-Please help!

  1. #1
    omripe is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default Problem encountered When the char '*' is passed as command line argument-Please help!

    Hi Everyone
    I am writing a console application in Java and recieving a strange problem:
    The user is passing command line argument's when he start's the application.
    If the user passed the following argument's: -hostName as* it means that he want's to check all the host names(here '*' has the regular expression meaning '.*') that start with 'as'
    The problem is, that if there exist a file that start's with the name 'as' - in the utility directory - then the file name will be passed instead of the argument 'as*' so I will recieve - for example -
    -hostName,asp.txt .
    I found that only if I surround the argument with ' ' then The problem doesn't occure.
    What is the problem? How can I solve this??
    Thanks!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    I'm not completely sure you can solve this with just Java, as that's not a Java problem- that's an OS problem. Maybe change the character you use for wildcards?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    omripe is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default

    I dont think my manager want's to change it - I tried that approach
    Perhaps wrap the app with some kind of script that will wrap each argument with ' ' chars?
    But the script might have the same problem right? - It's related to the shell..
    What do you suggest besides replacing the character?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    Honestly I'm not really sure, as this isn't really a Java question. How exactly does the user run the Java program? From the command prompt? By running a script? Something else?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    omripe is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default

    It will be done with a script

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    You answered your own question:

    I found that only if I surround the argument with ' ' then The problem doesn't occure.
    Your later comment is also correct: this is related to the shell. Shell expansion of wildcards is a common problem, and surrounding arguments with single quotes is the standard solution. For example, if you want to use wildcards with the UNIX find command, you type something like

    Java Code:
    find ./ -name '*.txt'
    That's just how it works. I don't understand why you're seeking a different solution.
    JeffGrigg likes this.
    Get in the habit of using standard Java naming conventions!

  7. #7
    omripe is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default

    Hi kjkrum,
    So a script that will activate the program will have the same problem right?

    And also,
    What if the user want's to enter an argument with a space and a wild card? that will force him to use " " and ' ' chars right ? (I tried only ' ' with that option like: -hostName 'as *rty' but the argument's were splitted... So the only thing that worked was: -hostName "'as *rty'" and that is too much to ask from the user I think)
    Thanks

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Change the app to prompt for the args. If you read them, the OS won't touch them.
    JeffGrigg and kjkrum like this.

  9. #9
    omripe is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default

    That is a really nice solution - I will try this approach but I don't think my boss will agree..
    This utility is part of a family of utilities that use command line argument's and they don't want it to be diffrent..
    Maybe I'll try to change his mind about replacing the '*' with another character..

  10. #10
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    In bash, you'd only need to type single quotes around an argument like 'ab *c' to pass it unchanged to your program. Typing "'ab *c'" would make the single quotes literally part of the argument, but not expand the wildcard. Strangely, typing '$FOO' would cause your program to literally receive $FOO, but typing "'$FOO'" would make the single quotes part of the argument and replace $FOO with the environment variable FOO!

    I'm sure other shells behave quite differently. Exactly what the user has to type depends on what shell they're using, and you cannot anticipate all the possibilities. It's ultimately up to the user to know how to use their own shell correctly.

    It's also completely impossible for a script to correct a user's wildcard quoting mistakes. The shell will expand the wildcards before passing the arguments to the script interpreter, and the script will never see what the user actually typed.

    I think Norm's suggestion is the only portable one. If that won't work, all you can do is provide your users with documentation for how to use their shell properly.
    Last edited by kjkrum; 08-05-2011 at 06:47 PM.
    Get in the habit of using standard Java naming conventions!

  11. #11
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    This utility is part of a family of utilities that use command line argument's and they don't want it to be diffrent..
    I'm guessing the other utilities either don't use arguments with wildcards, or don't actually work the way "they" think they do...
    Get in the habit of using standard Java naming conventions!

  12. #12
    omripe is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Command line argument
    By denisatandi in forum New To Java
    Replies: 8
    Last Post: 10-16-2012, 11:37 PM
  2. Replies: 4
    Last Post: 10-31-2010, 05:19 PM
  3. Maximum length of Command Line Argument
    By balamurugan.murthy in forum Advanced Java
    Replies: 11
    Last Post: 05-21-2010, 04:45 PM
  4. Command line argument help...
    By arson09 in forum New To Java
    Replies: 4
    Last Post: 05-06-2010, 05:49 PM
  5. Command Line Argument
    By right2001 in forum New To Java
    Replies: 6
    Last Post: 02-17-2009, 02:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •