Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

First of all, i have to state that this is not intended for malicius use.

I am fed up with iTunes, so i want to write a program that detects it running and kills it.

How do you do that in Java?
Posted
Comments
Slacker007 3-Apr-12 5:29am    
Is this for your own use or for others as well?
Liam S. Crouch 3-Apr-12 7:11am    
For my own use, but i may make a version to be shared on my website if i have time for a nice GUI.
Richard MacCutchan 3-Apr-12 5:56am    
Why not just uninstall it.
TorstenH. 3-Apr-12 6:05am    
I would uninstall that too - there are other programs out there to feed your Apple device with music. SharePod for example is really good.
Liam S. Crouch 3-Apr-12 7:12am    
Because i need it some times. I just want it dead while i dont need it

1 solution

You could try calling external processes to do the job, like this;

First a helper class called StreamEater;
Java
package my.company;
 
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
 
public class StreamEater extends Thread
{
    private final InputStream is;
    private final StringBuilder result = new StringBuilder();
    
    private boolean failed = false;
    private Exception exception;
    
    StreamEater(final InputStream is)
    {
        this.is = is;
    }
    
    public String getResult() {
        return result.toString();
    }
    
    public boolean hasFailed() {
        return failed;
    }
    
    public Exception getException() {
        return exception;
    }
    
    public void run()
    {
        try
        {
            final InputStreamReader isr = new InputStreamReader(is);
            final BufferedReader br = new BufferedReader(isr);
            
            String line = null;
            while ((line = br.readLine()) != null) {
                result.append(line + "\n");
            }
        } 
        catch (Exception e)
        {
            exception = e;
            failed = true;  
        }
    }
}  


And then have an application that looks something like this;

Java
package my.company;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class Program {
    
    private static String executeCommand(String[] command) throws IOException, InterruptedException {
        final Process process = Runtime.getRuntime().exec(command);
        final StreamEater error = new StreamEater(process.getErrorStream());
        final StreamEater output = new StreamEater(process.getInputStream());
        
        error.start();
        output.start();
        
        final int exitCode = process.waitFor();
        
        // Check errors
        if (error.hasFailed())
            System.err.println("Encountered problems reading error stream; " + error.getException().getMessage());
        else {
            if (error.getResult().length() > 0) {
                System.err.println("Error output:");
                System.err.println(error.getResult());
            }
        }
        
        // Check output
        if (output.hasFailed()) {
            System.err.println("Encountered problems reading output stream; " + output.getException().getMessage());
        }

        return output.getResult();
    }
    
    private static String executeCommand(String command) throws IOException, InterruptedException {
        return executeCommand(new String[] { command });
    }
    

    
    public static List<integer> getPids(String exeName) throws IOException, InterruptedException {
        String result = executeCommand("tasklist");
        
        StringTokenizer tokenizer = new StringTokenizer(result, "\r\n");
        
        List<integer> pids = new ArrayList<integer>();
        while(tokenizer.hasMoreTokens()) {
            String line = tokenizer.nextToken();
            if (line.startsWith(exeName)) {
                StringTokenizer spaceTokenizer = new StringTokenizer(line, " ");
                if (spaceTokenizer.hasMoreTokens()){
                    spaceTokenizer.nextToken();
                    if (spaceTokenizer.hasMoreTokens()){
                        pids.add(Integer.parseInt(spaceTokenizer.nextToken()));
                    }
                }
            }
        }
        
        return pids;
    }
    
    public static void killProcess(int pid) throws IOException {
        Runtime.getRuntime().exec("taskkill /F /PID " + pid);
    }
    
    public static void main(String[] args) throws Exception {

        List<integer> pids = getPids("notepad.exe");
        for(int pid : pids) {
            System.out.println(pid);
            killProcess(pid);
        }
        
    }
}</integer></integer></integer></integer>


Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900