Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Question!!!

Options
  • 23-01-2004 5:21pm
    #1
    Closed Accounts Posts: 1,637 ✭✭✭


    OK thanks for the help on the last thread, but not really what im after i'll explain..

    Is there a command to restart a program or go gack to the first line of code,

    e.g. Unload Me in VB, can it be done in java....

    Thanks loads in advance....

    Thanks joePC


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    The best way is probably to enclose the entire contents of the main() method in a while loop. so for example:
    public class MyClass {
        public static void main(String[]args) {
             while(true) {
    
                      //..............insert your code here.........
    
              }
         }
    }
    

    To loop back to the start of the while loop (ie skip the rest of the while loop) you use the 'continue' keyword.

    There must be a better way to do what you're trying to do though......


  • Closed Accounts Posts: 1,637 ✭✭✭joePC


    Thanks Seamus, but that would cause alot of problems, there must be another way

    Thanks joePC...


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Indeed there must be Daniel Jackson.

    What exactly is it you want to do? And post up some source code of what you've tried already.


  • Closed Accounts Posts: 137 ✭✭124124


    I haven't touched VB since 1997, so pardon me ignorance, does 'UNLOAD ME' mean 'close this program'?

    Now, from other posts, I gather that JoePC is trying writing java applications (launched from command prompt, with java command.). If you are looking for a way to exit an application, its

    System.exit(0);

    Hope this helps,

    124.


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    unload me means unload this form, me having the same meaning as 'this' in java.


  • Advertisement
  • Closed Accounts Posts: 1,637 ✭✭✭joePC


    Thanks for the help, got it all sorted, I put the contents of my main into a method and can access the method anytime using mathod();

    Thanks joePC


  • Registered Users Posts: 53 ✭✭martinoc


    use the following:



    Runtime.exec("java mypackage.MyMainClass");

    and then exit the program.


    The above executes a new process (at os level).
    If the above does not work then the string probably should be fully qualified so should read:

    Runtime.exec("c:\j2re4.1.2\bin\java mypackage.MyMainClass");

    Replace the path with the location of your own jre.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Originally posted by martinoc
    use the following:



    Runtime.exec("java mypackage.MyMainClass");

    and then exit the program.

    Why?

    Why not just javaw instead of java? Or for that matter why Runtime.exe() ?


  • Registered Users Posts: 53 ✭✭martinoc


    You can use javaw if you want. Javaw is just a windows extention to the jre. It's irrelevent.

    The point of using Runtime.exec(String command) is that a clean process is started.
    The program in question can in effect spawn itself before exiting. Exactly what the original poster wanted.

    I know its a horrible hack and is os dependant, but it can solve the posters problem.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    or he could build his app as a class then have main call that class and have it recreate the class to go back to the start.


  • Advertisement
  • Registered Users Posts: 53 ✭✭martinoc


    That was already suggested (kinda) above and he rejected it and asked if there was any other way. I've just suggested an alternative.


  • Closed Accounts Posts: 137 ✭✭124124


    Originally posted by sjones
    unload me means unload this form, me having the same meaning as 'this' in java.

    Well, not to be rude or anything, but from the questions that JoePc is posting (and more so from his solutions), I honestly think he should get his basics right - things like what’s a class, and what and why a method, process etc. With out correct basics, java is an awfully difficult language to master.

    Good luck mate!

    124.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Well seamus is correct but better to do this way.

    public class MyClass {
         private boolean mWorkComplete = true;
    
        // This is the method that runs by default. 
        public static void main(String[]args) {
              
              MyClass m = new MyClass();
              
              do {
                      m.doMyStuff();
              } while(m.isStillWorking());
         }
    
          public boolean isStillWorking() {
                return mWorkComplete;
          }
    
          public void doMyStuff() { 
                mWorkComplete = false;
               // Put your code here. 
               // When the work is done set mWorkComplete to true.
          }
    }
    

    This means at a later point you can seperate the main () to another class or create MyClass in another class/application and run it there.

    Although a thread would be better.


Advertisement