File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 19790
    
  20

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: 19790
    
  20

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: 40052
    
  28
Moving thread because it is too difficult for “beginning”.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

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: 19790
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remotely checking Windows services in java