【问题标题】:Passing variables to main function in another JAVA class将变量传递给另一个 JAVA 类中的主函数
【发布时间】:2012-10-04 11:44:11
【问题描述】:

在我的 Helloworld.java 类的主函数中,我创建了一个字符串或一个对象。然后,我创建了另一个类 HelloAnotherClass.java。我想将我的变量从 Helloworld main() 传递给 HelloAnotherClass.java 的主函数。

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

如何使用参数或其他数据结构将变量从 HelloWorld main() 发送到 HelloAnotherClass main(),然后再将其返回到 HelloWorld.java?简而言之,我想将另一个 java 类的 main() 函数用作 HelloWorld 类中的函数。

这是我写的代码示例

    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); 
    }

【问题讨论】:

    标签: java variables main args


    【解决方案1】:

    这取决于如何你想运行另一个类。

    如果你想在当前的 JVM 中运行它,那么你可以通过显而易见的方式来做:

    • 创建一个包含参数的字符串数组。
    • 以数组为参数调用main方法。

    如果你想在新的 JVM 中运行它,那么你可以使用 System.exec(...) (或等效的)和一个命令字符串,就像你自己从命令行运行 java 一样. (如果参数字符串包含空格,你想使用特定的 Java 安装,你想使用相同的 JVM 选项,等等......这会更复杂。)

    这两种方法各有优缺点:

    • 调用另一个类 main 可以让您快速“启动”时间,但是:

      1. 另一个类不会有一组独立的System.in/out/err 流,因为它与原始main 类共享静态,
      2. 如果调用System.exit()整个JVM都会退出,
      3. 如果它行为不端,原来的main 类可能无法摆脱它,以此类推。
    • 启动单独的 JVM 会导致启动时间明显变慢,但子 JVM 将无法干扰父 JVM。


    顺便说一句,您最初尝试失败的原因是您传递的是字符串而不是字符串数组。编译器不会让你这样做...

    【讨论】:

      【解决方案2】:

      默认的main 接受参数为String[]。更新HelloAnotherClass如下:

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

      如果您尝试打印 HelloAnotherClass 类中的参数,它将如下所示:

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

      如果你想有另一个以String为参数的main方法,也可以这样做:

      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); 
          }
       }
      

      如果您正在寻找其他/其他详细信息,请告诉我。

      【讨论】:

        【解决方案3】:

        这是 HelloWorld,带有一些参数,(我通过了“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());
        }
        
        }
        

        这是HelloAnotherWorld

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

        由于在 HelloAnotherWorld 中 main 是静态的,你可以调用 main 方法为 HelloAnotherWorld.main("array goes here"); 另请注意,main 返回 void,因此任何传递原语并返回它的尝试都将失败,除非您重载了 main 方法。

        【讨论】:

          【解决方案4】:

          如果您正在运行两个独立的程序并希望它们交换数据,请阅读Inter-process communication

          【讨论】:

            猜你喜欢
            • 2014-04-02
            • 1970-01-01
            • 1970-01-01
            • 2021-04-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-03-15
            相关资源
            最近更新 更多