Reputation: 151
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);
}
Upvotes: 1
Views: 9063
Reputation: 719679
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:
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:
System.in/out/err
streams because it shared statics with the original main
class,System.exit()
the entire JVM will exit, 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 ...
Upvotes: 1
Reputation: 34397
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.
Upvotes: 0
Reputation: 2629
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.
Upvotes: 0
Reputation: 117675
If you are running two independent programs and you want them to exchange data, then read about Inter-process communication.
Upvotes: 0