Remote Shutdown using Java

Discussion in 'Java' started by christopher_board@yahoo.co.uk, Jun 14, 2007.

  1. Guest

    Hi all. I want to be able to shutdown remote computers using Java.
    Below are the things that have been imported :
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.rmi.*;
    import java.rmi.server.*;
    import javax.swing.*;

    and below is the code

    public void remoteShutdown_actionPerformed(ActionEvent e) {
    try{
    Runtime.getRuntime().exec("shutdown -m \\toshiba_cwb -s -t
    99");
    }catch(Exception ex){System.out.println("Fail to
    ShutDown"+ex);}

    The code works fine without the the -m and the computer name, however
    when I put the -m \\computerName it won't shut the computer down but
    no error messages have been displayed.

    What is wrong with this.

    Any help in this matter would be truly appreciated.
    , Jun 14, 2007
    #1
    1. Advertising

  2. Eric Sosman Guest

    wrote On 06/14/07 17:08,:
    > Hi all. I want to be able to shutdown remote computers using Java.
    > Below are the things that have been imported :
    > import java.awt.*;
    > import java.awt.event.ActionEvent;
    > import java.awt.event.ActionListener;
    > import java.rmi.*;
    > import java.rmi.server.*;
    > import javax.swing.*;
    >
    > and below is the code
    >
    > public void remoteShutdown_actionPerformed(ActionEvent e) {
    > try{
    > Runtime.getRuntime().exec("shutdown -m \\toshiba_cwb -s -t
    > 99");
    > }catch(Exception ex){System.out.println("Fail to
    > ShutDown"+ex);}
    >
    > The code works fine without the the -m and the computer name, however
    > when I put the -m \\computerName it won't shut the computer down but
    > no error messages have been displayed.
    >
    > What is wrong with this.
    >
    > Any help in this matter would be truly appreciated.


    I have no idea what this "shutdown" program you're
    using is; it certainly doesn't look like the one I know.
    Nonetheless, I have a suspicion: are the two backslashes
    part of the actual command syntax? That is, do you need
    two backslashes in the executed command line? If so, be
    aware that what you've written is only *one* backslash,
    because of the way the Java compiler uses \ in strings
    to introduce hard-to-type characters. To get two, you'll
    need to double up each of them:

    ... ("shutdown -m \\\\toshiba_cwb ...");

    Depending on what happens to the command line after
    you launch it, even that might not be enough. For example,
    if the \ is also special to the command processor ("shell"),
    then you may need to double it yet again or escape it by
    whatever mechanism the shell uses:

    ... ("shutdown -m \\\\\\\\toshiba_cwb ...");

    (Intepretation: The Java compiler generates one "delivered"
    backslash for each pair in the source, making four. Then
    the shell makes one backslash out of each pair that *it*
    sees, making two. YMMV.)

    --
    Eric Sosman, Jun 14, 2007
    #2
    1. Advertising

  3. Guest

    On 14 Jun, 22:29, Eric Sosman <> wrote:
    > wrote On 06/14/07 17:08,:
    >
    >
    >
    >
    >
    > > Hi all. I want to be able to shutdown remote computers using Java.
    > > Below are the things that have been imported :
    > > import java.awt.*;
    > > import java.awt.event.ActionEvent;
    > > import java.awt.event.ActionListener;
    > > import java.rmi.*;
    > > import java.rmi.server.*;
    > > import javax.swing.*;

    >
    > > and below is the code

    >
    > > public void remoteShutdown_actionPerformed(ActionEvent e) {
    > > try{
    > > Runtime.getRuntime().exec("shutdown -m \\toshiba_cwb -s -t
    > > 99");
    > > }catch(Exception ex){System.out.println("Fail to
    > > ShutDown"+ex);}

    >
    > > The code works fine without the the -m and the computer name, however
    > > when I put the -m \\computerName it won't shut the computer down but
    > > no error messages have been displayed.

    >
    > > What is wrong with this.

    >
    > > Any help in this matter would be truly appreciated.

    >
    > I have no idea what this "shutdown" program you're
    > using is; it certainly doesn't look like the one I know.
    > Nonetheless, I have a suspicion: are the two backslashes
    > part of the actual command syntax? That is, do you need
    > two backslashes in the executed command line? If so, be
    > aware that what you've written is only *one* backslash,
    > because of the way the Java compiler uses \ in strings
    > to introduce hard-to-type characters. To get two, you'll
    > need to double up each of them:
    >
    > ... ("shutdown -m \\\\toshiba_cwb ...");
    >
    > Depending on what happens to the command line after
    > you launch it, even that might not be enough. For example,
    > if the \ is also special to the command processor ("shell"),
    > then you may need to double it yet again or escape it by
    > whatever mechanism the shell uses:
    >
    > ... ("shutdown -m \\\\\\\\toshiba_cwb ...");
    >
    > (Intepretation: The Java compiler generates one "delivered"
    > backslash for each pair in the source, making four. Then
    > the shell makes one backslash out of each pair that *it*
    > sees, making two. YMMV.)
    >
    > --
    > - Hide quoted text -
    >
    > - Show quoted text -


    Thanks for your help that works fine
    , Jun 14, 2007
    #3
  4. Roedy Green Guest

    On Thu, 14 Jun 2007 14:08:30 -0700,
    wrote, quoted or indirectly quoted someone who said :

    > Runtime.getRuntime().exec("shutdown -m \\toshiba_cwb -s -t
    >99");


    to solve a problem like this, first solve it issuing the shutdown
    command from the command line.

    Then when you have that working, use quoter at
    http://mindprod.com/applets/quoter.html

    to convert the command line into a java string that you can feed to
    exec. It handles the \ doubling etc. for you.

    See: http://mindprod.com/jgloss/shutdown.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Jun 15, 2007
    #4
  5. Eric Sosman wrote:
    > wrote On 06/14/07 17:08,:
    >> Hi all. I want to be able to shutdown remote computers using Java.
    >> Below are the things that have been imported :
    >> import java.awt.*;
    >> import java.awt.event.ActionEvent;
    >> import java.awt.event.ActionListener;
    >> import java.rmi.*;
    >> import java.rmi.server.*;
    >> import javax.swing.*;
    >>
    >> and below is the code
    >>
    >> public void remoteShutdown_actionPerformed(ActionEvent e) {
    >> try{
    >> Runtime.getRuntime().exec("shutdown -m \\toshiba_cwb -s -t
    >> 99");
    >> }catch(Exception ex){System.out.println("Fail to
    >> ShutDown"+ex);}
    >>
    >> The code works fine without the the -m and the computer name, however
    >> when I put the -m \\computerName it won't shut the computer down but
    >> no error messages have been displayed.
    >>
    >> What is wrong with this.
    >>
    >> Any help in this matter would be truly appreciated.

    >
    > I have no idea what this "shutdown" program you're
    > using is; it certainly doesn't look like the one I know.


    It's the CLI way of shutting down a Windows PC. The OP took it for
    granted that wanting to shutdown remote computers really meant to us
    that he wanted to shutdown only Windows-based remote computers since
    Windows-based computers are the only ones that exist.
    Brandon McCombs, Jun 16, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    0
    Views:
    888
  2. wurznsepp
    Replies:
    2
    Views:
    4,783
    wurznsepp
    Jan 26, 2004
  3. julia
    Replies:
    6
    Views:
    456
    Sudsy
    Oct 19, 2004
  4. Tim Gosselin
    Replies:
    1
    Views:
    616
    Michael Hobbs
    Jan 12, 2005
  5. Replies:
    3
    Views:
    956
Loading...

Share This Page