0

I write a lot of java tools which get fed information from the command line, which java provides access to via main(args[]). The apps in question are typically Windows tools (so running from command line, or batch files).

From what I have learned through painful experience, Windows does its own (sometimes whacky) conversions and transformations on the argument data depending on what actually called it. Which then gets palmed off onto Java, which does its own parsing and processing, eventually presenting you with args[]. It's this multiple levels of parsing\pre-processing that is currently causing me grief, as it isn't always clear where things are being mangled.

My first question is what actual parsing\conversion does Java actually do before getting to args? From what I can gather, it just takes the argument data supplied by the OS (which I understand is just one long string), and delimits it based on white space (tab, space chars). Is it that simple?

Second question, is it possible to get the unprocessed command line data, as it is passed to Java? While having the args broken up by spaces is often convenient, it falls apart when you start adding more complicated arrangements. So "-foo=bar" might end up as "-foo=" "bar", or "-foo" "=" "bar", or "-foo" "=bar" depending on which way the white-space winds have blown. If the assumptions made in my first question are correct, then you can just build a string by inserting a space between each element in args, but if other processing is done then accessing the raw args becomes more useful.

As a final note, I am aware that there are already a host of libraries available which process command line data in Java. I should probably start using one of those at some point, but for now I just want to understand exactly what is being done, and what alternatives (if any) Java itself provides.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
user3404036
  • 85
  • 1
  • 4
  • 1
    "what actual parsing\conversion does Java actually do before getting to args" None. You get whatever is passed by your shell. – Andy Turner Dec 04 '15 at 06:57
  • `So "-foo=bar" might end up as "-foo=" "bar", or "-foo" "=" "bar", or "-foo" "=bar"` - do you have a working example for that which demonstrates that `-foo=bar` is passed as anything else than `"-foo=bar"` in `args[]`? – Andreas Fester Dec 04 '15 at 07:07
  • See also http://stackoverflow.com/questions/27923366/how-does-the-jvm-determine-the-default-character-encoding-for-argv-on-linux – Raedwald Dec 04 '15 at 09:34

2 Answers2

0

The arguments are parsed by the shell and passed in to your program. Java doesn't do any extra parsing.

Buddy
  • 10,874
  • 5
  • 41
  • 58
-1

you can get the whole line, with some special tricks

use

ManagementFactory.getRuntimeMXBean()

and

getInputArguments()

thanks to: How do I get the commandline that started the process

Community
  • 1
  • 1