using commandline switch

i want to be able to pass a switch arugment to my java program eg
java foo.java --url=http://experts-exchange.com
so my program would see the --url= and take the rest of it
angelblade27Asked:
Who is Participating?
 
malfunction84Connect With a Mentor Commented:
The -D is a special argument that gets passed to the java executable, not to your program.  It sets a system property which can be retrieved by your Java program.  For instance, if you ran...

java -Durl=http://experts-exchange.com Foo

...then you could fetch the value of the "url" property in foo.java...

class Foo {
    public static void main(String[] args) {
        String url = System.getProperties().get("url");
        if (url != null) {
            System.out.println(url); //prints "http://experts-exchange.com"
        }
    }
}

However, if you want your program to take the arguments instead of the java executable, you should call it this way...

java foo -url=http://experts-exchange.com

...and process it this way...

class Foo {
    public static void main(String[] args) {
        String url = null;
        for (String arg : args) {
            if (arg.toLowerCase().startsWith("-url=")) {
                url = arg.replaceFirst("-url=", "");
            }
        }
        if (url != null) {
            System.out.println(url); //prints "http://experts-exchange.com"
        }
    }
}
0
 
CEHJCommented:
You can pass them


java -Durl=http://experts-exchange.com foo

or

java url= foo url=http://experts-exchange.com

0
 
CEHJCommented:
Sorry - 2nd one should have been

java foo url=http://experts-exchange.com
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CPColinSenior Java ArchitectCommented:
public static void main(String[] args)

The command line parameters are put into the args parameter in the order they appear on the command line. They're delimited by the spaces.
0
 
malfunction84Commented:
public static void main(String[] args) {
    String url = null;
    for (String arg : args) {
        if (arg.toLowerCase().startsWith("--url=")) {
            url = arg.replaceFirst("--url=", "");
        }
    }
    if (url != null) {
        //do something with the url
    }
}
0
 
malfunction84Commented:
Or, if you're not using Java 5+:

...
String url = null;
for (int i = 0; i < args.length; i++) {
    String arg = args[i];
    if (arg.toLowerCase().startsWith("--url=")) {
...
0
 
angelblade27Author Commented:
CEHJ@ just by passing it like that i should have it coded isn't it?
0
 
CEHJCommented:
Not sure what your question is, but in the first form of my answer, it sets a system property, which you can retrieve with

String url = System.getProperty("url");
0
 
angelblade27Author Commented:
What about the second form? the first one requires the D. i wish to have it in some format like -url=www.expert-exchange.com
0
 
CEHJCommented:
angelblade27, i wonder why my comments were totally ignored and unrewarded - it *is* possible to split points ...
0
 
malfunction84Commented:
I agree.  CEHJ deserves some credit.
0
 
CEHJCommented:
Thank you m. :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.