Ce que dit Thelvin est correct.
Cependant si tu veux un truc pour windows qui marche +-, j'espère que ca te servira j'ai passé 20 minutes dessus ^^ (bon c'est bricolage mais ca marche).
public enum TYPE {ADDED, REMOVED};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| /**
* @author Warnotte Renaud
*
*/
public class WProcess
{
long PID;
String name;
long size;
String type;
public WProcess(long pID, String name, long size, String type)
{
super();
PID = pID;
this.name = name;
this.size = size;
this.type = type;
}
public synchronized long getPID()
{
return PID;
}
public synchronized void setPID(long pID)
{
PID = pID;
}
public synchronized String getName()
{
return name;
}
public synchronized void setName(String name)
{
this.name = name;
}
public synchronized long getSize()
{
return size;
}
public synchronized void setSize(long size)
{
this.size = size;
}
public synchronized String getType()
{
return type;
}
public synchronized void setType(String type)
{
this.type = type;
}
@Override
public String toString()
{
return "WProcess [PID=" + PID + ", name=" + name + "]";
}
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.swing.event.EventListenerList;
/**
* @author Warnotte Renaud
*
*/
public class ThreadList extends Thread
{
EventListenerList ProcessNotifyListeners = new EventListenerList();
int PollingDelay = 500; // ms
/**
* @param args
*/
public static void main(String[] args)
{
ThreadList tl = new ThreadList();
tl.start();
}
public void run()
{
List<WProcess> lastList;
try
{
lastList = ProcessLister.getProcessesList();
while (true) // Pas bien :)
{
Thread.sleep(PollingDelay);
List<WProcess> ret = ProcessLister.getProcessesList();
compareList(lastList, ret);
lastList = ret;
}
}
catch (IOException | InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param lastList
* @param newList
*/
private void compareList(List<WProcess> lastList, List<WProcess> newList)
{
for (int i = 0; i < lastList.size(); i++)
{
WProcess wp = lastList.get(i);
if (isPIDExistInList(newList, wp.PID) == false)
{
fireProcessNotify(new ProcessNotificationEvent(TYPE.REMOVED, wp));
//System.err.println("Process " + wp + " removed");
}
}
for (int i = 0; i < newList.size(); i++)
{
WProcess wp = newList.get(i);
if (isPIDExistInList(lastList, wp.PID) == false)
fireProcessNotify(new ProcessNotificationEvent(TYPE.ADDED, wp));
//System.err.println("Process " + wp + " added");
}
}
private boolean isPIDExistInList(List<WProcess> lastList, long PID)
{
for (Iterator<WProcess> iterator = lastList.iterator(); iterator.hasNext();)
{
WProcess wProcess = (WProcess) iterator.next();
if (wProcess.PID == PID)
return true;
}
return false;
}
public void addProcessNotifyListener(ProcessNotifiable listener)
{
ProcessNotifyListeners.add(ProcessNotifiable.class, listener);
}
public void removeProcessNotifyListener(ProcessNotifiable listener)
{
ProcessNotifyListeners.remove(ProcessNotifiable.class, listener);
}
public void fireProcessNotify(ProcessNotificationEvent xxxEvent)
{
Object[] listeners = ProcessNotifyListeners.getListenerList();
// loop through each listener and pass on the event if needed
int numListeners = listeners.length;
for (int i = 0; i < numListeners; i += 2)
{
if (listeners[i] == ProcessNotifiable.class)
{
// pass the event to the listeners event dispatch method
((ProcessNotifiable) listeners[i + 1]).ProcessNotification((xxxEvent));
}
}
}
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| /**
* @author Warnotte Renaud
*/
public class ProcessNotificationEvent
{
TYPE type;
WProcess process;
public ProcessNotificationEvent(TYPE type, WProcess process)
{
super();
this.type = type;
this.process = process;
}
@Override
public String toString()
{
return "ProcessNotificationEvent [type=" + type + ", process=" + process + "]";
}
} |
1 2 3 4 5 6 7 8 9 10 11
| import java.util.EventListener;
/**
*
* @author Warnotte Renaud
*
*/
public interface ProcessNotifiable extends EventListener
{
void ProcessNotification(ProcessNotificationEvent wp);
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author user
*/
public class ProcessLister
{
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException
{
List<WProcess> lastList = getProcessesList();
}
static List<WProcess> getProcessesList() throws IOException
{
List<WProcess> ret = new ArrayList<>();
String line;
Process p = Runtime.getRuntime().exec(System.getenv("windir") + "\\system32\\" + "tasklist.exe" + " /FO CSV");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
line = input.readLine();
while ((line = input.readLine()) != null)
{
line = line.replaceAll("\"", "");
String splitted[] = line.split(",");
WProcess pro = createProcessFromCSVString(splitted);
// Filtrage pour ne pas avoir le processus qu'on vient de lancer
if (pro.getName().contains("tasklist.exe"))
continue;
if (pro.getName().contains("conhost.exe"))
continue;
ret.add(pro);
}
input.close();
return ret;
}
/**
* @param splitted
* @return
*/
private static WProcess createProcessFromCSVString(String[] splitted)
{
long PID = new Long("" + splitted[1]);
String Name = splitted[0];
String Type = splitted[2];
WProcess pro = new WProcess(PID, Name, 0, Type);
return pro;
}
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| /**
* @author Warnotte Renaud
*
*/
public class mainExemple implements ProcessNotifiable
{
public static void main(String args[])
{
new mainExemple();
}
public mainExemple()
{
ThreadList tl = new ThreadList();
tl.addProcessNotifyListener(this);
tl.start();
}
@Override
public void ProcessNotification(ProcessNotificationEvent wp)
{
System.err.println("Process notification : "+wp);
}
} |
EDIT : Note que ca fonctionne par Polling (500ms reglable) et que donc, tu px looper des processus tres rapide (creer puis detruit en moins de 500ms) donc ce n'est pas la panacée, juste une bricole depannage
Partager