• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

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
0
angelblade27
Asked:
angelblade27
  • 5
  • 4
  • 2
  • +1
1 Solution
 
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
 
CPColinCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
malfunction84Commented:
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:
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now