How do I run the main method in a different file in Java?

Yajus Gakhar :
package jav;
class PackageDemo
{
    public void display()
    {
        System.out.println("PackageDemo executed");
    }
}
public class PackageDemoDriver
{
    public static void main(String[] args) {
        PackageDemo boy = new PackageDemo();
        boy.display();
    }
}

This is the code for a package. I will be importing this package into a different file. The code for that is:

package exercise;
import jav.PackageDemoDriver;
class Exe
{
    public static void main(String[] args) {



    }
}

What should I fill in the main method to run display(), if it is possible to do so?

Sam :

Based off your question I assume you want the main function in Exe to essentially run the main function in PackageDemoDriver:

public class Exe {
    public static void main(String args[]) {
        PackageDemoDriver.main(null);
    }
}

I think that'll provide the functionality you're after, if your PackageDemo and PackageDemoDriver are in different classes

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=396456&siteId=1
Recommended