Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8399311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:16:06+00:00 2026-06-09T21:16:06+00:00

I’d like to generate alarms on my Java desktop application : alarms set with

  • 0

I’d like to generate alarms on my Java desktop application :

  • alarms set with a specific date/time which can be in 5 minutes or 5 months
  • I need to be able to create a SWT application when the alarm is triggered
  • I need this to be able to work on any OS. The software users will likely have Windows (90% of them), and the rest Mac OS (including me)
  • the software license must allow me to use it in a commercial program, without requiring to open source it (hence, no GPL)
  • I cannot require the users to install Cygwin, so the implementation needs to be native to Windows and Unix

I am developing using Java, Eclipse, SWT and my application is deployed from my server using Java Web Start. I’m using Mac OS X.6 for developing.


I think I have a few options:

  1. Run my application at startup, and handle everything myself;
  2. Use a system service.
  3. Use the cron table on Unix, and Scheduled Tasks on Windows

Run at startup

I don’t really like this solution, I’m hoping for something more elegant.
Refs: I would like to run my Java program on System Startup on Mac OS/Windows. How can I do this?

System service

If I run it as a system service, I can benefit from this, because the OS will ensure that my software:

  • is always running
  • doesn’t have/need a GUI
  • restarts on failure

I’ve researched some resources that I can use:

  • run4j — CPL — runs on Windows only, seems like a valid candidate
  • jsvc — Apache 2.0 — Unix only, seems like a valid candidate
  • Java Service Wrapper — Various — I cannot afford paid licenses, and the free one is a GPL. Hence, I don’t want to/can’t use this

My questions in the system service options are:

  1. Are there other options?
  2. Is my planned implementation correct:

    • at the application startup, check for existence of the service
    • if it is not installed:
      • escalate the user to install the service (root on Unix, UAC on Windows)
      • if the host OS is Windows, use run4j to register the service
      • if the host OS is Unix, use jsvc to register the service
    • if it is not running, start it

Thus, at the first run, the application will install the service and start it. When the application closes the service is still running and won’t need the application ever again, except if it is unregistered.
However, I think I still miss the “run on startup” feature.

Am I right? Am I missing something?

cron / Task Scheduler

On Unix, I can easily use the cron table without needing the application to escalate the user to root. I don’t need to handle restarts, system date changes, etc. Seems nice.

On Windows, I can use the Task Scheduler, even in command-line using At or SchTasks. This seems nice, but I need this to be compatible from XP up to 7, and I can’t easily test this.


