Hier noch eine kleine Funktion, wie man einen zweiten Programm-Start verhindert, wenn das Programm schon Läuft.
Hierzu werden die laufenden Prozesse (vom Task-Manager) ausgelesen und und nach der dementsprechend „.exe“ gesucht, wenn sie in der Liste gefunden wurde, dann wird das Programm nicht gestartet.
Diese Methode läuft nur unter Windows
Main-Methode
public static void main(String[] args) {
if (isAppAlive()) {
System.out.println("Läuft schon");
new Thread(task).start();
} else {
System.out.println("Starten");
launch(args);
}
}
Methoden
private static boolean isAppAlive() {
try {
String line;
Process p = Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line); //<-- Parse data here.
// Hier natürlich eine andere exe angeben
if (line.contains("javaw.exe")) {
return true;
}
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
return false;
}
Ein Beispiel mit Fenster
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.channels.*;
import java.nio.channels.FileLock;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.jutils.jprocesses.JProcesses;
import org.jutils.jprocesses.model.ProcessInfo;
import application.funktionen.PfadErmitteln;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class DateiLock3 extends Application {
private Button button;
@Override
public void start(Stage primaryStage) {
try {
primaryStage.setTitle("Fenster");
StackPane root = new StackPane();
Scene scene = new Scene(root,400,400);
button = new Button();
button.setText("Klick mich");
root.getChildren().add(button);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void stop() throws Exception {
System.out.println("Stop");
}
public static void main(String[] args) {
if (isAppAlive()) {
System.out.println("Läuft schon");
new Thread(task).start();
} else {
System.out.println("Starten");
launch(args);
}
}
private static boolean isAppAlive() {
try {
String line;
Process p = Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line); //<-- Parse data here.
// Hier natürlich eine andere exe angeben
if (line.contains("javaw.exe")) {
return true;
}
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
return false;
}
private static void meldung(AlertType alertType, String title, String ContentText) {
Platform.runLater(new Runnable() {
@Override
public void run() {
Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText("Das Programm läuft schon");
alert.showAndWait();
}
});
}
// Alert muss in einem Task ausgeführt werden, sonst gibt es eine Exception
static Task task = new Task<Void>() {
@Override public Void call() {
meldung(AlertType.WARNING, "", "Programm läuft schon.");
return null;
}
};
}