How to Restart a Java Program Here

How to restart a java program here

Once the main method exits the application exits. Put your code in another method and call it inside a while loop.

public static void main(String[] args) {
while(true){
process();
}
}

static void process(){
System.out.println("Please enter your official name.");

Scanner typing = new Scanner(System.in);
String name = typing.nextLine();

while (!name.matches("[a-zA-Z]")) {
System.out.println("Please enter your official name.");
name = typing.nextLine();
}
if (name.length() >= 4)
System.out.println("Your name is: " + name);

else
System.out.println("So your name is less than four characters.");
}

This will run the program again and again infinitely. If you want to exit, press Ctrl + C in cmd or you can use,

System.exit(0);

somewhere in your code.

Restart java program

First, put (almost) everthing in the first section of the above code (until the do-while statement) in a introPhase() method or something.

Secondly, put your do-while in a second gamePhase() method or something.

Finally, surround both with a conditional loop statement of some sort.

You should end up with something like this:

boolean finishFlag = false;
while(!finishFlag) {
introPhase();
gamePhase();
finishFlag = getExitInfoFromUser();
}

Good luck!

How can I restart a Java application?

Of course it is possible to restart a Java application.

The following method shows a way to restart a Java application:

public void restartApplication()
{
final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
final File currentJar = new File(MyClassInTheJar.class.getProtectionDomain().getCodeSource().getLocation().toURI());

/* is it a jar file? */
if(!currentJar.getName().endsWith(".jar"))
return;

/* Build command: java -jar application.jar */
final ArrayList<String> command = new ArrayList<String>();
command.add(javaBin);
command.add("-jar");
command.add(currentJar.getPath());

final ProcessBuilder builder = new ProcessBuilder(command);
builder.start();
System.exit(0);
}

Basically it does the following:

  1. Find the java executable (I used the java binary here, but that depends on your requirements)
  2. Find the application (a jar in my case, using the MyClassInTheJar class to find the jar location itself)
  3. Build a command to restart the jar (using the java binary in this case)
  4. Execute it! (and thus terminating the current application and starting it again)

How to restart a program from a method

If you just want to make it work you can do something like this :

private static void game()//game method
{
boolean exit = false;
while(!exit){
//...int play = JOptionPane.showOptionDialog(null,"Play Again?", "Do you want to play again?", JOptionPane.PLAIN_MESSAGE,JOptionPane.DEFAULT_OPTION, null, again, again[1]);
//end of game


if (play == 0) {
exit = true;
}

}
System.exit(0);//exit

But a better more professionnal approach would be to refactor your code, so you extract game logic and separate it from User dialog interaction.

How to restart a Java program to get environment variables active

Now I used Apache exec and it seem to work.

Executor exec = new DefaultExecutor();
CommandLine cl = CommandLine.parse(command);
Map<String, String> env = EnvironmentUtils.getProcEnvironment();
env.put("XYZ", "XYZ"); //own vars
exec.execute(cl, env);


Related Topics



Leave a reply



Submit