Results 1 to 14 of 14
  1. #1
    veronica is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Question 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!

    veronica
    Last edited by veronica; 02-09-2011 at 09:15 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  3. #3
    veronica is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Question 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.

    Quote Originally Posted by pbrockway2 View Post
    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.
    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

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  5. #5
    veronica is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Question

    Quote Originally Posted by masijade View Post
    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.
    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

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  7. #7
    veronica is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Question ok, going on...

    Quote Originally Posted by masijade View Post
    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.
    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

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Make a batch script.

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  10. #10
    veronica is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Smile indeed

    Quote Originally Posted by masijade View Post
    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.
    "All I can do is to search" - yes, that was my interpretation :D and moreover I was so impressed that I thought: "how helpful!"

    Anyway, I use this occasion to say that if you know please tell me, it would be really helpful :)

  11. #11
    veronica is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Make a batch script.
    I'll give a try, thank you very much!

  12. #12
    veronica is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Question ok!

    Quote Originally Posted by masijade View Post
    Make a batch script.
    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 :

    Java Code:
    public class Run{
              public static void main(String[] args){
                  Runtime.getRunTime().exec("myfile.bat");
              }
    }
    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.
    Can anyone help me please? I would very appreciate it.

    Thanks a lot for your attention!

    veronica

  13. #13
    veronica is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Unhappy help please...

    Hello guys,

    I tried in this way to run more main() simultaneously:

    Java Code:
    public class StartUp{
    
    	public static void main(String[] args){
    		
    		Boss.main(null);
                    Servant.main(null);
                    Servant.main(null);
                    Servant.main(null);
    	}
    }
    but it doesn't work. All I can see on the console is the Boss' output.
    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.

  14. #14
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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

  1. Replies: 3
    Last Post: 10-12-2010, 05:21 PM
  2. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  3. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 08:50 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •