• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

run more mains in Java

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi gus,

this in my first post, and I am a total beginner.
I have done a project for a uni exam in java, but it has to be fixed in some points, so here I am to solve a little problem.
I have more objects with main: there is the Boss {main()}, to be run once, and there are the Servants{main()}
to be run more times as the number of servants required.
The point is that I don't know how to make a smart start-up. I mean, I can do either a batch script (but I don't know how to write it yet)
either run the Boss, and then run the Servants one after the other, asking the Boss every time the own id. Or maybe there is another way less cumbersome, in this case please give me a clue!

Thanks a lot for your attention,

maribo
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not make a Master class that calls the main() methods on the other parts? that method is just a method after all.

 
mari bo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you David for your reply,

I 've just tried, but it works fine for the Boss.main() (I can see the output on the console), but I can't see the servants works, because no one of the output(like, "i am alive!) comes out.
And by the way, I need one console for each.

So, what can I do ?

Thanks a lot

maribo
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you need to provide more requirements for us to be able to help you.
If you want a separate console for each and you're running Windows, you can run a batch script with 'start' at the beginning of the line
eg
start java Boss
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think you have misunderstood the purpose of the main method It is there to start an application. Ideally it should contain one line. So your application main method would create an object, and call a method on that, which starts everything else running.
At your stage you would run everything else on the same console, so you would not usually require several consoles.

Please explain more.
 
mari bo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch



Thank you

Campbell Ritchie wrote:
I think you have misunderstood the purpose of the main method It is there to start an application. Ideally it should contain one line. So your application main method would create an object, and call a method on that, which starts everything else running.At your stage you would run everything else on the same console, so you would not usually require several consoles.
Please explain more.



Sure, I'll explain more. So, I have to do Boss{} and Servants{} both with the main because what I am doing is a project for uni, and my professor absolutely wants those objects istantiated separately.
The situation Boss{main()} and Servants{main()} is required by default.
Boss{main()} has to be run once
Servants{main()} has to be run a fixed number of times.
In the following there is what is it done by both main in short:

BOSS



ps. the object Peer is an object that is the "base model" of the Boss and Servants, things done in common.

SERVANT



Originally I did the project with only one main, but the professor told me to change things in order to have the Boss and Servant with their own main, because he wants everything to be differently istantiated.
So what I did is the following: I have separated the main in two as you can see above. Then, I run the Boss, and the Servants the number of times required with the green button of eclipse, paying attention to pin the console and select a new console each time. In the Servant object I added also a line that allowed me to type the ]index. It worked and everyone has it own console. But this way it is really cumbersome.

Now, what I am trying to do is the following:
(?)_make a nice and simple start-up, where I can set up the number of servants, give the id and the listening port to the Servants and make everybody knows each other references (host, port) because everyone comunicates with everybody by UDP socket;
WELLKNOWN =7000 is the port if the Boss; so every port of the servants is like 7001, 7002, 7003...blablabla, and the respective index is 0(Boss),1, 2, 3...blablabla.

This for loop is in the object Peer.

(?)_If the batch script can be a solution, how can I write it?

(?)_ Any idea is very very wellcome

Thank you and sorry if my post is wordy.

maribo





 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand your question correctly, maybe you should look into Threads in Java.

That way, you can both the Servant and the Boss classes simultaneously.

Oops, you've already done that!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic