2

I'm trying to write a basic Scala application that can run as a daemon using commons-daemon. The following code was based on the java program in this post

package daemon

import org.apache.commons.daemon._ 
import java.util.{ Timer, TimerTask, Date }

class EchoTask extends TimerTask{
  def run() { println(new Date() + " running ...") }
}

object DaemonApp extends App with Daemon {
  val timer = new Timer();
  timer.schedule(new EchoTask(), 0, 1000); 

  def init(daemonContext: DaemonContext) {} 
  def start(){} 
  def stop(){} 
  def destroy(){}
}

this works fine when I run from sbt or build a jar. I can't get the code to run from jsvc though. for example if I run:

$ ./jsvc -cp $DAEMON_HOME/daemon.jar -pidfile $DAEMON_HOME/pidfile -errfile '&2' -outfile '&1' daemon.DaemonApp 

I'll get the following on stderr

java.lang.NoSuchMethodException: daemon.DaemonApp.init([Ljava.lang.String;)
    at java.lang.Class.getMethod(Class.java:1670)
    at org.apache.commons.daemon.support.DaemonLoader.load(DaemonLoader.java:176)
Cannot load daemon
Service exit with a return value of 3

I don't understand that whats going on with the method it's looking for, the init(Array[String]). Is there something in the way scalac is compiling the main class that is making the code incompatible with the daemon interface?

Thanks!

Community
  • 1
  • 1
awfulHack
  • 857
  • 1
  • 10
  • 25
  • Does your jar contain all your libraries? If you're running a scala app through `java` or something like it, you may need to add the scala standard library to the classpath. – lmm Feb 14 '15 at 19:22
  • I'm packaging it with sbt-assembly, the scala library is in the jar. I did say in the post that it runs fine when I run the jar from java, the problem is when I run from jsvc. – awfulHack Feb 14 '15 at 19:34
  • Have you found a solution? I'm facing the same problem (but using maven with assembly plugin) – ppopoff Mar 02 '15 at 14:27

2 Answers2

1

The daemon should be implemented as a class in Scala, not an object. I ran into this when I coded the daemon in Scala as an object to make it easy to run in the IDE.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
0

I faced the same problem. And looks like the solution is implementing this class in java. After that I've got things working.

ppopoff
  • 658
  • 7
  • 17