Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 724661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:14:53+00:00 2026-05-14T06:14:53+00:00

There’s plenty of information on running Java apps as services, but I need to

  • 0

There’s plenty of information on running Java apps as services, but I need to know how to detect whether a windows service is running or not. Does anyone know how???

At the DOS prompt, I can run:

tasklist /svc|findstr "NonRunningService"
echo Return code for N onRunningService is %ERRORLEVEL%
tasklist /svc|findstr "RunningService"
echo Return code for RunningService is %ERRORLEVEL%

I get the following:

Return code for NonRunningService is 1
Return code for RunningService is 0

In code, I have:

int retCode = Runtime.getRuntime.exec("tasklist /svc|findstr \"NonRunningService\"").waitFor();
System.out.println("Return code for NonRunningService is " + retCode);
retCode = Runtime.getRuntime.exec("tasklist /svc|findstr \"RunningService\"").waitFor();
System.out.println("Return code for RunningService is " + retCode);

I get the following output

Return code for NonRunningService is 1
Return code for RunningService is 1

According to the JavaDocs, the waitFor() should block until the process finishes, and give me the exit value of the process.

I’ve also tried using the Process/ProcessBuilder command line calls:

//'tasklist /nh /fi "SERVICES eq RunningService"' will return a line for 
// each running service of the requested type.
Process p1 = new ProcessBuilder("tasklist", "/nh", "/fi" "SERVICES eq RunningService").start();
p1.waitFor();
BufferedReader is = new BufferedReader(new InputStreamReader(p1.getInputStream()));
String line = is.readLine();
System.out.println("Service - " + line);
System.out.println("Running? ", (line==null?"No":"Yes");

gives:

Service -
Running? No

even when I get lines in the output at the command line!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:14:54+00:00Added an answer on May 14, 2026 at 6:14 am

    I recreated your code and added some extra debugging output by creating a class to print the output from process:

    private static class Writer implements Runnable {
    
        private InputStream is;
    
        public Writer(InputStream is) {
            this.is = is;
        }
    
        @Override
        public void run() {
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
    

    In the main method I then started an instance of this before calling waitFor():

    public static void main(String[] args) throws Exception {
        String command = "tasklist /svc | findstr \"svchost.exe\"";
        Process p = Runtime.getRuntime().exec(command);
    
        new Thread(new Writer(p.getInputStream())).start();
        new Thread(new Writer(p.getErrorStream())).start();
    
        System.out.println("Return code: " + p.waitFor());
    
    }
    

    The output from this is:

    ERROR: Invalid argument/option - '|'.
    Type "TASKLIST /?" for usage.
    

    Because the command isn’t executed in a shell, you first need to call the Windows shell, and pass in the command as an argument:

    String command = "cmd /c tasklist /svc | findstr \"svchost.exe\"";
    

    After changing this the output is now Return code: 0.

    I’ve found a strange issue however, in that if you don’t handle the output from the process’s stdout channel, for some reason the process does not terminate. To get around this, I’ve had to put in a loop to read and discard the output from the process:

    public static void main(String[] args) throws Exception {
        String command = "cmd /c tasklist /svc | findstr \"svchost.exe\"";
        Process p = Runtime.getRuntime().exec(command);
        while(p.getInputStream().read() != -1) {} //hangs without this line
    
        System.out.println("Return code: " + p.waitFor());
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are 2 servers, they need to know the status(live oe dead) each other.
There are several major unit tests frameworks, but as far as I know all
There's http://www.amazon.com/gp/product/0321278542/ but it looks a bit dated. Specificaly, it talks about bits that
There are two following options in Java HotSpot VM Options: -XX:OnError=<cmd args>;<cmd args> Run
There are huge numbers of threads running in parallel continuously (let's assume this continuous
There is no problem to get the src or alt separately,but how can get
I need to clean up various Word 'smart' characters in user input, including but
There's a lot of reading on self referencing problems, but I can't seem to
There are things like f.call(...) f.apply(...) But then there's this (1, alert)('Zomg what is
There is a lot of posts about nested Ajax problems, but I can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.