简体   繁体   English

从其他类运行main方法

[英]running main method from other class

I've been trying to run main method from other class. 我一直在尝试从其他类运行main方法。 I know that swing is single threaded. 我知道秋千是单线程的。 So i'm using this code 所以我正在使用这段代码

private static void runUpdate(){
    SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        String[] args1={"10"};
        Update.main(args1);
    }
});
}

private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
    runUpdate();
}  

this code is in InventoryMgr class and i want to run main method from Update class. 此代码在InventoryMgr类中,我想从Update类运行main方法。

I got StackOverFlowError 我有StackOverFlowError

Look through the stack trace in your StackOverFlowError. 在您的StackOverFlowError中查看堆栈跟踪。 It will show you what method called what, and on what line number. 它将告诉您什么方法称为什么,以及在什么行号上。 The problem is that your main() calls a method which calls a method which calls main() which calls ... 问题是您的main()调用一个方法,该方法调用一个调用main()的方法,而main()调用...

The solution is to pull the functionality you need to repeat out of main() and put it in a separate method. 解决方案是从main()中提取需要重复的功能,并将其放在单独的方法中。 Then you can call it without causing infinite recursion. 然后,您可以调用它而不会引起无限递归。

It is legal to call main(), and it is not really special other than the JVM will call it to begin your program. 调用main()是合法的,并且除了JVM会调用它来启动您的程序外,它并不是真正的特殊。 However, it is unusual to actually want to call main() again. 但是,实际要再次调用main()是不寻常的。

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

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