Link to home
Start Free TrialLog in
Avatar of komlaaa
komlaaa

asked on

run program from command line

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
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that is not possible to give that argument for method in commandline
Avatar of mrigank
mrigank

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
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
If the scope of the variables is the method, it cannot be done.