简体   繁体   English

在Java中是否可以调用另一个类的main方法并返回到调用代码?

[英]Is it possible in Java to Invoke another class' main method and return to the invoking code?

public class A{
    public static void main(String[] args)
    {
        //Main code
    }
}

public class B{
    void someMethod()
    {
        String[] args={};
        A.main();
        System.out.println("Back to someMethod()");
    }
}


Is there any way to do this? 有没有办法做到这一点? I found a method of doing the same using reflection but that doesn't return to the invoking code either. 我找到了一种使用反射做同样的方法,但也没有返回到调用代码。 I tried using ProcessBuilder to execute it in a separate process but I guess I was missing out something. 我尝试使用ProcessBuilder在一个单独的进程中执行它,但我想我错过了一些东西。

Your code already nearly does it - it's just not passing in the arguments: 你的代码已经差不多了 - 它只是没有传入参数:

String[] args = {};
A.main(args);

The main method is only "special" in terms of it being treated as an entry point. 就被视为切入点而言, main方法只是“特殊的”。 It's otherwise a perfectly normal method which can be called from other code with no problems. 它是一种非常普通的方法,可以从其他代码中调用而没有任何问题。 Of course you may run into problems if it's written in a way which expects it only to be called as an entry point (eg if it uses System.exit ) but from a language perspective it's fine. 当然,如果它以一种期望它只被称为入口点的方式编写(例如,如果它使用System.exit ),那么你可能会遇到问题,但从语言的角度来看,这很好。

Yes you can do call A.main() . 是的,你可以打电话给A.main()

You can do this: 你可以这样做:

String[] args = {};
A.main(args);

If you don't care about the arguments, then you can do something like: 如果您不关心参数,那么您可以执行以下操作:

public static void main(String ... args)

and call it with: 并称之为:

A.main(); //no args here

There's nothing magic about the name "main". 名字“主”没有什么神奇之处。 What you've sketched ought to work, so your problem must be something else. 你所描绘的应该是什么,所以你的问题必须是别的。 Test my claim by changing the name of "main" to something else, I bet it still doesn't work. 通过将“main”的名称更改为其他内容来测试我的声明,我敢打赌它仍然不起作用。

Actually, you can call main method like the way you have just asked but not the way you have done it. 实际上,你可以像你刚才问的那样调用main方法,而不是你做的方式。 First, every execution process starts from the main method. 首先,每个执行过程都从main方法开始。 So, if you want to do it the way you want you can do right this way. 所以,如果你想按照你想要的方式去做,你就可以这样做。 This code will execute Hello! World 这段代码将执行Hello! World Hello! World eight times: Hello! World八次:

class B
{
  B()
  {
    while(A.i<8)
    {
      String a[]={"Main","Checking"};
      A.main(a);
    }
    System.exit(0);
  }

}

class A
{
  public static int i=0;
  public static void main(String args[])
  {
    System.out.println("Hello! World");
    i++;
    B ob=new B();
  }
}

` The iteration part, you can leave it if you want. `迭代部分,你可以保留它。 I Hope I gave you the solution just the way you wanted. 我希望我能按你想要的方式给你解决方案。 :) :)

Of course. 当然。 String[] args = {}; String [] args = {}; A.main(args); a。主(参数);

Just be aware: from purely what you have up there, the main method is still the entry point to the program. 请注意:从纯粹的东西来看,主要方法仍然是程序的切入点。 Now, if you are trying to run the main method in a new PROCESS, this is a different story. 现在,如果您尝试在新的PROCESS中运行main方法,这是一个不同的故事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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