Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 23-11-2009
Member
 
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.
Reply With Quote
  #2  
Old 23-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
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 ...
}
Reply With Quote
  #3  
Old 23-11-2009
Member
 
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.
Reply With Quote
  #4  
Old 23-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
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 ...
}
Reply With Quote
  #5  
Old 24-11-2009
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Restart main in java"
Thread Thread Starter Forum Replies Last Post
Java main class not found Rily Software Development 6 13-08-2010 10:21 AM
Explain main method in java Angelica Maria Software Development 5 10-03-2010 01:18 PM
Why main method is static in java? MKAIF Software Development 5 06-03-2010 04:20 PM
How to Make Frames or Main Windows in Java? Soumen Software Development 5 11-02-2010 01:14 AM
Procedure to run Java program without main method Sheena_thakkar Software Development 3 26-11-2009 02:57 PM


All times are GMT +5.5. The time now is 08:31 AM.