1

I am trying to run a Java program as a Windows service and my log file shows this error. I guess there is an error in providing the path but I am not able to figure it out.

2017-06-27 15:43:21 Commons Daemon procrun stderr initialized
    java.lang.NoClassDefFoundError: ajavaservice/DemoService 
Caused by:
    java.lang.ClassNotFoundException: ajavaservice.DemoService  
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)

My batch file

set SERVICE_NAME=JavaService
set PR_INSTALL=C:\Eclipse\JavaService\prunsrv.exe

REM Service log configuration
set PR_LOGPREFIX=JavaService
set PR_LOGPATH=C:\Logs
set PR_STDOUTPUT=C:\Logs\stdout.txt
set PR_STDERROR=C:\Logs\stderr.txt
set PR_LOGLEVEL=Error

REM Path to java installation
set PR_JVM=C:\Program Files\Java\jre7\bin\client\jvm.dll
set PR_CLASSPATH=DemoService1.jar

REM Startup configuration
set PR_STARTUP=auto
set PR_STARTMODE=jvm
set PR_STARTCLASS=com.service.demoservice.DemoService
set PR_STARTMETHOD=start

REM Shutdown configuration
set PR_STOPMODE=jvm
set PR_STOPCLASS=com.service.demoservice.DemoService
set PR_STOPMETHOD=stop

REM JVM configuration
set PR_JVMMS=256
set PR_JVMMX=1024
set PR_JVMSS=4000
set PR_JVMOPTIONS=-Duser.language=DE;-Duser.region=de

REM Install service
prunsrv.exe //IS//JavaService

1 Answers1

1

According to the stacktrace, the prunsrv.exe is trying to load a Java class whose full name is ajavaservice.DemoService, but the classloader cannot find it.

Now, I can't see where your BAT file is telling prunsrv.exe to use that particular name. (Maybe you have "massaged" something? Maybe your com.service.demoservice.DemoService class refers to it in some way?)

Either way, the solution is to ensure that the missing file is actually on the classpath. For a start, check that the the JAR file you are using contains a an entry for "/ajavaservice/DemoService.class". (Use "jar -t ..." to check it!)


UPDATE

Based on the comments, that's what the problem turned out to be. The classpath was incorrect. It apparently used an incorrect pathname for a JAR, which caused an old version to be used.

For other people with problems like this: if you get a ClassNotFoundException check your JARs and your classpath carefully. You are missing something.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Actually, i had previously named my package ajavaservice and later changed it to com.service.demoservice.DemoService. The error still uses ajavaservice though. "FindClass ajavaservice/DemoService failed" – Prerna Achtani Jun 27 '17 at 12:13
  • Did you recompile? Did you create a new JAR? Did you deploy it to the place you are attempting to use it? And the BAT file? – Stephen C Jun 27 '17 at 12:42
  • yes i modified the old jar by overwriting it. Yes i placed the jar and the batch file in the respective folder. My service says "starting" and is hung there. I have increased the servicepipetime as well – Prerna Achtani Jun 29 '17 at 05:58
  • Something does not add up here. You say you changed the package name. You say that you updated everything. And yet ... something is still trying to use the old name for the package. It does not add up. – Stephen C Jun 29 '17 at 06:44
  • Yes this confused me as well so i made up a completely new project. It still gives the findclass error and the same java.lang.NoClassDefFoundError. What can be done to avoid this error? – Prerna Achtani Jun 29 '17 at 12:12
  • Well, obviously, the only logical explanation is that your Windows service is using old configurations. – Stephen C Jun 29 '17 at 12:16
  • It isn't cause of setting a wrong path somewhere? My current machine is 32-bit windows 7 enterprise version. – Prerna Achtani Jun 30 '17 at 05:26
  • That is one possible explanation. There could be others. I doubt that the version of Windows is relevant. – Stephen C Jun 30 '17 at 07:10
  • Okay i will look into that. If there is any problem in the bat file or anything plz let me know – Prerna Achtani Jun 30 '17 at 08:53
  • Well ... short of re-installing Windows or something equally ridiculous, I don't know what to suggest. Clearly, there is a problem in something, but it is not something that you have shown us. Hint: the computer does not pull names like "ajavaservice" out of thin air. – Stephen C Jul 04 '17 at 07:57
  • ajavaservice is my package name and DemoService is the class name. I have pasted my batch file. I only have a java code with start and stop methods for which im using this batch file – Prerna Achtani Jul 04 '17 at 09:27
  • You are contradicting yourself. Previously you said "Actually, i had previously named my package ajavaservice **and later changed it to com.service.demoservice.DemoService**". But here's what you should do. Make sure that your source code, the JAR file **that you are actually using**, and the BAT file that **you are actually using** are all **consistent** about the class and package names for the `DemoService` class. The exception is happening because **something** is inconsistent. – Stephen C Jul 04 '17 at 10:16
  • Exactly..That is why i created another completely new project. The same error occurs though. java.lang.NoClassDefFoundError. It is searching for the name of the project and not the class. – Prerna Achtani Jul 04 '17 at 12:02
  • I resolved it. There was an error in the classpath. I tried giving absolute path to the jar and it worked. Thanks for your help. :) – Prerna Achtani Jul 05 '17 at 06:03
  • As I said in my original answer .... *"Either way, the solution is to ensure that the missing file is actually on the classpath."* – Stephen C Nov 09 '18 at 13:37