checking the state of a window service through Java - java

I am looking for a way to check the state of a windows service through Java. From some basic search through Google and here it sounds like Java has no api to query the Windows Services.
On the Windows command prompt running: sc \some_host_name query "serviceName"
gets me the info i want. Now i want to be able to run that in a Java program and be able to parse the output.
Any one know of a way to run a Windows command through Java?

Sounds like you need the Java 5+ ProcessBuilder.
A quick example (based the above documentation)
To start a process running:
Process p = new ProcessBuilder("sc", "\\some_host_name", "query", "serviceName").start();
The Process class provides methods to get the output (and error) stream from the process - it's standard stream handling from there.
The pre-Java 5 way of doing this was Runtime.exec(). I haven't actually used ProcessBuilder on Windows myself, drop a comment if you have problems?

Related

Git Fast Import - Sending the SIGUSR1 command

I am writing a Java program that feeds data into the git fast-import function. Currently, I know all my formatting works and conforms to https://git-scm.com/docs/git-fast-import#_signals However, my problem is sending the final "kill" command: SIGUSR1.
I get things to work right now by running this command type fast-import.script | git fast-import. That fact that this works tells me my formatting and such is proper. But, I know there is a way to continuously run git fast-import and feed commands into its standard-input, but the very last command has to be SIGUSR1
Without sending a SIGUSR1 the fast-import utility doesn't know to end. I have tried a few things:
1- Destroying the process using regular Java Process API commands such as
fastImportProcess.destroyForcibly();
2- Use a Java library WinProcess (Maven) to find the pid and kill it.
WinProcess wp = new WinProcess(fastImportProcess);
wp.killRecursively();
That didn't work either. And I tried combinations of things related to the Java Process API and the WinProcess API, etc. Although I can get WinProcess to force the fast-import utility to shut down, it doesn't actually create my commits.
3- Lastly, endlessly research what SIGUSR1 is and all I can come up with is SIGUSR is some kind of user-defined signal. C- what exactly is SIGUSR1 syntactically Not too helpful.
I don't think this issue is Java related per-se, its really about how to use git fast-import. I notice when I run fast-import in the CLI, when I press ctrl-c the commits execute. But I don't know exactly how to do this in Java.

JavaFX show looping Python print output

I built a GUI in JavaFX with FXML for running a bunch of different Python scripts. The Python scripts continuously collect data from a device and print it to the console as it's collected in a loop at anywhere from around 10 to 70 Hz depending on which script was being run, and they don't stop on their own.
I want the end-user to be able to click a button on my GUI which launches the scripts and lets them see the output. Currently, the best I have done was using Runtime.exec() with the command "cmd /c start cmd /k python some_script.py" which opens the windows command prompt, runs python some_script.py in it, and keeps the command prompt open so that you can see the output. The problem with this is that it only works on Windows (my OS) but I need to have universal OS support and that it relies on Java starting an external program which I hear is not very elegant.
I then tried to remedy this by executing the python some_script.py command in Java, capturing the process output with BufferedReader, creating a new JavaFX scene with just a TextArea in an AnchorPane to be a psuedo-Java-console and then calling .setText() on that TextArea to put the script output in it.
This kinda worked, but I ran into many problems in that the writing to the JavaFX console would jump in big chunks of several dozens of lines instead of writing to it line by line as the Python code was making Print() calls. Also, I got a bunch of NullPointerException and ArrayIndexOutOfBoundsException somewhat randomly in that Java would write a couple of hundred lines correctly but then throw those errors and freeze the program. I'm pretty sure both of these issues were due to having so much data at such high data rates which overflowed the BufferedReader buffer and/or the TextArea.setText() cache or something similar.
What I want to know is what approach I should take at this. I cannot migrate the Python code to Java since it relies on someone else's Python library to collect its data. Should I try to keep with the pseudo-Java-console idea and see if I can make that work? Should I go back to opening a command prompt window from Java and running the Python scripts and then add support for doing the same with Terminal in Mac and Linux? Is there a better approach I haven't thought of? Is the idea of having Java code call Python code and handle its output just disgusting and a horrible idea?
Please let me know if you would like to see any code (there is quite a lot) or if I can clarify anything, and I will try my best to respond quickly. Thank you!
My solution was to still call the Python code from the Java Processbuilder, but use the -u option like python -u scriptname.py to specify unbuffered Python output.

how to start/stop an application pool in IIS 6.0 from Java remotely

