Thursday, January 28, 2021

Java libgrowl Revisit

Java libgrowl Revisit



This time I am going to write my own Java program with libgrowl GrowConnect class, just take it as an exercise.

First you need to get a copy of the libgrowl or you can compile on your own. There are at least 2 sources for the program:

classic https://github.com/feilimb/libgrowl
maven https://github.com/sbower/libgrowl

Write a GrowlNotify program to accept message, tittle, host, application name, notification name as parameters. Minimal to provide message as parameters, others are optional.
eg. $ GrowlNotify 'Hello, world.'

import net.sf.libgrowl.GrowlConnector;
import net.sf.libgrowl.Application;
import net.sf.libgrowl.Notification;
import net.sf.libgrowl.NotificationType;

class GrowlNotify {
	public static void main(String[] args) {
		if (args.length<1) {
			System.out.println("Usage: TestGrowlNotify <message> [<title> <host> <application> <notification>]");
			System.out.println("eg. TestGrowlNotify 'Hello, world.'");
			System.exit(0);
			}

    int counter= 0;
    int i;
    String message="", title="", host= "localhost", appName="libgrowl", notificationName="libgrowl";
    GrowlConnector growl;
    Application application;
    Notification notification;
    NotificationType notificationType;
    NotificationType[] notificationTypes;

    for (i=0; i<args.length; i++) {
      switch(counter++) {
      case 0:
        message= args[0]; break;
      case 1:
        title= args[1]; break;
      case 2:
        host= args[2]; break;
      case 3:
        appName= args[3]; break;
      case 4:
        notificationName= args[4]; break;
      default:
        break;
      }
    }

    application = new Application(appName);
    notificationType = new NotificationType(notificationName);
    notificationTypes = new NotificationType[] { notificationType };

    growl= new GrowlConnector(host);
    growl.register(application, notificationTypes);
    notification = new Notification(application,
        notificationType, title, message);
    growl.notify(notification);

    }
}



$ java -cp libgrowl.jar GrowlNotify.java
⇒ GrowlNotify.class
$ java -cp libgrowl.jar GrowlNotify 'Hello, world.'


If Growl software is installed and running, it shows "Hello, world.".

Just sample

Maven


I am using maven 3.6. Add dependency in pom.xml file:


<dependencies>
...
<dependency>
<groupId>net.sf</groupId>
<artifactId>libgrowl</artifactId>
<version>0.5</version>
</dependency>
<dependencies>


Copy libgrowl.jar to ~/.m2/repository/net/sf/libgrowl/0.5/.


$ cp libgrowl.jar ~/.m2/repository/net/sf/libgrowl/0.5/.
$ mvn compile
$ mvn exec:java -Dexec.mainClass="com.example.GrowlNotify" -Dexec.args="'Hello, world.'"

No comments:

Post a Comment