• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Am i right in thinking this is how main(String[] args) works?

Say im at a windows command prompt im at the right path etc and i type "start somejavaapp -PortNumber 1088 -Name john"

Will somejavaapp have "1088" and "john" stored in the String array args when it starts up? Is that how it works?

If so where do the -PortNumber and -Name identifiers come into it?
 
I believe that "-PortNumber", "1088", "-Name" and "john" will be in the args array. It's up to the method implementation to parse and validate the array and pair the values together.
 
I believe that "-PortNumber", "1088", "-Name" and "john" will be in the args array. It's up to the method implementation to parse and validate the array and pair the values together.

I see, so the array would look like ["-PortNumber", "1088", "-Name", "john"] in that order?
 
IIRC the first arg is always the name of the executable (including its full path?). Everything after that is split based on spaces and dumped into separate args.

A simple test program will give a definite answer:

Code:
public class TestArgs {
  public static void Main(String[] args) {
    for (String s : args) {
      System.out.println(s);
    }
  }
}
 
The application is responsible for interpreting the parameters and arguments. For example, .NET has methods to directly access the key/value pair. This can either be done by index or name value.
 
Very cool, so thats how it works with a command line... for a GUI, windows for example the equivalent is having "-something" in one of these boxes? I remember sticking stuff in these types of boxes to solve problems quite a few times.

TX0skx1.png


And I guess linux and other OS's have a similar facility for GUI's to do this. Always wondered how adding stuff like that to the programs shortcut would change anything, now i know 🙂

Every program that has different options like that must have some sort of check for certain arguments in its main method, i guess maybe check if args == null to begin with and go from there.
 
Every program that has different options like that must have some sort of check for certain arguments in its main method, i guess maybe check if args == null to begin with and go from there.

There are a number of ways that a program can receive options settings at runtime. Every Windows process and every Linux process can receive options passed on the command line, as you note (and all the GUI example you posted is doing is passing them behind the scenes ). Programs can also read local configuration files (.ini or the registry in windows, often .conf in Linux). A program could even ping an external server on the Internet for its runtime parameters, which is what malware often does.
 
Back
Top