私の理解では、SingleInstance
アプリケーションを探していて、バックグラウンドプロセスを実行するために終了したくないのですか?
私は理解していません第3の質問ですが、私はあなたに最初と2番目の質問にまっすぐ答えを与えるつもりです。
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
checkIfRunning(); //Check first if the Application is aready running.
Parent root = FXMLLoader.load(getClass().getResource("fxml/main.fxml"));
primaryStage.setTitle("Hello World");
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
Platform.setImplicitExit(false); //Prevent the Application from Terminating when it's close
}
public static void main(String[] args) {
launch(args);
}
/**The function for checking if the application is already running start here**/
private static final int PORT = 9999;
private static ServerSocket socket;
private static void checkIfRunning() {
try {
//Bind to localhost adapter with a zero connection queue
socket = new ServerSocket(PORT,0, InetAddress.getByAddress(new byte[] {127,0,0,1}));
}
catch (BindException e) {
System.err.println("Application already running.");
System.exit(1);
}
catch (IOException e) {
System.err.println("Unexpected error when checking if application is already running.");
e.printStackTrace();
System.exit(2);
}
}
}
試してみると、アプリケーションを終了するとまだ実行されており、複数のアプリケーションを起動することはできません。
私の3番目の質問は、アプリケーション全体でパラメータ(またはデータ)を渡す方法でした。アイデアはソケットを使用するので、データの送受信にも使用します。 – Happy
また、ソケットまたはデータグラムを使用してアプリケーション間で通信することもできます。 – Polar