Cześć.
Podczas testów aplikacji mam niespotykany błąd. Google przeszukane niestety bez rezultatów.
Aplikacja startuje w trayu i wyświetla komunikaty o pewnych zdarzeniach. Gdy uruchomię aplikację dwuklikiem to niestety komunikaty się nie pojawiają (aplikacja działa, ale bez komunikatów). Gdy odpalę ja z consoli z** java -jav app.jar **wszystko działa bez problemów. Postanowiłem problem obejść i odpalić apkę z bata, ale niestety tutaj jest sytuacja taka, że konsola zostaje otwarta po uruchomieniu z komendy start java -jar app.jar.
Poszukuję rozwiązania, albo z plikiem bat tak by konsola się zamykała, ale nie zamykała aplikacji lub modyfikacja kodu javy. Ewentualnie czy istnieje możliwość odpalenia pliku jar z komendą java -jar ? Dopisanie tego w manifescie czy gdzieś ?
Winne są pewnie komunikaty i odpalenie apki w trayu.
Klasa komunikatów:
public class NotificationControl {
private static Notifications notificatorBulider;
public Notifications getNotificatorBulider() {
return notificatorBulider;
}
public void createNotification(String title, String text) {
Stage dummyPopup = new Stage();
dummyPopup.initModality(Modality.NONE);
dummyPopup.initStyle(StageStyle.UTILITY);
dummyPopup.setOpacity(0d);
final Screen screen = Screen.getPrimary();
final Rectangle2D bounds = screen.getVisualBounds();
dummyPopup.setX(bounds.getMaxX());
dummyPopup.setY(bounds.getMaxY());
final Group root = new Group();
dummyPopup.setScene(new Scene(root, 1d, 1d, Color.TRANSPARENT));
dummyPopup.show();
notificatorBulider = Notifications.create()
.title(title)
.text(text)
.hideAfter(Duration.hours(24))
.position(Pos.BOTTOM_RIGHT)
.onAction(event ->{
notificatorBulider = null;
if (Main.getStage() != null) {
Main.getStage().show();;
Main.getStage().toFront();
}
});
notificatorBulider.showInformation();
}
}
wycinek klasy wyswietlajacej komuniakt:
//Listener nasłuchuje czy jest nowa wiadomość i wyświetla komunikat
NotificationControl n = new NotificationControl();
MessageControls.isNewMessage().addListener((arg, oldVal, newVal) ->{
Platform.runLater(() -> {
String title = "Nowa Wiadomość!";
String message = "Masz nową wiadomość! Otwórz program " + Main.appName;
if(!(n.getNotificatorBulider() != null))
n.createNotification(title, message);
});
});
}
i klasa main
public class Main extends Application{
protected static Stage stage;
public static Stage getStage() { return stage;}
public static void setStage(Stage stage) {Main.stage = stage;}
public static void main(String[] args) { launch(args); }
@Override
public void start(Stage primaryStage) throws Exception {
Platform.setImplicitExit(false);
javax.swing.SwingUtilities.invokeLater(() -> {
try { addAppToTray();
} catch (AWTException | IOException e ) {e.printStackTrace();}
});
Main.stage = primaryStage;
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/main/Main.fxml"));
Pane pane = loader.load();
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
SceenControl.setDefaultScreent();
primaryStage.getIcons().add(new javafx.scene.image.Image((this.getClass().getResourceAsStream("appIcon.jpg"))));
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
primaryStage.hide();
}
});
}
private static java.awt.SystemTray tray;
private void addAppToTray() throws AWTException, IOException {
java.awt.Toolkit.getDefaultToolkit();
if (!java.awt.SystemTray.isSupported()) {
System.out.println("No system tray support, application exiting.");
Platform.exit();
}
tray = java.awt.SystemTray.getSystemTray();
ImageIcon imageIcon = new ImageIcon(getClass().getResource("/icons/appIconTray.jpg"));
Image image = imageIcon.getImage();
java.awt.TrayIcon trayIcon = new java.awt.TrayIcon(image);
trayIcon.addActionListener(event -> Platform.runLater(this::showStage));
java.awt.MenuItem openItem = new java.awt.MenuItem(appName + " (Otwórz)");
openItem.addActionListener(event -> Platform.runLater(this::showStage));
java.awt.Font defaultFont = java.awt.Font.decode(null);
java.awt.Font boldFont = defaultFont.deriveFont(java.awt.Font.BOLD);
openItem.setFont(boldFont);
java.awt.MenuItem exitItem = new java.awt.MenuItem("Exit");
exitItem.addActionListener(event -> {
MessageNewTask ms = new MessageNewTask();
ms.cancel();
Platform.exit();
tray.remove(trayIcon);
});
final java.awt.PopupMenu popup = new java.awt.PopupMenu();
popup.add(openItem);
popup.addSeparator();
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
tray.add(trayIcon);
}
private void showStage() {
if (stage != null) {
stage.show();
stage.toFront();
}
}