Using JNA to call Winspool.drv from a Java program installed as a windows service

373 views
Skip to first unread message

Scott M

unread,
Jun 12, 2014, 3:56:59 PM6/12/14
to jna-...@googlegroups.com
Hello,
 
I apologize in advance for asking what is likely a very simple question but I'm hoping that you will go easy on my since I am very new to JNA and windows
 
I've created a Java program using JNA examples that I've found to read print job information from a windows printer spool and it works just fine when running it on my Win2k8 R2 server in a command prompt with access to the system environment. When running the java comand I just added the jna and platform jarfiles to the classpath and it runs just fine.
 
When installing the java program as a windows service, however, the service appears to start but nothing is being written out to my logs. I suspect it is because in running as a service it does not have access to the environment that is set up when running in a command prompt and it can't find Winspool.drv.
 
For the service AppParameters I've tried adding C:\Windows\System32 to the java.library.path and jna.library.path but neither has seemed to help.
 
Am I missing other dependencies for running my program as a service if I want to make calls to Winspool.drv?
 
Is there an issue trying to make such api calls from a windows service?
 
Thanks in advance for any help that you can give in pointing me towards a solution.
 
 

Daniel Doubrovkine

unread,
Jun 12, 2014, 5:35:24 PM6/12/14
to jna-...@googlegroups.com
You really want to find out what actually fails in the service. Does the java part run at all and hits your code? Is an actual API call failing? 

If you do have logging, make sure that the service runs as a user who has access to whatever log file you're trying to create/write to.


--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

dB. | Moscow - Geneva - Seattle - New York
code.dblock.org - @dblockdotorg - artsy.net - github/dblock

Scott M

unread,
Jun 13, 2014, 9:47:48 AM6/13/14
to jna-...@googlegroups.com
Thanks for taking the time to reply back. I feel that this is likely going to be a very simple issue which I am missing because of my ignorance of Windows Services and JNA. Hopefully it ends up being something very simple to fix which is just escaping me at the moment.
 
To give some more detail, I am able to run my java program successfully using the following command lines
 
CD \LMPOutputManager\bin
 
"D:\Program Files (x86)\Java\jre7\bin\java.exe" -Djava.library.path=D:\LMPOutputManager\lib;D:\oracle\product\11.2.0.3\cli_2\bin -Xms256m -Xmx1024m -cp .;./RPOutputManager.jar;./LMPOutputManager.jar;./ojdbc6.jar;./registry.jar;./sapjco.jar;./jna-4.1.0.jar;./jna-platform-4.1.0.jar lmp.remoteprinting.LMPPrintingOutputManager
------------------------------------------------------------------------------------------------------------------------------------------------
 
When I've installed the program as a service using srvany, I've added the following service parameters
 
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LMPPrintingOutputMgr\Parameters]

"Application"="D:\\Program Files (x86)\\Java\\jre7\\bin\\java.exe"
"AppParameters"="-Djava.library.path=d:\\LMPOutputManager\\lib;d:\\oracle\\product\\11.2.0.3\\cli_2\\bin;c:\\Windows\\system32 -Xms256m -Xmx1024m -cp .;./RPOutputManager.jar;./LMPOutputManager.jar;./ojdbc6.jar;./registry.jar;./sapjco.jar;./jna-4.1.0.jar;./jna-platform-4.1.0.jar lmp.remoteprinting.LMPPrintingOutputManager"
"AppDirectory"="D:\\LMPOutputManager\\bin"
------------------------------------------------------------------------------------------------------------------------------------------------
 
The java program runs just fine when running from a command prompt but it doesn't seem to even start as a service. I feel that I am missing some dependency that the windows environment is giving me when running in a command prompt but is not available when running as a service.
 
I've tried running the service as both the local system account and my own account and it doesn't run under either. I've tried starting the service using the windbg utility and get the following screenshot (see attachment) but it doesn't seem to let me attach completely to the process.
 
As a test, I took another service that I have running using srvany which is also a java process which does not use JNA and just modified its classpath to add the JNA jar files and just doing that seems to stop that service from running as well.
 
I am sure that I am missing something fairly simple.
RemotePrinting_Service_Error.jpg

Timothy Wall

unread,
Jun 14, 2014, 8:38:12 AM6/14/14
to jna-...@googlegroups.com
One common difference between command-line and service mode is that normally a service does not have desktop access. You might try granting the service desktop access and see if that changes the behavior.
> <RemotePrinting_Service_Error.jpg>

Kustaa Nyholm

unread,
Jun 14, 2014, 11:01:47 AM6/14/14
to jna-...@googlegroups.com

Have you (OP) tried a very simple single class main() only test ... my
incrementally including stuff I would think you should easily bisect when
things go south.

br Kusti




This e-mail may contain confidential or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. We will not be liable for direct, indirect, special or consequential damages arising from alteration of the contents of this message by a third party or as a result of any virus being passed on or as of transmission of this e-mail in general.

Daniel Doubrovkine

unread,
Jun 15, 2014, 5:11:54 PM6/15/14
to jna-...@googlegroups.com
Is this program an actual service? A Windows service has all kinds of other entry points other than 'main', you can't just take any command line thing and make it into a Windows service. 

JNA has a sample in https://github.com/twall/jna/tree/master/contrib/ntservice, but I have not run it in eons. 


--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Scott M

unread,
Jun 23, 2014, 9:18:11 AM6/23/14
to jna-...@googlegroups.com
Sorry for not coming back and updating this thread sooner and thank you for pointing me to the example Service code.
 
The Java code that I am charged with maintaining isn't/wasn't really a Windows Service. It is just POJOs wrapped as a service using srvany.
 
When I added code to be able to start as a service without using the srvany wrapper, I was able to successfully run the program including the new JNA calls that I wanted to use.
 
Thanks all for taking the time to respond to my thread and help.
Reply all
Reply to author
Forward
0 new messages