0

Well, i'm working a project to test my knowledge of java programming.

My project is a game and i'm making some kind of launcher for it that closes when the project has fully booted. I searched on the web to see how i check if a java program is running or not. I found a way to see if a program is running (here is the link to it or see code below). I tested it but it didn't work so i searched for the answer to check if a process/program is running under a "bigger" program (exemple: multiple windows in firefox). I didn't find it so i hope that you know the answer.

Here is the way to check if a program is running that i found (And here is the link again):

String line;
String pidInfo ="";

Process p =Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");

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

while ((line = input.readLine()) != null) {
    pidInfo+=line; 
}

input.close();

if(pidInfo.contains("file.class"))
{
    // do what you want
}
Community
  • 1
  • 1
Beau
  • 33
  • 10

1 Answers1

1

Instead of checking whether the game process is running (Which isn't so possible with the pid, since that's a number), you can check the contents of a file:

File f = new File("path_to_file.txt");
BufferedReader br;
String text = "";
try {
     br= new BufferedReader(new FileReader(f));
     String line;
     while((line = br.readLine())!=null){
         text+=line;
     }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
if(text.equals("started")){
    // we have to make sure it can work next time so we clear the file's contents
    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(f));
        bw.write("");
        bw.flush();
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //close launcher
}

And then the only thing left to do is have the game write to that file the text "started" after it has started.

ItamarG3
  • 4,092
  • 6
  • 31
  • 44