将变量传递给另一个JAVA类中的主函数

将变量传递给另一个JAVA类中的主函数,java,variables,main,args,Java,Variables,Main,Args,在Helloworld.java类的主函数中,我创建了一个字符串或对象。然后,我创建了另一个类HelloAnotherClass.java。我想将变量从Helloworld main()传递到HelloAnotherClass.java的主函数 package helloworld; public class HelloAnotherClass { public static void main(String[] args) throws IOExcepti

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

如果您运行两个独立的程序并且希望它们交换数据,请阅读。

如果您运行两个独立的程序并且希望它们交换数据,请阅读。

这取决于您希望如何运行另一个类

如果您想在当前JVM中运行它,那么您可以采用以下明显的方式:

  • 创建包含参数的字符串数组
  • 使用数组作为参数调用
    main
    方法
如果您想在新的JVM中运行它,那么可以使用System.exec(…)(或等效命令)和一个命令字符串,就像您自己从命令行运行
java
一样。(如果参数字符串包含空格,则需要使用特定的Java安装,需要使用相同的JVM选项,等等……这将更加复杂。)

这两种方法各有优缺点:

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

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


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

这取决于您希望如何运行另一个类

如果您想在当前JVM中运行它,那么您可以采用以下明显的方式:

  • 创建包含参数的字符串数组
  • 使用数组作为参数调用
    main
    方法
如果您想在新的JVM中运行它,那么可以使用System.exec(…)(或等效命令)和一个命令字符串,就像您自己从命令行运行
java
一样。(如果参数字符串包含空格,则需要使用特定的Java安装,需要使用相同的JVM选项,等等……这将更加复杂。)

这两种方法各有优缺点:

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

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



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

默认的
main
将参数作为
String[]
。更新HelloAnotherClass,如下所示:

public class HelloWorld { 

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

  public static void main(String[] coming) throws IOException, ParseException {  
    System.out.println(coming[0]); 
  }
}
如果您尝试在
HelloAnotherClass
类中打印参数,则如下所示:

public class HelloWorld { 

   /** * @param args the command line arguments */ 
   public static void main(String[] args) { 
        HelloAnotherClass.main(new String[]{"example"}); 
   }
}
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); 
    }
 }

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

默认的
main
将参数设置为
String[]
。更新HelloAnotherClass,如下所示:

public class HelloWorld { 

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

  public static void main(String[] coming) throws IOException, ParseException {  
    System.out.println(coming[0]); 
  }
}
如果您尝试在
HelloAnotherClass
类中打印参数,则如下所示:

public class HelloWorld { 

   /** * @param args the command line arguments */ 
   public static void main(String[] args) { 
        HelloAnotherClass.main(new String[]{"example"}); 
   }
}
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); 
    }
 }

如果您想了解其他详细信息,请告诉我。

这是HelloWorld,有一些参数,(我通过了“Hello Crazy World”)

这是他另一个世界

public static void main(String args[]) {        
    args[1]="foobar";
}
由于main在HelloAnotherWorld中是静态的,因此可以将main方法调用为HelloAnotherWorld.main(“array goes here”);
还要注意,main返回void,因此任何传递原语并返回它的尝试都将失败,除非您重载了main方法。

这是HelloWorld,带有一些参数,(我传递了“Hello疯狂世界”)

这是他另一个世界

public static void main(String args[]) {        
    args[1]="foobar";
}
由于main在HelloAnotherWorld中是静态的,因此可以将main方法调用为HelloAnotherWorld.main(“array goes here”); 还要注意,main返回void,所以任何传递原语并返回原语的尝试都不会成功,除非您重载了main方法