We help IT Professionals succeed at work.

run program from command line

komlaaa
komlaaa asked
on
Medium Priority
247 Views
Last Modified: 2010-03-31
Hi,
i have a void method(NO Argument) has local variables. i would like to test this
method by imputing these local variables from command line.
Is there a way to do this?

Thanks
Comment
Watch Question

Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
no

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Manikandan ThiagarajanSenior consultant

Commented:
that is not possible to give that argument for method in commandline

Commented:
You can only run the main method from the command line.
Take the variables from the command line and pass these to the  method you want to test from the main method.

public class MainClass{
    public static void main(String args[]){
        MyClassToTest m = new MyClassToTest();
        m.callMyMethodToTest(args[0],args[1],.......,args[n]);
    }
}

command line
java MainClass

Commented:
sorry

you need to initialise the local variables via the constructor

MyClassToTest m = new MyClassToTest(args[0],args[1],.......,args[n]);
m.callMyMethodToTest();


or you need to use Refection APIs to populate the local variables

Commented:
If the scope of the variables is the method, it cannot be done.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.