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:
- Find the java executable (I used the java binary here, but that depends on your requirements)
- Find the application (a jar in my case, using the
MyClassInTheJar
class to find the jar location itself) - Build a command to restart the jar (using the java binary in this case)
- 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
How to Find Max Duplicate Number Count in Given Array Using for Loop
Classcastexception Because of Classloaders
How to Append a Query Parameter to an Existing Url
Sharing a Variable Between Multiple Different Threads
Use Only One Bufferedreader Object to Read Multiple Files in Java
Delete a Line from a File Using Java
Spring Kafka - How to Reset Offset to Latest With a Group Id
How to Get Response as Json With Responseentity in Java
Password Encrypt and Decrypt Using Spring-Security
Turning Sonar Off for Certain Code
How to Exclude a Dependency from Parent'S Project in Maven
Using an Empty Keystore Password Used to Be Possible
How to Properly Re-Run Spring Boot Application from Eclipse
Handling the Null Value from a Resultset
Splitting a String Containing Special Characters in to Individual Characters