0

I'm trying to do a cli aplication in java with spring and picocli that you start it on cmd and with a dialog you do some configurations to program a task, (in this case show an "alarm messege" on the same cmd.) I used Timer and TimerTask and it works but I can't do anything else on cmd until the app do this task and finish.

That I would love is to config the task, finish this configuration dialog, be able to do anything else with the command line and in the date and time sheduled print a message on cmd or also a loop message every XX minutes.

I expended a lot of time trying to search a solution but may be this is not possible...

Other option that I'm thinking maybe would be a good solution would be a way to use a custom commands from any where in the cmd (as enviroment variables) and interact with application running on the background continously. In this way the cmd would be only a comunication way to send or recive information form this app in the background.

But again, I didn't find any way to do this neither.

Is there any way to do any one of this two options or do you have other idea to achive this task?

Thanks in advance!

  • Interesting question. No, I don’t think it is possible in pure Java the way you describe it. Maybe with some operating system specific support it will be. Which OS is this for? Would you want it to work on more than one OS? – Ole V.V. Aug 02 '21 at 02:57
  • I'm trying to run it on windows, for sure I would like it works on all OS if possible! This morning I had an idea, I put it as an answer below. It's less ambitious but it works. – Gerard Puig Aug 02 '21 at 13:10

1 Answers1

1

I have found an alternative solution to this problem but less ambitious. I leave it here in case someone has a similar problem.

It consists of making a small new program with the Alarm functionality and sending by arguments the date and time of the scheduled task, at the scheduled time, the application will send jOptionpanel with the alarm:

public class Alarm {

    public static void main(String[] args) {
        String alarmDate = args[0];
        String alarmTime = args[1];

        LocalDateTime taskTime = LocalDateTime.parse(alarmDate + "T" + alarmTime);
        Date taskTimeAsDate = Date.from(taskTime.atZone(ZoneId.systemDefault()).toInstant());

        Timer timer = new Timer();
        timer.schedule(setAlarm("Wake Up! Wake Up!"), taskTimeAsDate);  
    }

    private static TimerTask setAlarm(String message) {
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                Component Jframe = null;
                JOptionPane.showMessageDialog(Jframe, message, "Alarm Ringing", JOptionPane.PLAIN_MESSAGE);
                System.exit(0);
            }
        };
        return timerTask;
    }
}

And I call the jar from the cli app with ProcessBuilder:

    public void setAlarm(String alarmDate, String alarmTime) throws IOException {   
        ProcessBuilder timer = new ProcessBuilder("java","-jar", "Alarm.jar", alarmDate, alarmTime);
        timer.start();
    }

In this way the alarm is scheduled and the cmd free! If someone has other idea or solution I'd really love to know it!