简体   繁体   中英

Passing variables to main function in another JAVA class

In my main function of Helloworld.java class, I created a string or an object. Then, I created another class HelloAnotherClass.java. I want to pass my variable from Helloworld main() to main function of HelloAnotherClass.java.

package helloworld;
        public class HelloAnotherClass {
           public static void main(String[] args) throws IOException, ParseException {
        //... for example print passed variables
        }

How can I send the variables from HelloWorld main() to HelloAnotherClass main() using arguments or another structure of data and then return it againt to HelloWorld.java? Briefly, I vant to use main() function of another java class as a function in HelloWorld class.

that is the code sample I wrote

    HelloWorld { 
    /** * @param args the command line arguments */ 
    public static void main(String[] args) { 
    HelloAnotherClass.main("example"); 
    } 

public class HelloAnotherClass { 
    public static void main(String coming) throws IOException, ParseException { 
System.out.println(coming); 
    }

It depends on how you want to run the other class.

If you want to run it in the current JVM, then you do it in the obvious way:

  • Create an array of strings, containing the arguments.
  • Call the main method with the array as a parameter.

If you want to run it in a new JVM, then you use System.exec(...) (or equivalent) with a command string that looks like it would if you were running java from the command line yourself. (If the argument strings contain spaces, you want to use a specific Java installation, you want to use the same JVM options, etcetera ... it will be more complicated.)

The two approaches have advantages and disadvantages:

  • Calling another classes main gives you fast "launch" times, but:

    1. the other class won't have an independent set of System.in/out/err streams because it shared statics with the original main class,
    2. if it calls System.exit() the entire JVM will exit,
    3. if it misbehaves, the original main class probably won't be able to get rid of it, and so on.
  • Launching a separate JVM will result in significantly slower launch times, but the child JVM won't be able to interfere with the parent JVM.


Incidentally, the reason the your initial attempt failed is that you are passing a String rather than an array of Strings. The compiler won't let you do that ...

如果您正在运行两个独立程序,并且希望它们交换数据,请阅读有关进程间通信的信息

The default main takes argument as String[] . Update HelloAnotherClass as below:

public class HelloWorld { 

   /** * @param args the command line arguments */ 
   public static void main(String[] args) { 
        HelloAnotherClass.main(new String[]{"example"}); 
   }
}

If you try to print the args in HelloAnotherClass class, it would be as below:

public class HelloAnotherClass { 

  public static void main(String[] coming) throws IOException, ParseException {  
    System.out.println(coming[0]); 
  }
}

If you want to have another main method with String as parameter, that also can be done:

public class HelloWorld { 

    /** * @param args the command line arguments */ 
    public static void main(String[] args) { 
       HelloAnotherClass.main("example"); 
    }
 }

 public class HelloAnotherClass { 

    public static void main(String coming) throws IOException, ParseException {  
       System.out.println(coming); 
    }
 }

Please let me know, if you are looking for some other/additional details.

This is HelloWorld, with some arguments, ( I passed "Hello Crazy World")

public static void main(String args[]) {
    //call static main method of Main2 class
    HelloAnotherWorld.main(args);
      //Modifcation made on Main2 is reflected
    System.out.println(args[1].toString());
}

}

This is HelloAnotherWorld

public static void main(String args[]) {        
    args[1]="foobar";
}

Since main is static in HelloAnotherWorld, you can call the main method as HelloAnotherWorld.main("array goes here"); Also note that , main returns void, so any attempt to pass primitives and return it would be unsuccessfull, unless you have overloaded main method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM