Results 1 to 14 of 14
Thread: how to run "2 mains"
- 02-09-2011, 07:45 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
how to run "2 mains"
Hi everybody,
I have a question, I am sorry in advance if it is too obvious, but this is the right place to ask little question as mine, isn't it? :o Thank you in advance for your attentiona nd your answers.
So, my problem is this : I have a class called "Boss", and another class of the "Servants". Both classes have their own main, because the purpose is that everything has to be differently istantiated. This classes can comunicate through socket UDP.
Now my question is:
I can choose a specific number of servants, it mean that for instance if I have 3 servants I have to run the class servants 3 times.
So, how am I suppose to do that in a smart way?
Thanks guys for your attention!
veronicaLast edited by veronica; 02-09-2011 at 08:15 AM.
- 02-09-2011, 08:23 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
A program executes a single main() method. Basically you pass the name of a class to the Java runtime and it gets the main() method of that class and invokes it.
Note that it makes sense to talk of invoking (causing to happen) the main() method, or running a program. But it sounds a little odd to talk about running a class - a class is not a thing: its a type of thing.
The main() method is not at all special. Any class can have a method main(). The compiler regards m-a-i-n as it would any other valid sequence of letters used to name a method.
In eclipse you can cause the runtime to invoke a particular class's main() method by right clicking on the source file and selecting "run as... Java application". Or click the run button several times. It is important to realise that, in line with the rule "one program one main()", the effect is that you are running multiple programs. It's like the Boss was in one galaxy and the Servant in quite another. There is no commincation between them. (OK you could organise them to leave messages on the file system for one another to read, but I don't think that's what you intend.)
One program (one main()) with three servants is quite a different thing from three programs (three main()s) with one servant each.
Here is a simple example of two programs with three servants.
Java Code:// file: ServantTest.java public class ServantTest { public static void main(String[] args) { Servant test = new Servant("Tom"); System.out.println(test.getName()); test = new Servant("Dick"); System.out.println(test.getName()); test = new Servant("Harry"); System.out.println(test.getName()); } }Java Code:// file: Servant.java public class Servant { private String name; public Servant(String name) { this.name = name; } public String getName() { return name; } public static void main(String[] args) { Servant tom = new Servant("Tom"); System.out.println("Hello, I'm " + tom.getName()); Servant foo = new Servant("Dick"); System.out.println("I'm " + foo.getName()); Servant bar = new Servant("Harry"); System.out.println("And I'm " + bar.getName()); } }
As you can see the main() method can go in the Servant or in another class - it makes no difference. But the two main() methods define different programs. The creation of multiple servants is done by using "new" multiple times, not by calling main() multiple times.
- 02-09-2011, 09:30 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
you are right, but...
Hi pbrockway2,
thank you for your quick answer! I have to apologize if my java-vocabulary is not correct as you noticed, but I am a total beginner. So thank you for your tips, in this way I can learn more of java and in a correct way.
Yes, I know that a program executes a single main method. And that was what I've done. But since the program is a project for a uni exam, my professor told me that he doesn't want only one main(as I've done! :o), but a single main for each of the guys (one for the Boss, and one for the Servants) - because the project is all about distributed system and the project should simulate one of them.
So I have to use multiple mains. In this way I need multiple consoles too. What I am doing is running first the Boss, and then running one Servants. If I want to run multiple Servants what I do is running the same program more times, each with a different id. But what happens is that everytime I run the Servants_main more than one time, the console related to the Servant clean itself everytime, starting again.
So my question is:
how do I run multiple Servants with its own console?
Thank you very much for your attention!
veronica
- 02-09-2011, 09:40 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Start multiple jobs in Eclipse. Each of those will get their own console. On the top right of the console window in Eclipse is a menu to select which console you currently want to see when multiple processes are running, but you can start multiple processes.
- 02-09-2011, 10:06 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
Thank you Masjijade,
yes, I tried and I did it with clicking buttons here and there, pinning one console, then clicking on "new console view", and at the end it worked!
Thanks a lot.
Now I was asking myself if there is a way to do all this stuff in a simple programmable way and not doing it with the mouse and clicking all this stuff.
Since I have not the answer obviuosly ;), gently I ask you guys if it exists this way.
Thank you a lot!
veronica
- 02-09-2011, 10:16 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Not AFAIK, unless you want to write an Eclipse Plug-In. All I can is to search the project and window properties and see if there is any option like "always pin console" or something.
- 02-09-2011, 12:20 PM #7
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
ok, going on...
oh yes, why not thank you if you give a try.
Anyway, dropping the idea of the easy programmable way to have each one Servant one console, I was wondering if there is any good and simple idea to make a START-UP. The situation in a pseudo-code:
Boss{ main() } - [it runs once]
Servants{ main() } - [it runs more times]
What is the best way according to you to start these objects, knowing the number of total servants?
Thank you very much!
veronica
- 02-09-2011, 12:22 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Make a batch script.
- 02-09-2011, 12:24 PM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
BTW the phrase "All I can is to search" should have been "All I can say, is to search", I seem to have missed a word. You Seem to have read it as "All I can do, is to search". IOW, this is for something for you to do.
- 02-09-2011, 02:45 PM #10
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
- 02-09-2011, 02:46 PM #11
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
- 02-09-2011, 07:58 PM #12
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
ok!
I have done a long search trough internet and I've found out that if I want to open a .bat script with eclipse I have to do something that looks like :
Fair enough, that looks nice. I have a little problem now: honestly I have no clue of what to write in the .bat script to run the main(s) of the Boss and Servants object. I have been looking for a solution on the internet all the afternoon but I couldn't understand what really I have to write.Java Code:public class Run{ public static void main(String[] args){ Runtime.getRunTime().exec("myfile.bat"); } }
Can anyone help me please? I would very appreciate it.
Thanks a lot for your attention!
veronica
- 02-10-2011, 06:42 PM #13
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
help please...
Hello guys,
I tried in this way to run more main() simultaneously:
but it doesn't work. All I can see on the console is the Boss' output.Java Code:public class StartUp{ public static void main(String[] args){ Boss.main(null); Servant.main(null); Servant.main(null); Servant.main(null); } }
No sign of life from the Servants: if Boos send a message to them they won't reply at all.
Would someone have a good idea?:confused:
Thanks,
v.
- 02-10-2011, 07:13 PM #14
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
The "batch script" idea was simply to write a batch script that java four times. And this last attempt of yours does not start four different programs it calls the main methods one after the other waiting for the previous to finish before the next executes. I thought the idea was to start four different programs, so start four different programs. Either manually or with a batch script.
Similar Threads
-
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
problem with argument list and precedence "(" and ")"
By helpisontheway in forum Advanced JavaReplies: 6Last Post: 12-24-2009, 07:50 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks