Results 1 to 5 of 5

Thread: Restart main in java

  1. #1
    Join Date
    Nov 2009
    Posts
    446

    Restart main in java

    Hi
    I am an intermediate in java, so I know some programs in java and have a hand on session experience in java. My problem is that how to restart main the class in java. I have created and IRC socket and want to restart the program after closing the stream and socket. So, can any body help me out of this problem. Thanks in advance.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Restart main in java

    Hello
    If you have a main method which something looks like this.
    Code:
    public static void main(String[] args) {
    
       // a whole lot of code here that doesn't belong here to start with
    }
    Now if you want to restart the main method, then you should do something like this
    Code:
    public static void main(String[] args) {
       while(true)
          doWork(args);
    }
    private static void doWork(String[] args) {
    
       // the entire bunch of code here ...
    }

  3. #3
    Join Date
    Nov 2009
    Posts
    446

    Re: Restart main in java

    Hi
    I am little confused what you posted in your above post. What command should I put to restart it. Does I have to do some more code to restart the main method. Can you help me more. That is in detail please. The problem is that I have not understood the basic operation which is behind the screens. So, can you explain me what exactly is happening in the background. So that I can clear the concept first.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Restart main in java

    Hi
    First of all the code that I have posted, just go through it. the first is the main method which is in its general form and the second one is the main method which will be restarted. I am posting the code again, this is the code which will restart your main method
    Code:
    public static void main(String[] args) {
       while(true)
          doWork(args);
    }
    private static void doWork(String[] args) {
    
       // the entire bunch of code here ...
    }

  5. #5
    Join Date
    Feb 2009
    Posts
    96

    Re: Restart main in java

    basically he is saying that your main would be like something like this:

    public void main(String[] args)
    {
    run();
    }

    run()
    {
    bunch of code
    }


    take a look at this example that i actually used:

    Code:
    public static void main (String args[])
    	 {
    		(new FitClub()).run();
    	}
    
    
    
    	public void run()
    	 {
    		
    		String msg = " *******Members Menu******* \n" +
    		"Select one of the following options:\n"+
    		"1. Add Club Members\n" +
    		"2. Delete a Member\n" +
    		"3. Display the Club Members\n" +
    		"4. Exit\n" +
    		"\nYour Choice:";
    		
    		while(true)
    		 {
    			String str = JOptionPane.showInputDialog(null, msg, "", 1);
    			int sel = Integer.parseInt(str);
    			
    			switch(sel)
    			 {
    				case 1:
    					addMembers();
    					break;
    				case 2:
    					deleteMember();
    					break;
    				case 3:
    					head();
    					displayMembers();
    					break;
    				case 4:
    					return; 
    				default: 
    					System.out.println("Wrong choice, try again.");
    			}		
    		}//end while
    	}//end run

Similar Threads

  1. Java main class not found
    By Rily in forum Software Development
    Replies: 6
    Last Post: 13-08-2010, 10:21 AM
  2. Explain main method in java
    By Angelica Maria in forum Software Development
    Replies: 5
    Last Post: 10-03-2010, 01:18 PM
  3. Why main method is static in java?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 04:20 PM
  4. How to Make Frames or Main Windows in Java?
    By Soumen in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 01:14 AM
  5. Procedure to run Java program without main method
    By Sheena_thakkar in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 02:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,095,278.43042 seconds with 16 queries