21

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

4
  • In eclipse you can do that in Run/Debug configuration for that class. Under tab (x)=Arguments. Commented Jan 6, 2012 at 10:27
  • Similar way that you pass main class. Commented Jan 6, 2012 at 10:30
  • i know that i am initializing another VM inside another launched VM and for that VM i m passing arguments as main class i need to pass some arguments and this launch VM is going to run main class n i need a mechanism to pass those arguments at the beginning and the arguments which i am goign to pass for main class is class and its function. Commented Jan 6, 2012 at 10:36
  • Can you please update the question such that it includes waht you have mentioned in the above comment? Commented Jan 6, 2012 at 10:39

4 Answers 4

58

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
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please give me it with example.
no i got this example but this not the way i want to do.i posted the code what i wanna do.
4
Run ---> Debug Configuration ---> YourConfiguration ---> Arguments tab

enter image description here

1 Comment

No i am already running a main class and from this main class i am initializing another VM and to instantiate another VM we need some connector arguments so that it can launch that VM. and this man class needs to invoke another class's function .so how do i pass it?
3

We can pass string value to main method as argument without using commandline argument concept in java through Netbean

 package MainClass;
 import java.util.Scanner;
 public class CmdLineArgDemo {

static{
 Scanner readData = new Scanner(System.in);   
 System.out.println("Enter any string :");
 String str = readData.nextLine();
 String [] str1 = str.split(" ");
 // System.out.println(str1.length);
 CmdLineArgDemo.main(str1);
}  

   public static void main(String [] args){
      for(int i = 0 ; i<args.length;i++) {
        System.out.print(args[i]+" ");
      }
    }
  }

Output

Enter any string : 
Coders invent Digital World 
Coders invent Digital World

1 Comment

As for what I was looking for, this suits perfectly, thanks!
0

go to Run Configuration and in argument tab you can write your argument

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.