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 8 of 8

Thread: ShutDown from JButton

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question ShutDown from JButton

    When i use this from NetBeans it's work but when i use outside of NetBenas don't Code of the ShutDown Class is:

    import java.io.IOException;
    public class ShutDown {
    public static void shutdown() throws RuntimeException, IOException {
    String shutdownCommand;
    String operatingSystem = System.getProperty("os.name");
     
    if (operatingSystem.startsWith("Lin") || operatingSystem.startsWith("Mac")) {
        shutdownCommand = "shutdown -h now";
    }
    else if (operatingSystem.startsWith("Win")) {
        shutdownCommand = "shutdown.exe -s -t 0";
    }
    else {
        throw new RuntimeException("Unsupported operating system.");
    }
     
    Runtime.getRuntime().exec(shutdownCommand);
    System.exit(0);
    }
    }


    Code of the ShutDown Button is:

    private void ShutDownBtnActionPerformed(java.awt.event.ActionEvent evt)
        {                                            
        try {
            ShutDown.shutdown();
        } catch (RuntimeException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    Last edited by mija; July 6th, 2012 at 06:34 PM.


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

    Default Re: ShutDown from JButton

    When you say doesn't work, what do you mean? Is there an exception? Please post any diagnostics you can (i.e. exception messages, stack traces, etc.)

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ShutDown from JButton

    Quote Originally Posted by helloworld922 View Post
    When you say doesn't work, what do you mean? Is there an exception? Please post any diagnostics you can (i.e. exception messages, stack traces, etc.)
    this is the exception
    ShutDown exception.JPG

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

    Default Re: ShutDown from JButton

    It looks like a class-path error. Make sure all your base class paths get included (if there's more than one). What's the folder structure you have, and what command are you using to run from the command line?

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ShutDown from JButton

    Quote Originally Posted by helloworld922 View Post
    It looks like a class-path error. Make sure all your base class paths get included (if there's more than one). What's the folder structure you have, and what command are you using to run from the command line?
    I have two packages one is "rd" where is my "loginform.java" and the other is "classes" where is "sdown.java"
    I run it from the cmd with this command java -jar "C:\Documents and Settings\Daniel Miovski\My Documents\NetBeansProjects\R D\dist\R_D.jar"
    Last edited by mija; July 7th, 2012 at 04:01 AM.

  6. #6
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: ShutDown from JButton

    Quote Originally Posted by mija View Post
    I have two packages one is "rd" where is my "loginform.java" and the other is "classes" where is "sdown.java"
    I run it from the cmd with this command java -jar "C:\Documents and Settings\Daniel Miovski\My Documents\NetBeansProjects\R D\dist\R_D.jar"
    Well, I never use IDE to develop an app (I swear ). Every good IDE takes care about the running process whether it's finishes or still in...waiting. That why your Shutdown class works within your IDE. If you run this class outside your "protected" IDE environment your class is lost. Why? Let analyse your code. The command Runtime.getRuntime().exec(shutdownCommand); is fired then the next command is executed....and it is an EXIT, your JVM terminates and your previous command is probably still in process. Remember Java JVM works asynchronously.

    You have to remove the System.exit(0) and put it in the JButton method ShutDownBtnActionPerformed(...) or wait for the termination of the exec() execution.
    For example:
    Process p = Runtime.getRuntime().exec(shutdownCommand);
    p.waitfor();

    My advice. IDE relieves a lot of writing work....but it fools its users, too. You learn more if you write your code without IDE (using any fine editor, for example the free editor Jedit of Sourceforge)

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

    Default Re: ShutDown from JButton

    Terminating the other process not the issue here. Rather, it's an exception being thrown before it starts the process. Processes created by the JVM continue to run even after the JVM shuts down.

    @mija

    You're two class file names must match the names of the classes. So sdown.java belongs to the class sdown, not to the class ShutDown. You must also make sure your jar file's folder structure follows your package structure.

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    mija (July 7th, 2012)

  9. #8
    Junior Member
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ShutDown from JButton

    I SOLVED this problem by moving the class
    sdown
    into the
    rd
    package
    Thanks

Similar Threads

  1. Shutdown Remote Computer using java....
    By pupivama in forum Member Introductions
    Replies: 1
    Last Post: May 8th, 2012, 07:44 AM
  2. Replies: 0
    Last Post: October 13th, 2011, 07:05 PM
  3. Replies: 1
    Last Post: November 9th, 2010, 09:58 AM
  4. remote system shutdown
    By prince joseph in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 28th, 2010, 02:35 AM
  5. JButton Help
    By ravjot28 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 17th, 2010, 11:38 AM

Tags for this Thread