Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: how to restart my program

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default how to restart my program

    Hi
    i want to restart my program(a network simulator) after i check if the network has died in my program , so tha i dont have to hit the run button on eclipse. How can i do this


  2. #2
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: how to restart my program

    I have this code which one of my classmate is using ,but i dont know how should i use it ,it has .jar file in the syntax




    try{

    Runtime.getRuntime().exec("java -jar /Users/xyz/Desktop/Test/file.jar");
    System.exit(0);
    }
    catch (Exception e){}

    }

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: how to restart my program

    restarting the program

    Duplicate post
    Improving the world one idiot at a time!

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: how to restart my program

    First of all you people have two different addresses,and i dint know both of them point to one forum ,its my fault!
    secondly atleast reply to a post,someone who needs your assistance is not here to hear you lame comments that its a duplicate/cross post..
    i think the main goal of a forum should be to help solving problems ,instead of caring about these lil issues

  5. #5
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: how to restart my program

    jack_nutt,

    Can i please bring your attention to a particular section of the forum guidance post,
    3b. Please post links to the sites you have cross-posted your question(s) on. This allows our members to join in on the discussion without having to ask the same questions over again. See The Problems With Cross Posting for greater details on this subject.
    Please note that we have rules against cross-posting, as do many other forums. Not only are you breaking ours, but you are breaking others.

    java-forums.org is not a memebr of our family, just many of our memebrs choose to be active members on multiple forums, so that they can provide solutions to more people, because its what they do.

    Please take the time to read the post aboutthe problems with cross posting as linked to in the above qoutation. When you do this, you will realise why it is in your interest to abide by these rules.

    Junky, is a very useful member of the community and provides help to many people.

    The main goal of the forum is to provide solutions to people as well as a friendly environment, but people who end up wasting time posting replies to threads already solved on other forums cannot help many people.


    Regards,
    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  6. The Following 3 Users Say Thank You to Freaky Chris For This Useful Post:

    bgroenks96 (November 3rd, 2011), copeg (November 3rd, 2011), Tjstretch (November 10th, 2011)

  7. #6
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: how to restart my program

    try
    setVisible(false);
     
    // if statement to see network status..
    // if the net status is true
     
    ...
    new netProgram();
    setVisible(true);
    ...
    I'm asuming in this example that your class name is netProgram.. which would give your first method a name of

    public netProgram(){
     
    }

    Also I'd use a timer.. So make the program generate this code every say few mins.. to stop the processing usage..

    But this is just a quick way of doing it

  8. #7
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: how to restart my program

    Quote Originally Posted by jack_nutt View Post
    First of all you people have two different addresses,and i dint know both of them point to one forum ,its my fault!
    secondly atleast reply to a post,someone who needs your assistance is not here to hear you lame comments that its a duplicate/cross post..
    i think the main goal of a forum should be to help solving problems ,instead of caring about these lil issues
    Perhaps this is nitpicky, but it also assists those trying to help you, if you take the time to utilize the SHIFT key for capitalization and SPACE key for spaces, as well as using full
    English words rather than slang abbreviations. Not only does this promote a sense of professionalism with your issue, but it simply allows people to understand what you are
    saying in a much simpler and faster fashion, rather than spending time that could be used on solving this issue on deciphering a mess of lazy typing.

    Bad English is one thing, as those who aren't fluent simply can't help it, but lazy typing is just sort of rude when asking people to help solve a problem.

  9. The Following User Says Thank You to bgroenks96 For This Useful Post:

    jack_nutt (November 10th, 2011)

  10. #8
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: how to restart my program

    Now to reply to the actual post:

    There are two perfectly good methods to doing this. First, you can try what another person has already mentioned, which is restarting via a console command:
    Runtime.getRuntime().exec(System.getProperty("user.dir")+[program file name here]);    //Starts a new JVM running the same program.
    System.exit(0);   //Terminates the currently running JVM

    Alternatively, and probably more cleanly for your situation, you can keep a master while loop in the main thread, so that as long you nor the JVM have issued a termination command, the program will
    always restart upon method completion unless told otherwise.

    private static boolean running = true;
     
    public static void main(String[] args) {
        while(running) {
            //Do whatever
        }
    }
     
    //False if you want execution to halt upon the completion of your code.  True if you want it to remain running and "restart" once your code has finished.
    public static void setStatus(boolean stat) {
        running = stat;
    }
    Last edited by bgroenks96; November 3rd, 2011 at 09:42 PM. Reason: Fixed code tags and added information.

  11. The Following User Says Thank You to bgroenks96 For This Useful Post:

    jack_nutt (November 10th, 2011)

  12. #9
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: how to restart my program

    Quote Originally Posted by bgroenks96 View Post
    Now to reply to the actual post:

    There are two perfectly good methods to doing this. First, you can try what another person has already mentioned, which is restarting via a console command:
    Runtime.getRuntime().exec(System.getProperty("user.dir")+[program file name here]);    //Starts a new JVM running the same program.
    System.exit(0);   //Terminates the currently running JVM

    What should be the "+[program file name here]" , should it be my java file name or the project folder name ??

    can you give an example as how to put the code for a specific file??

    thanks

  13. #10
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: how to restart my program

    import java.io.*;
    public class a {
     
     
    		public static void main(String[] args) {
     
     
    			for(int i=0;i<50;i++)
    		{
     
     
    			  try {
     
    				  BufferedWriter meanValue = new BufferedWriter(new FileWriter(new File("filename.txt"), true));
                      meanValue.write(""+i);
                      meanValue.newLine();
                      meanValue.close();
     
     
     
              } catch (Exception e) {
              }
     
     
    			System.out.println(i);
    			if(i>10){
     
    				try{
     
                        Runtime.getRuntime().exec("C:\\Users\\abc\\workspace\\crejrfiles\\lib\\jarf.jar");
                        System.exit(0);
                        }
                        catch (Exception e){}
     
                        }
     
    			}
    			}	}

    I have created a .jar file at location C:\Users\abc\workspace\crejrfiles\lib\jarf.jar ,using eclipse

    But the program runs only once ,how can i make it to keep on restarting so that i keep on logging values in my text file.. what paramaters should i give to
    Runtime.getRuntime().exec (...............)

  14. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how to restart my program

    You need to put in the syntax you would need to call the program as if you were doing it from the command line.

    To run a java program from the command line:

    java -jar jarf.jar
    On windows systems, you will either have to manually type in the path to the java executable, or add it to your system path variable.

    Ex.:

    Say I have java.exe located at C:/Program Files/java/jre7/bin

    The the command would be:
    "C:/Program Files/java/jre7/bin/java -jar C:/some_jar.jar"
    You'll need to format this command to fit inside of a Java string, then execute it from Runtime.getRuntime().exec()

  15. #12
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: how to restart my program

    I tried this in Windows


      Runtime.getRuntime().exec("java -jar C:\\Users\\abc\\workspace\\crejrfiles\\lib\\jarf.jar");

    But its not working ...
    my jarfile is at location "C:\\Users\\abc\\workspace\\crejrfiles\\lib\\jarf. jar"

    what should i do?

  16. #13
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how to restart my program

    Define "not working".

    Did you add the java executable path to your Windows system path?

  17. #14
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: how to restart my program

    By your description i tried the command

     
    Runtime.getRuntime().exec("C:\\Program Files\\Java\\jre6\\bin\\java -jar C:\\Users\\abc\\workspace\\crejrfiles\\lib\\jarf.jar");


    As my jave files are in folder C:\Program Files\Java\jre6\bin


    Not working means
    i want my program(process) to keep on restarting and running in backhand ...I am actually logging a file in my program and i want the simulation to run automatically everytime.

  18. #15
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: how to restart my program

    Quote Originally Posted by jack_nutt View Post
    By your description i tried the command

     
    Runtime.getRuntime().exec("C:\\Program Files\\Java\\jre6\\bin\\java -jar C:\\Users\\abc\\workspace\\crejrfiles\\lib\\jarf.jar");


    As my jave files are in folder C:\Program Files\Java\jre6\bin


    Not working means
    i want my program(process) to keep on restarting and running in backhand ...I am actually logging a file in my program and i want the simulation to run automatically everytime.
    Is the JAR launching properly? If the JAR you are launching is the same one executing this command, it should continuously keep spawning a new process with each execution. Just be careful to set that up correctly. You don't want to inadvertently fork bomb your computer...

  19. #16
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: how to restart my program

    No i think my jar file is not working, when ever i click on it ,it gives an error
    "invalid or corrupt jar file C:\Users\abc\workspace\project\lib\jarf.jar "

    how can i do it in Eclipse? what options do i need to consider while exporting this jar file
    how can i get rid of this error??

Similar Threads

  1. Sliding puzzle Restart button(same exact game)
    By carterb32 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 21st, 2011, 04:29 AM
  2. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM