Reputation: 313
I have two packages inside my project, a
and b
.
a
is my "main class" that runs when the program runs but I need to make b
run from a
(if that makes sense).
I'm sure its something along the lines of PackageB.BMain
but I'm not sure.
Edit:
Okay so I've learned a few new things, to start my main project is RevIRC, inside that I have two packages, MainChat and RevIRC, now when I run the program RevIRC is ran, I need to make Mainchat run when RevIRC is ran.
Like I said before I'm sure its something along the lines of RevIRC.MainChat.ChatMain()
but I can't seem to figure it out.
Upvotes: 0
Views: 4395
Reputation: 385
if i am not wrong you want to run main method of class B from class A. That you can call using B.main(arg[]); eg :
package a;
public class A
{
public static void main(String[] args)
{
System.out.println("This is main method of class A");
B.main(null);
/*pass any args if you want or simply set null arg*/
}
}
package b;
public class B
{
public static void main(String[] args)
{
System.out.println("This is main method of class B");
}
}
i hope this simple example will clear your doubt.
you can refer to link which contains Java tutorial for beginners.
Upvotes: 0
Reputation: 10995
If you have two main methods it will either run from A or B. The JVM will choose the first main method it sees IIRC.
Have a standalone class that will have main. And create your classes there.. ?
import a.Class1;
import b.Class2;
public class MainController
{
public static void main(String args[])
{
Class1 class1 = new Class1() ;
Class2 class2 = new Class2() ;
//Both class no start at the "same" time.
}
}
Upvotes: 0
Reputation: 52185
You have 2 options:
PackageB.BMain b = new PackageB.BMain();
BMain
in a static way like so: PackageB.BMain.someMethod();`Note that you can use either of these exclusively or mix them up together, however, it all depends on how you have written your BMain
class.
So for instance:
package PackageB
public class BMain
{
public BMain()
{ }
public void foo()
{
System.out.println("This is not a static method. It requires a new instance of BMain to be created for it to be called");
}
public static void bar()
{
System.out.println("This is a static method. It can be accessed directly without the need of creating an instance of BMain");
}
}
Then in your main class (the class which has the main
method):
package PackageA
public class AMain
{
public static void main(String[] args)
{
PackageB.BMain.bar();
PackageB.BMain bInstance = new PackageB.BMain();
bInstance.foo();
}
}
Upvotes: 1