wood burning stoves*
The moose likes Java in General and the fly likes Remotely checking Windows services in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Remotely checking Windows services in java" Watch "Remotely checking Windows services in java" New topic
Author

Remotely checking Windows services in java

sadhu charan
Greenhorn

Joined: Nov 27, 2012
Posts: 18
Hi good Afternoon,
I am trying to check some particular windows services running or stop remotely throw java program that program works fine in local system and give the out put but when i put Ip address of remote machine that is not showing anything thing but in command prompt it display the output of remote computer not in java program, My java program is like that

package com.sadhu;

import java.sql.*;
import java.io.*;
import java.text.*;
public class CheckServices
{
public static void main(String args[])
{
try
{
Process p=Runtime.getRuntime().exec("sc query ALG");

BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));

String line=reader.readLine();
while(line!=null)
{
if(line.trim().startsWith("STATE"))

{

if (line.trim().substring(line.trim().indexOf(":")+1,line.trim().indexOf(":")+4).trim().equals("1"))
System.out.println("Stopped");
else
if (line.trim().substring(line.trim().indexOf(":")+1,line.trim().indexOf(":")+4).trim().equals("2"))
System.out.println("Startting....");
else
if (line.trim().substring(line.trim().indexOf(":")+1,line.trim().indexOf(":")+4).trim().equals("3"))
System.out.println("Stopping....");
else
if (line.trim().substring(line.trim().indexOf(":")+1,line.trim().indexOf(":")+4).trim().equals("4"))
System.out.println("Running");

}
line=reader.readLine();
}

}

catch(IOException e1) { }



}
}


It works fine for Local system but not working for remote server , i put command line "sc \\43.45.46.100 query ALG" it display the status of Remote Services ,When i put "sc \\43.45.46.100 query ALG" in place of "sc query ALG" then it is not working or not showing anything


Thanks in Advance
SCP

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Did you escape the \\ in your Java String to \\\\?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
sadhu charan
Greenhorn

Joined: Nov 27, 2012
Posts: 18
Thanks Rob Spoor That is solved I did like this sc \\\\43.45.46.100 and it work fine , Thanks once again
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You're welcome.
sadhu charan
Greenhorn

Joined: Nov 27, 2012
Posts: 18
Hi Rob Spoor It is working for Services which only contain one word like "ALG" but which window services contain space in middle like "SOS SIT Standby-38087" that is not working in this command like
sc \\\\43.45.46.100 query SOS SIT Standby-38087 or i did like sc \\\\?43.45.46.100 query SOS SIT Standby-38087 boths are not working can you help me ,
Thanks in advance
SCP
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
Moving thread because it is too difficult for “beginning”.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Quote the service name. The command is sc \\43.45.46.100 query "SOS SIT Standby-38087" so as a String it becomes this (escaping the \ and " characters):
But are you sure that's the service name, and not just the display name?
sadhu charan
Greenhorn

Joined: Nov 27, 2012
Posts: 18
Thanks to all it is working which is suggested by Rob Spoor

Thank you
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You're welcome.
 
jQuery in Action, 2nd edition
 
subject: Remotely checking Windows services in java
 
Similar Threads
convert .txt file in .csv format
Issue in passing command line arguments
FTP very slow
modifying an input file based on pattern matching
exercise getServiceByName