简体   繁体   English

Java:从另一个类的主方法调用主方法

[英]Java: Call a main method from a main method in another class

I have a set of Java files in the same package, each having main methods. 我在同一软件包中有一组Java文件,每个文件都有主要方法。 I now want the main methods of each of the classes to be invoked from another class step by step. 现在,我希望逐步从另一个类中调用每个类的主要方法。 One such class file is Splitter.java. 这样的类文件之一是Splitter.java。 Here is its code. 这是它的代码。

public static void main(String[] args) throws IOException {

    InputStream modelIn = new FileInputStream("C:\\Program Files\\Java\\jre7\\bin\\en-sent.bin");
    FileInputStream fin = new FileInputStream("C:\\Users\\dell\\Desktop\\input.txt");
    DataInputStream in = new DataInputStream(fin);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine = br.readLine();
    System.out.println(strLine);

    try {
        SentenceModel model = new SentenceModel(modelIn);
        SentenceDetectorME sentenceDetector = new SentenceDetectorME(model);
        String sentences[] = sentenceDetector.sentDetect(strLine);

        System.out.println(sentences.length);
        for (int i = 0; i < sentences.length; i++) {
            System.out.println(sentences[i]);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (modelIn != null) {
            try {
                modelIn.close();
            } catch (IOException e) {
            }
        }
        fin.close();
    }
}

I now want this to be invoked in AllMethods.java inside a main method. 我现在希望在主要方法的AllMethods.java调用AllMethods.java So how can I do this? 那我该怎么做呢? There are several other class files having main methods with IOException which have to be invoked in AllMethods.java file. 还有其他一些类文件,这些类文件具有带有IOException主要方法,必须在AllMethods.java文件中进行调用。

Update - 更新-

I have main methods having IOException as well as main methods not having IOEXception that has to be invoked in AllMethods.java . 我有具有IOException主要方法以及没有IOEXception主要方法,必须在AllMethods.java调用AllMethods.java

You can do that. 你可以做到的。 Main method is also just like any other static method 主方法也和其他静态方法一样

public static void main(String[] args) throws IOException {
 ....// do all the stuff


Splitter.main(args); // or null if no args you need
}

First of all, what you should probably do is refactor your code so each main method calls some other method, and then AllMethods makes calls to those new methods. 首先,您可能应该做的就是重构代码,以便每个主要方法调用其他方法,然后AllMethods调用这些新方法。 I can imagine there might be some cases where it's useful if you're just trying to, for example, write some test code, but usually you wouldn't want to call main methods directly. 我可以想象,在某些情况下,如果您只是想尝试编写一些测试代码,则很有用,但是通常您不想直接调用主方法。 It's just harder to read. 很难阅读。

If you want to try it though, it's pretty easy, you just call the main method like any other static method. 如果您想尝试一下,那很简单,您只需像其他静态方法一样调用main方法即可。 I once in college wrote a web server where, to handle authentication, I recursed on the main method. 我曾经在大学里写过一个Web服务器,在其中处理身份验证,我使用了main方法。 I think I got a C because it was unreadable code, but I had fun writing it. 我认为我得到了C,因为它是不可读的代码,但是我写它很有趣。

class AllMethods {
    public void callsMain() {
        String[] args = new String[0];
        Splitter.main(args);
    }
}

In the Main.java, the main method should add throws Exception as shown below: 在Main.java中,main方法应添加throws Exception,如下所示:

package com.company;

import java.io.FileNotFoundException;

public class Main extends makeFile {

    public static void main(String[] args) throws FileNotFoundException {

        makeFile callMakeFile = new makeFile();

        makeFile.main(args);
        // cannot figure out how to call the main method from the makeFile class here...
    }
}

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

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