Can I start and stop IIS 6.0 application pools on one machine from within Java code running on another machine? If so, how?
Use Java's facility to execute another program and use the command line tool such as
appcmd stop apppool /apppool.nam
appcmd start apppool /apppool.nam
(see http://technet.microsoft.com/en-us/library/cc732742(WS.10).aspx)
I believe you can do it using WMI. Please take a look http://msdn.microsoft.com/en-us/library/ms525309(v=vs.90).aspx
and here: http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/9041b0a5-c314-46d9-8f56-01506687f357.mspx?mfr=true
Or just google "iis wmi".
First find API you need and find a way to write JScript or VBScript that does what you need.
Then you have 2 ways.
Just execute script you have just done from java using Runtime.exec()
Use one of interoparability packages like JaWin, JIntegra or JInterop (or, probably others) to call the same code from java.

Send and recieve multiple ssh commands via java runtime and cygwin

Hey I have run into the following problem when attempting to build a program in java which executes commands on a remote linux server and returns the output for processing...
Basically I have installed Cygwin with an SSH client and want to do the following:
Open Cygwin,
Send command "user#ip";
Return output;
Send command "password";
Return output;
Send multiple other commands,
Return output;
...etc...
So far:
Process proc = Runtime.getRuntime().exec("C:/Power Apps/Cygwin/Cygwin.bat");
Works nicely except I am at a loss as to how to attempt the next steps.
Any help?
The quick way: Don't go through cygwin. Pass your login info and commands as arguments to ssh.
A better way: Install and use the open source and very mature Sun Grid Engine and use its DRMAA binding for Java to exec your commands. You might also consider switching to a scripting language (yours is a very script like task). If you do DRMAA has Perl, Ruby and other bindings as well.
You could also use Plink:
Download here
There is a good set of instructions link here
You can use a command like:
plink root#myserver -pw passw /etc/backups/do-backup.sh
Use a ssh implementation in java. I used Ganymede a couple of years ago, there are perhaps better alternatives now. (?)
Using Ganymede, you will get an input stream to read from, and an output stream to write to.
You can create a LineInputReader on the input stream and use that to read Strings representing the output from the remote server. Then use a regexp Pattern/Matcher to parse responses.
Create a PrintWriter on the output stream and use println() to send your commands.
Its simple and actually quite powerful (if you know regexp... It might require some trial and error to get it right...)

How can I set the process name for a Java-program? [duplicate]

This question already has answers here:
how to change the name of a Java application process?
(10 answers)
Closed 8 years ago.
If a Java program is started, it get's in the system process-monitor the name java. Many Java-programs are that way hard to distinguish. So it would be nice, if a way exists, to set the name, that will be shown in the process-monitor. I'm aware that this may work different on different Operating Systems.
A simple way would be, if the java-interpreter would support a switch to set the name, like this:
java -processname MyProgram -jar MyProgram
But I couldn't find such a switch, so it is probably non-existant. An API in Java to set the process-name would be also fine.
So, so you have any suggestions?
I don't know if this is possible, but you could use a command line tool that comes with the JDK called 'jps'. It's like *nix ps, but just Java programs instead. jps -v shows all the arguments you have passed to java.
Also, I have seen people attach a "process name" to their java processes by adding an unused -Dmyprocessname to the args.
as #omerkudat said:
jps -v
prints out all java processes {processID, params list}
If the params list is not enough to recognize the applications you need,
try adding some dummy params when running them:
java -Dname=myApp -cp myApp.jar some.client.main.MainFrame
This will print like:
7780 MainFrame -Dname=myApp
and you can use the process ID to kill / monitor it.
You can do this with an LD_PRELOAD shim: https://github.com/airlift/procname
The shim simply calls the Linux-specific prctl() when the process starts:
static void __attribute__ ((constructor)) procname_init()
{
prctl(PR_SET_NAME, "myname");
}
The call has to happen on the main thread, so it isn't possible to do this from Java or even with a JVMTI agent, since those happen on a different thread.
When I first read this, the idea of changing the process name struck me as impossible. However, according to this ancient thread on the sun forum you can use C++ wrappers around the JVM executable to achieve this.
Though frankly, I wonder what your real problem is, as I'd guess there is a more standard solution then attempting to change the process name.
Your best option is something like launch4j
http://launch4j.sourceforge.net/
There is a bug logged in the sun bugtracker for this, but it's not high priority
http://bugs.sun.com/view_bug.do?bug_id=6299778
There are mainly 2 approaches: one is as already described: using tools like Launch4j, WinRun4J to create native Windows launchers.
Another approach that seems better is to use Apache Procrun to wrap the java application as a Windows service. During the install service process, we can give the process an meaningful name such as OurApp.exe.
All we need do is rename prunsrv.exe to OurApp.exe and replace every occurrence of prunsrv.exe in our install|start|stop|uninstall service scripts to MyApp.exe.
See more from Using Apache Procrun to Rename Process Name of a Java Program in Windows
If you want to use a different process name you'll have to create your own binary to launch your Java application using something like JSmooth.
Look at this question for a discussion of creating such binaries.
That's because Java applications aren't actually executable they're ran by the Java virtual machine which is why java appears in the process monitor, it's the host of your application.
Things like LimeWire however do but I think that's more down to GCJ - http://gcc.gnu.org/java/

Categories