Generate Keyboard Output in Java Program running as Windows Service - java

I created a simple standalone Java application (JAR) that connects to a Honeywell barcode scanner over a virtual COM port (using RxTx libraries for setting up the COM connection).
After connecting, the program then receives the input from the scanner, transforms it using some custom logic and finally redirects the transformed output to the standard out using the Robot class. Nothing really complex.
So, if I run the program and then scan an value "A", the app transforms it into "B" and when notepad is active (or some other input field/program), "B" is outputted as if it was originally scanned by the scanner.
This program runs perfectly when run as a jar file (or wrapping bat file). However, when wrapping this JAR/BAT file as a windows service (using JSW Community Edition or YAJSW) the program will function perfectly, but will not output the "mimicked" keystrokes performed by the Robot.class.
I even used JNA libraries with the sendInput() method (as replacement for the Robot class) to create the keystrokes as close to the OS level as possbile, but this didn't work also... I also tried to make the service interactive but also this didnt work. Currently i am running out of options.
So my question is: is it correct that you cannot generate keyboard press events when running as a Windows Service? I could imagine that this could be a big security problem and maybe therefore it is disallowed.
Any feedback or possible solution would be very welcome! Many Thanks in advance!

It is a specification called session 0 isolation, which was introduced from Windows Vista.
Programs that act as WindowsService do not have a user interface.
Please refer to these articles and documents.
Application Compatibility – Session 0 Isolation
Session 0 Isolation - Microsoft Download Center
What is Session 0 Isolation? What do I need to know about it?
Session 0 Isolation and Secure Desktop: Windows 7 AppCompat Series
In Addition
Please try registering to Task Scheduler.
Register task in windows task scheduler in java
In that case, it may be ineffective unless it is "Run only when user is logged on".
This is an article on Windows Server.
show window of task scheduler schedule program.
However, there is a possibility that it will not work even if you do it.
Simple c++ program fails to run as a scheduled-task (interactive/non-interactive issue?)

Related

Is it possible to interact with a Linux Command line interface (CLI) through a Graphical user interface (GUI)?

I am quite a beginner, any advice is much appreciated.
I have a linux application OpenBTS used to simulate and run a GSM network on a software defined radio device e.g USRP.
I want to build an application that interfaces with the OpenBTS command line on Linux. I want to give the user an easier way to configure and to display the current configurations of the application. The user would have an interface in which he could play with the configurations without the need to use the terminal.
I don't know if this is possible ?
Is it possible to interact with a Linux CLI through a GUI ? If yes what is the most efficient programming language, coding technique or approach to do that ?
Thanks a lot
You could generate a config file using your GUI and then use standard in to get the configuration into OpenBts. When launching OpenBts with the configuration file config.txt you could simply run it as follows.
./OpenBts < config.txt
You could also do this from you GUI by launching OpenBts in a process from your GUI application in a similar fashion, however this requires a fork() and exec()
In C++, you can take inputs from user through GUI then run appropriate openBTS commands using system()/popen(). Based on the return values of system()/popen(), can provide status of the operation back to the user.

JAVA: Opening an application when the user starts their computer

I have an application which has a purpose to run when the user first runs their computer.
However, I'd like to make a check box on the app that enables/disables the application loading when the system is started. Is there any way to do this?
Thanks
I recommend you to create a service under windows. My suggestion is http://winrun4j.sourceforge.net/
This question has been discussed in SO time ago:
Code for Auto starting a java application on windows startup
Auto startup for java desktop application?
However, maybe the easier solution is to create a batch file,like suggested in the first link, to run your application as the user logs in.
If you want to enable/disable the startup through a checkbox, the first and simplest solution that comes to my mind is this: you can make a method, invoked by the checkbox listener, that edits the batch file and enables/disables the line used to run the application, in the batch file.
If you have Windows 7, read this tutorial.

Using a Java program I made on a Web Server

