Remote desktop to shutdown
I made a program to monitor the active machines in a networked place and perfectly displayed the IP addresses to JList(jList1). When I clicked one of the IP address that is on the list then right clicked the mouse, a JPopupMenu will appear with a menu of "Shutdown". When i tried to click the "shutdown" menu from the popup menu there's an error displayed, says:
Apr 14, 2011 1:57:07 PM UsernamePasswordPackage.NetCafeTime$9 actionPerformed SEVERE: null java.lang.RuntimeException: Unsupported operating system.
at UsernamePasswordPackage.NetCafeTime.shutdown(NetCafeTime.java:359)
at UsernamePasswordPackage.NetCafeTime$9.actionPerformed(NetCafeTime.java:444)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:809)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:850)
at java.awt.Component.processMouseEvent(Component.java:6267)
a开发者_开发技巧t javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Codes I used to show popup menu:
public void showProjectMenu(MouseEvent e){
JPopupMenu menu = new JPopupMenu();
JMenuItem menuItem;
menuItem = new JMenuItem("Shutdown");
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev){
//JOptionPane.showMessageDialog(null, "newline");
try {
shutdown();
} catch (RuntimeException ex) {
Logger.getLogger(NetCafeTime.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(NetCafeTime.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
menu.add(menuItem);
menu.show(e.getComponent(),e.getX(),e.getY());
}
Mouse clicked event codes:
private void jList1MousePressed(java.awt.event.MouseEvent evt) {
if(SwingUtilities.isRightMouseButton(evt)&&!jList1.isSelectionEmpty()&&jList1.locationToIndex(evt.getPoint())==jList1.getSelectedIndex()){
showProjectMenu(evt);
}
}
private void jList1MouseReleased(java.awt.event.MouseEvent evt) {
if(SwingUtilities.isRightMouseButton(evt)&&!jList1.isSelectionEmpty()&&jList1.locationToIndex(evt.getPoint())==jList1.getSelectedIndex()){
showProjectMenu(evt);
}
}
Codes I used to show the popup menu:
public void showProjectMenu(MouseEvent e){
JPopupMenu menu = new JPopupMenu();
JMenuItem menuItem;
menuItem = new JMenuItem("Shutdown");
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev){
//JOptionPane.showMessageDialog(null, "newline");
try {
shutdown();
} catch (RuntimeException ex) {
Logger.getLogger(NetCafeTime.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(NetCafeTime.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
menu.add(menuItem);
menu.show(e.getComponent(),e.getX(),e.getY());
}
Codes I used to shutdown another pc by it's IP address:
public void shutdown()throws RuntimeException, IOException{
String shutdownCommand;
String operatingSystem = System.getProperty("os.name");
if("Linux".equals(operatingSystem)||"Mac OS X".equals(operatingSystem)){
shutdownCommand = "shutdown -h now";
}else if("windows".equals(operatingSystem)){
shutdownCommand = "shutdown.exe -s -t 0";
}else{
throw new RuntimeException("Unsupported operating system.");
}
Runtime.getRuntime().exec(shutdownCommand);
}
Problem is, I cannot get to shutdown the other pc...
Any kind of help would be greatly appreciated and thanks...
The error says: "Unsupported operating system"
and in your shutdown method this is the message of the RuntimeException
which is thrown if the the current operating system is neigher "Linux", "Max OS X", nor "windows". So did you check what System.getProperty("os.name")
actually returns?
EDIT: After re-reading your question I think you want to shut down the machine with the selected IP address. So in your shutdown method I think you'll have to connect to that machine first and execute the shutdown
command there. This means you will need to run a server program on each machine you want to shutdown remotely. Either write your own one or install an SSH server on each and connect to the machine using JSch
I suggest you to check this example.As much as I understand String operatingSystem = System.getProperty("os.name");
this code returns Windows 2000 in this example.
check this too
精彩评论