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.
Die Prozesse werden mit der Klasse „ProcessHandle“ aus gelesen, diese ist aber erst ab „Java 9“ verfügbar.
Variablen
private static boolean b = false;
Main-Methode
public static void main(String[] args) {
if (isAppAlive()) {
System.out.println("Läuft schon");
new Thread(taskAlert).start();
} else {
System.out.println("Starten");
launch(args);
}
}
Methoden
private static boolean isAppAlive() {
ProcessHandle.allProcesses()
.forEachOrdered(process -> {
// Hier natürlich eine andere exe angeben
if(processDetails(process).equals("javaw.exe")) {
b = true;
}
});
return b;
}
private static String processDetails(ProcessHandle process) {
String string = String.format("%1s", text(process.info().command()));
File file = new File(string);
System.out.println(file.getName());
return file.getName();
}
private static String text(Optional<?> optional) {
return optional.map(Object::toString).orElse("-");
}
Und hier nochmal ein Beispiel mit einem Fenster
import java.io.File;
import java.util.Optional;
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 DateiLock2 extends Application {
private static boolean b = false;
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(taskAlert).start();
} else {
System.out.println("Starten");
launch(args);
}
}
private static boolean isAppAlive() {
ProcessHandle.allProcesses()
.forEachOrdered(process -> {
// Hier natürlich eine andere exe angeben
if(processDetails(process).equals("javaw.exe")) {
b = true;
}
});
return b;
}
private static String processDetails(ProcessHandle process) {
String string = String.format("%1s", text(process.info().command()));
File file = new File(string);
System.out.println(file.getName());
return file.getName();
}
private static String text(Optional<?> optional) {
return optional.map(Object::toString).orElse("-");
}
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(ContentText);
alert.showAndWait();
}
});
}
// Alert muss in einem Task ausgeführt werden, sonst gibt es eine Exception
static Task taskAlert = new Task<Void>() {
@Override public Void call() {
meldung(AlertType.WARNING, "", "Programm läuft schon.");
return null;
}
};
}