I created a small application that, when run, creates or updates some tables in a database by extracting data from some PDF files. Everything works fine in this desktop application, but the next step for me would be to make it possible for an administrator on a website to upload a PDF file and my Java program would then run and update the tables accordingly.
The problem is I have no idea where to start with this (the site isn't done yet, but I'm running some tests and it is going to be coded in PHP). I'd like to know what kind of technologies I need to let the server run the program and update everything as it would in the offline version. Sometimes it takes a while to update everything, so ideally, the user uploading the PDF could continue browsing other pages while the server does its job. (I'll probably implement something that when the server is done processing the file, it says if the program ended successfully or not in a log file)
Can someone tell me what terms to search for on Google or give me some pointers? I haven't chosen where my website is going to be hosted either, so if someone could tell me what to look for to know if they support running applications like this, I'd really appreciate it as well!
This could also apply to other programming languages as I know a bit of Python and C++ as well, so in the future I might have some applications in those languages I'll want to use on the web.
If I'm not approaching this the right way, I'm open to other suggestions, but the best solution would be to keep my Java program intact as I know it works exactly like I want it to and I'd rather not have to start it all over again.
If your host is *NIX based you can use crontab (Automatic Task Scheduler) to run your program at set intervals. Make it check if a "new" PDF exists, and run the program if there is. There may be a way to use Windows Task Scheduler type programs to do it on Windows. This is probably the easiest way.
Alternately you can use You can use shell_exec() in your php to execute a command on your *NIX system directly to run your java program.

'Secret sauce' in MacOS X private APIs for Java - request for further detail

On his blog Scott Kovatch writes:
Without getting into too much detail, typing ‘java MyAWTCode’ from a Terminal window violates a whole lot of assumptions about what an application is on Mac OS X, and needs a lot of cooperation between the AWT and the Process Manager to sort it out.
http://skovatch.wordpress.com/2011/01/03/secret-smoke-screens/
Out of curiosity - what assumptions are violated? Surely this is just a candidate for an API call with callbacks?
Sure, I can elaborate on that a bit.
The Process Manager starts with the assumption that all applications that present a UI on Mac OS X are bundled, are packaged in a folder named Application.app, binary in Contents/MacOS/Application, and most importantly, have an Info.plist to get things like the name of the application that will be shown in the application menu. When you run a Java application from the command line (Swing or SWT) there is no Info.plist, so we had to create a CFDictionary to be passed off to a private SPI that would register the application, give it a proper name in the Dock -- as opposed to just 'java' -- and could be force-quit.
Even then, it's not perfect, because the Dock also assumes it can store an alias to the bundled application when you right-click and choose Keep In Dock, but since there isn't one that fails silently. There's no way to store a shortcut or command line to start the application like Windows can.
The SWT just calls TransformProcessType, which is a start but is nowhere near sufficient to turn a Java application into a full UI application. For doing pure SWT testing and development it's enough to get you going. When you create an Eclipse RCP-based application for deployment you end up with a bundled application with the Eclipse launcher, and plugins and features, and you're ready to go.
Of course, if you go the extra mile and package your Java application into a bundle, this is all moot, but the vast majority of developers coming from other platforms don't bother and just want to run an executable JAR file or even a folder of class files with a shell script.
I am not sure what he had in mind, but I guess a big difference is the file structure: a normal MacOS X application is a bundle with the structure NameOfTheApp.app/Contents/MacOS/NameOfTheApp , and specific files in the Contents directory. When we use the terminal with a "java" command, the JVM has to create a "virtual" application specific to the Java code, and handle all the MacOS events for it. Also, when you open an application twice in the Finder, it simply activates the application the second time, while you need to launch separate applications every time you use "java MyAWTCode".

How to hide svchost.exe (dos prompt) when executing a scheduled task in Windows

I created a java program that will pop up a dialog, and scheduled it as a task. Everything works fine except when the scheduled java program runs and the dialog pops up, there is another window (svchost.exe) hanging behind the dialog box and doesn't go anywhere until the java program finishes its execution. The program is running in Windows XP.
How can I avoid that dos prompt?
Thanks
One word of warning (posting as an answer so I can get fancy links and for length): Services which pop up UI are basically security holes waiting to be exploited (search for "shatter attack" for more information). That's why in Windows Vista and beyond services cannot display UI on the desktop (services run in session 0, the interactive user runs in session 1).
More importantly, there are several scenarios in Windows XP where your application will fail to work: If there are multiple users on the computer logged on at once (fast user switching) or if the machine is a server 2003 machine running with the terminal server role your UI won't pop up in the interactive user's session.
This article talks about the session 0 isolation issue and how to work around it.
If you want your Java program to have no console window, you need to launch Java using javaw, not java.
Create a shortcut for the thing you want to schedule. In the shortcut's properties dialog, Select Run Minimized in the shortcut tab. When scheduling this shortcut, make sure you refer to the shortcut, it ends in .lnk Browsing for it may bypass the shortcut for what the shortcut points to. Source: http://ask.metafilter.com/18994/Windows-Batch-File-Run-Minimized

Categories