Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass command line arguments to main method dynamically

Tags:

java

eclipse

I am passing my main class as a command line argument to launch VM

Now i need to pass command line arguments to that main class

Is there any way to do this?

this is the way i am doing it

    VirtualMachineManager manager = Bootstrap.virtualMachineManager();
    LaunchingConnector connector = manager.defaultConnector();
    Map arguments = connector.defaultArguments();
    ((Connector.Argument)arguments.get("options")).setValue(userVMArgs);
    ((Connector.Argument)arguments.get("main")).setValue(cmdLine);

here userVMargs is classpath of my main class and the also classpath of the class which is being used to invoke the method of class inside my main class

and cmdLine is having my main class along with the class and its function and i am using eclipse as IDE to develop my project

like image 561
mum Avatar asked Jan 06 '12 10:01

mum


People also ask

Can command line arguments be converted into automatically if required?

Can command line arguments be converted into int automatically if required? Explanation: All command Line arguments are passed as a string. We must convert numerical value to their internal forms manually.

How many arguments can be passed to main () using command line arguments?

Explanation: Infinite number of arguments can be passed to main(). 4.

What arguments can be passed to main function?

The argc and argv are the two arguments that can pass to main function. But main() function is actually called by the operating system (or shell program) when you run the program from the terminal.


2 Answers

If you want to launch VM by sending arguments, you should send VM arguments and not Program arguments.

Program arguments are arguments that are passed to your application, which are accessible via the "args" String array parameter of your main method. VM arguments are arguments such as System properties that are passed to the JavaSW interpreter. The Debug configuration above is essentially equivalent to:

java -DsysProp1=sp1 -DsysProp2=sp2 test.ArgsTest pro1 pro2 pro3

The VM arguments go after the call to your Java interpreter (ie, 'java') and before the Java class. Program arguments go after your Java class.

Consider a program ArgsTest.java:

package test;

import java.io.IOException;

    public class ArgsTest {

        public static void main(String[] args) throws IOException {

            System.out.println("Program Arguments:");
            for (String arg : args) {
                System.out.println("\t" + arg);
            }

            System.out.println("System Properties from VM Arguments");
            String sysProp1 = "sysProp1";
            System.out.println("\tName:" + sysProp1 + ", Value:" + System.getProperty(sysProp1));
            String sysProp2 = "sysProp2";
            System.out.println("\tName:" + sysProp2 + ", Value:" + System.getProperty(sysProp2));

        }
    }

If given input as,

java -DsysProp1=sp1 -DsysProp2=sp2 test.ArgsTest pro1 pro2 pro3 

in the commandline, in project bin folder would give the following result:

Program Arguments:
  pro1
  pro2
  pro3
System Properties from VM Arguments
  Name:sysProp1, Value:sp1
  Name:sysProp2, Value:sp2
like image 151
Manikandan Sigamani Avatar answered Oct 23 '22 03:10

Manikandan Sigamani


Run ---> Debug Configuration ---> YourConfiguration ---> Arguments tab

enter image description here

like image 4
aleroot Avatar answered Oct 23 '22 05:10

aleroot