So what would you do? Did I miss something? Do you have any advice that could help me pick the best and most elegant solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:16:07+00:00Added an answer on June 9, 2026 at 9:16 pm

    Here’s what I ended up implementing:

    public class AlarmManager {
        public static final String ALARM_CLI_FORMAT = "startalarm:";
        public static SupportedOS currentOS = SupportedOS.UNSUPPORTED_OS;
    
        public enum SupportedOS {
            UNSUPPORTED_OS,
            MAC_OS,
            WINDOWS,
        }
    
        public AlarmManager() {
            final String osName = System.getProperty("os.name");
            if (osName == null) {
                L.e("Unable to retrieve OS!");
            } else if ("Mac OS X".equals(osName)) {
                currentOS = SupportedOS.MAC_OS;
            } else if (osName.contains("Windows")) {
                currentOS = SupportedOS.WINDOWS;
            } else {
                L.e("Unsupported OS: "+osName);
            }
        }
    
        /**
         * Windows only: name of the scheduled task
         */
        private String getAlarmName(final long alarmId) {
            return new StringBuilder("My_Alarm_").append(alarmId).toString();
        }
    
        /**
         * Gets the command line to trigger an alarm
         * @param alarmId
         * @return
         */
        private String getAlarmCommandLine(final long alarmId) {
            return new StringBuilder("javaws -open ").append(ALARM_CLI_FORMAT).append(alarmId).append(" ").append(G.JNLP_URL).toString();
        }
    
        /**
         * Adds an alarm to the system list of scheduled tasks
         * @param when
         */
        public void createAlarm(final Calendar when) {
            // Create alarm
            // ... stuff here
            final long alarmId = 42;
    
            // Schedule alarm
            String[] commandLine;
            Process child;
            final String alarmCL = getAlarmCommandLine(alarmId);
            try {
                switch (currentOS) {
                case MAC_OS:
                    final String cron = new SimpleDateFormat("mm HH d M '*' ").format(when.getTime()) + alarmCL;
    
                    commandLine = new String[] {
                            "/bin/sh", "-c",
                            "crontab -l | (cat; echo \"" + cron + "\") | crontab"
                    };
                    child = Runtime.getRuntime().exec(commandLine);
                    break;
    
                case WINDOWS:
                    commandLine = new String[] {
                            "schtasks",
                            "/Create",
                            "/ST "+when.get(Calendar.HOUR_OF_DAY) + ":" + when.get(Calendar.MINUTE),
                            "/SC ONCE",
                            "/SD "+new SimpleDateFormat("dd/MM/yyyy").format(when.getTime()), // careful with locale here! dd/MM/yyyy or MM/dd/yyyy? I'm French! :)
                            "/TR \""+alarmCL+"\"",
                            "/TN \""+getAlarmName(alarmId)+"\"",
                            "/F",
                    };
                    L.d("create command: "+Util.join(commandLine, " "));
                    child = Runtime.getRuntime().exec(commandLine);
                    break;
                }
            } catch (final IOException e) {
                L.e("Unable to schedule alarm #"+alarmId, e);
                return;
            }
    
            L.i("Created alarm #"+alarmId);
        }
    
        /**
         * Removes an alarm from the system list of scheduled tasks
         * @param alarmId
         */
        public void removeAlarm(final long alarmId) {
            L.i("Removing alarm #"+alarmId);
            String[] commandLine;
            Process child;
            try {
                switch (currentOS) {
                case MAC_OS:
                    commandLine = new String[] {
                            "/bin/sh", "-c",
                            "crontab -l | (grep -v \""+ALARM_CLI_FORMAT+"\") | crontab"
                    };
                    child = Runtime.getRuntime().exec(commandLine);
                    break;
    
                case WINDOWS:
                    commandLine = new String[] {
                            "schtasks",
                            "/Delete",
                            "/TN \""+getAlarmName(alarmId)+"\"",
                            "/F",
                    };
                    child = Runtime.getRuntime().exec(commandLine);
                    break;
                }
            } catch (final IOException e) {
                L.e("Unable to remove alarm #"+alarmId, e);
            }
        }
    
        public void triggerAlarm(final long alarmId) {
            // Do stuff
            //...
            L.i("Hi! I'm alarm #"+alarmId);
    
            // Remove alarm
            removeAlarm(alarmId);
        }
    }
    

    Usage is simple. Schedule a new alarm using:

    final AlarmManager m = new AlarmManager();
    final Calendar cal = new GregorianCalendar();
    cal.add(Calendar.MINUTE, 1);
    m.createAlarm(cal);
    

    Trigger an alarm like this:

    public static void main(final String[] args) {
        if (args.length >= 2 && args[1] != null && args[1].contains(AlarmManager.ALARM_CLI_FORMAT)) {
            try {
                final long alarmId = Long.parseLong(args[1].replace(AlarmManager.ALARM_CLI_FORMAT, ""));
                final AlarmManager m = new AlarmManager();
                m.triggerAlarm(alarmId);
            } catch (final NumberFormatException e) {
                L.e("Unable to parse alarm !", e);
            }
        }
    }
    

    Tested on Mac OS X.6 and Windows Vista. The class L is an helper to System.out.println and G holds my global constants (here, my JNLP file on my server used to launch my application).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.