Find if services are enabled in Java [duplicate] - java

Here is the situation:
I have been called upon to work with InstallAnywhere 8, a Java-based installer IDE, of sorts, that allows starting and stopping of windows services, but has no built-in method to query their states. Fortunately, it allows you to create custom actions in Java which can be called at any time during the installation process (by way of what I consider to be a rather convoluted API).
I just need something that will tell me if a specific service is started or stopped.
The IDE also allows calling batch scripts, so this is an option as well, although once the script is run, there is almost no way to verify that it succeeded, so I'm trying to avoid that.
Any suggestions or criticisms are welcome.

here's what I had to do. It's ugly, but it works beautifully.
String STATE_PREFIX = "STATE : ";
String s = runProcess("sc query \""+serviceName+"\"");
// check that the temp string contains the status prefix
int ix = s.indexOf(STATE_PREFIX);
if (ix >= 0) {
// compare status number to one of the states
String stateStr = s.substring(ix+STATE_PREFIX.length(), ix+STATE_PREFIX.length() + 1);
int state = Integer.parseInt(stateStr);
switch(state) {
case (1): // service stopped
break;
case (4): // service started
break;
}
}
runProcess is a private method that runs the given string as a command line process and returns the resulting output. As I said, ugly, but works. Hope this helps.

You can create a small VBS on-th-fly, launch it and capture its return code.
import java.io.File;
import java.io.FileWriter;
public class VBSUtils {
private VBSUtils() { }
public static boolean isServiceRunning(String serviceName) {
try {
File file = File.createTempFile("realhowto",".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set sh = CreateObject(\"Shell.Application\") \n"
+ "If sh.IsServiceRunning(\""+ serviceName +"\") Then \n"
+ " wscript.Quit(1) \n"
+ "End If \n"
+ "wscript.Quit(0) \n";
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec("wscript " + file.getPath());
p.waitFor();
return (p.exitValue() == 1);
}
catch(Exception e){
e.printStackTrace();
}
return false;
}
public static void main(String[] args){
//
// DEMO
//
String result = "";
msgBox("Check if service 'Themes' is running (should be yes)");
result = isServiceRunning("Themes") ? "" : " NOT ";
msgBox("service 'Themes' is " + result + " running ");
msgBox("Check if service 'foo' is running (should be no)");
result = isServiceRunning("foo") ? "" : " NOT ";
msgBox("service 'foo' is " + result + " running ");
}
public static void msgBox(String msg) {
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, msg, "VBSUtils", javax.swing.JOptionPane.DEFAULT_OPTION);
}
}

Based on the other answers I constructed the following code to check for Windows Service status:
public void checkService() {
String serviceName = "myService";
try {
Process process = new ProcessBuilder("C:\\Windows\\System32\\sc.exe", "query" , serviceName ).start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
String scOutput = "";
// Append the buffer lines into one string
while ((line = br.readLine()) != null) {
scOutput += line + "\n" ;
}
if (scOutput.contains("STATE")) {
if (scOutput.contains("RUNNING")) {
System.out.println("Service running");
} else {
System.out.println("Service stopped");
}
} else {
System.out.println("Unknown service");
}
} catch (IOException e) {
e.printStackTrace();
}
}

I have been dealing with installers for years and the trick is to create your own EXE and call it on setup. This offers good flexibility like displaying precise error messages in the event an error occurs, and have success-based return values so your installer knows about what happened.
Here's how to start, stop and query states for windows services (C++):
http://msdn.microsoft.com/en-us/library/ms684941(VS.85).aspx
(VB and C# offers similar functions)

I have had some luck in the past with the Java Service Wrapper. Depending upon your situation you may need to pay in order to use it. But it offers a clean solution that supports Java and could be used in the InstallAnywhere environment with (I think) little trouble. This will also allow you to support services on Unix boxes as well.
http://wrapper.tanukisoftware.org/doc/english/download.jsp

A shot in the dark but take a look at your Install Anywhere java documentation.
Specifically,
/javadoc/com/installshield/wizard/platform/win32/Win32Service.html
The class:
com.installshield.wizard.platform.win32
Interface Win32Service
All Superinterfaces:
Service
The method:
public NTServiceStatus queryNTServiceStatus(String name)
throws ServiceException
Calls the Win32 QueryServiceStatus to retrieve the status of the specified service. See the Win32 documentation for this API for more information.
Parameters:
name - The internal name of the service.
Throws:
ServiceException

Here's a straignt C# / P/Invoke solution.
/// <summary>
/// Returns true if the specified service is running, or false if it is not present or not running.
/// </summary>
/// <param name="serviceName">Name of the service to check.</param>
/// <returns>Returns true if the specified service is running, or false if it is not present or not running.</returns>
static bool IsServiceRunning(string serviceName)
{
bool rVal = false;
try
{
IntPtr smHandle = NativeMethods.OpenSCManager(null, null, NativeMethods.ServiceAccess.ENUMERATE_SERVICE);
if (smHandle != IntPtr.Zero)
{
IntPtr svHandle = NativeMethods.OpenService(smHandle, serviceName, NativeMethods.ServiceAccess.ENUMERATE_SERVICE);
if (svHandle != IntPtr.Zero)
{
NativeMethods.SERVICE_STATUS servStat = new NativeMethods.SERVICE_STATUS();
if (NativeMethods.QueryServiceStatus(svHandle, servStat))
{
rVal = servStat.dwCurrentState == NativeMethods.ServiceState.Running;
}
NativeMethods.CloseServiceHandle(svHandle);
}
NativeMethods.CloseServiceHandle(smHandle);
}
}
catch (System.Exception )
{
}
return rVal;
}
public static class NativeMethods
{
[DllImport("AdvApi32")]
public static extern IntPtr OpenSCManager(string machineName, string databaseName, ServiceAccess access);
[DllImport("AdvApi32")]
public static extern IntPtr OpenService(IntPtr serviceManagerHandle, string serviceName, ServiceAccess access);
[DllImport("AdvApi32")]
public static extern bool CloseServiceHandle(IntPtr serviceHandle);
[DllImport("AdvApi32")]
public static extern bool QueryServiceStatus(IntPtr serviceHandle, [Out] SERVICE_STATUS status);
[Flags]
public enum ServiceAccess : uint
{
ALL_ACCESS = 0xF003F,
CREATE_SERVICE = 0x2,
CONNECT = 0x1,
ENUMERATE_SERVICE = 0x4,
LOCK = 0x8,
MODIFY_BOOT_CONFIG = 0x20,
QUERY_LOCK_STATUS = 0x10,
GENERIC_READ = 0x80000000,
GENERIC_WRITE = 0x40000000,
GENERIC_EXECUTE = 0x20000000,
GENERIC_ALL = 0x10000000
}
public enum ServiceState
{
Stopped = 1,
StopPending = 3,
StartPending = 2,
Running = 4,
Paused = 7,
PausePending =6,
ContinuePending=5
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class SERVICE_STATUS
{
public int dwServiceType;
public ServiceState dwCurrentState;
public int dwControlsAccepted;
public int dwWin32ExitCode;
public int dwServiceSpecificExitCode;
public int dwCheckPoint;
public int dwWaitHint;
};
}

I improvised on the given solutions, to make it locale independent.
Comparing the string "RUNNING" would not work in systems with non-english locales as Alejandro González rightly pointed out.
I made use of sc interrogate and look for the status codes returned by it.
Mainly, the service can have 3 states:-
1 - Not available
[SC] OpenService FAILED 1060: The specified service does not exist as an installed service.
2 - Not running
([SC] ControlService FAILED 1062: The service has not been started)
3 - Running
TYPE : 10 WIN32_OWN_PROCESS
STATE : 2 START_PENDING
(NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x7d0
PID : 21100code here
So using them in following code, gives us the desired result :-
public static void checkBackgroundService(String serviceName) {
Process process;
try {
process = Runtime.getRuntime().exec("sc interrogate " + serviceName);
Scanner reader = new Scanner(process.getInputStream(), "UTF-8");
StringBuffer buffer = new StringBuffer();
while (reader.hasNextLine()) {
buffer.append(reader.nextLine());
}
System.out.println(buffer.toString());
if (buffer.toString().contains("1060:")) {
System.out.println("Specified Service does not exist");
} else if (buffer.toString().contains("1062:")) {
System.out.println("Specified Service is not started (not running)");
} else {
System.out.println("Specified Service is running");
}
} catch (IOException e) {
e.printStackTrace();
}
}

During startup, create a file with File.deleteOnExit().
Check for the existence of the file in your scripts.

Simply call this method to check the status of service whether running or not.
public boolean checkIfServiceRunning(String serviceName) {
Process process;
try {
process = Runtime.getRuntime().exec("sc query " + serviceName);
Scanner reader = new Scanner(process.getInputStream(), "UTF-8");
while(reader.hasNextLine()) {
if(reader.nextLine().contains("RUNNING")) {
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

Related

Reboot javafx App

I'm trying to make a function that reboot the javafx application, On windows 10 works perfectly but on Windows 7 it doesn't, I search for this solution and it was perfect, then I test it on Windows 10 and nothing, the app just turn off. Also I test it watching for an exception inside the log file and it doesn't throw any Exception.
Something specific must be made in order to work also on windows 7? maybe a different approach? Thanks.
this is the code:
//Restart app the current Java application, with parameter you can pass a Runnable to do before restart app, null if not
public static void restartApplication(Runnable runBeforeRestart) throws IOException {
try {
/**
* Sun property pointing the main class and its arguments.
* Might not be defined on non Hotspot VM implementations.
*/
final String SUN_JAVA_COMMAND = "sun.java.command";
// java binary
String java = System.getProperty("java.home") + "/bin/java";
// vm arguments
List<String> vmArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
StringBuffer vmArgsOneLine = new StringBuffer();
for (String arg : vmArguments) {
// if it's the agent argument : we ignore it otherwise the
// address of the old application and the new one will be in conflict
if (!arg.contains("-agentlib")) {
vmArgsOneLine.append(arg);
vmArgsOneLine.append(" ");
}
}
// init the command to execute, add the vm args
final StringBuffer cmd = new StringBuffer("\"" + java + "\" " + vmArgsOneLine);
// program main and program arguments
String[] mainCommand = System.getProperty(SUN_JAVA_COMMAND).split(" ");
// program main is a jar
if (mainCommand[0].endsWith(".jar")) {
// if it's a jar, add -jar mainJar
cmd.append("-jar " + new File(mainCommand[0]).getPath());
} else {
// else it's a .class, add the classpath and mainClass
cmd.append("-cp \"" + System.getProperty("java.class.path") + "\" " + mainCommand[0]);
}
// finally add program arguments
for (int i = 1; i < mainCommand.length; i++) {
cmd.append(" ");
cmd.append(mainCommand[i]);
}
// execute the command in a shutdown hook, to be sure that all the
// resources have been disposed before restarting the application
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
try {
Runtime.getRuntime().exec(cmd.toString());
} catch (IOException e) {
logToFile.log(e, "info", "The application fail to restart, applying the command");
}
}
});
// execute some custom code before restarting
if (runBeforeRestart!= null) {
runBeforeRestart.run();
}
// Wait for 2 seconds before restart
//TimeUnit.SECONDS.sleep(2);
// exit
System.exit(0);
} catch (Exception e) {
// something went wrong
logToFile.log(e, "info", "The application fail to restart generally");
}
}
Update: Searching for other approach I found out a solution, It's test it on both Windows OS and works
here it's the code:
//Restart app the current Java application, with parameter you can pass a Runnable to do before restart app, null if not
public static void restartApplication(Runnable runBeforeRestart, Integer TimeToWaitToExecuteTask) throws IOException {
try {
// execute some custom code before restarting
if (runBeforeRestart != null) {
// Wait for 2 seconds before restart if null
if (TimeToWaitToExecuteTask != null) {
TimeUnit.SECONDS.sleep(TimeToWaitToExecuteTask);
} else {
TimeUnit.SECONDS.sleep(2);
}
runBeforeRestart.run();
}
final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
final File currentJar = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI());
/* is it a jar file? */
if (!currentJar.getName().endsWith(".jar"))
return;
/* Build command: java -jar application.jar */
final ArrayList<String> command = new ArrayList<String>();
command.add(javaBin);
command.add("-jar");
command.add(currentJar.getPath());
final ProcessBuilder builder = new ProcessBuilder(command);
builder.start();
System.exit(0);
} catch (Exception e) {
// something went wrong
logToFile.log(e, "info", "The application fail to restart generally");
}
}

Java Telegram API RpcCall Timeout

I have a Problem regarding the Telegram API
Whenever I do a RpcCall, it always give me a TimeoutException except when I do a NonAuth Call.
I can SignIn with a Number already and I set Authenticated to true in my AbsApiState and still can only do NonAuth Calls
Here is my Code:
private void startApi() throws Exception
{
api = new TelegramApi(new MyApiStorage(Moin.config.getProp("useTest").equalsIgnoreCase("true") ? true : false),
new AppInfo(Moin.api_id, "console", "???", "???", "en"),
new ApiCallback()
{
#Override
public void onAuthCancelled(TelegramApi arg0)
{
System.out.println("AuthCancelled");
}
#Override
public void onUpdate(TLAbsUpdates update)
{
System.out.println("Updated | " + update.getClass());
}
#Override
public void onUpdatesInvalidated(TelegramApi arg0)
{
System.out.println("Updatefailed");
}
});
TLConfig config = null;
config = api.doRpcCallNonAuth(new TLRequestHelpGetConfig());
if(config != null)
api.getState().updateSettings(config);
else
throw new Exception("config is null, could not update DC List");
login();
}
private void login() throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
TLSentCode code = null;
String defaultNumber = Moin.config.getProp("phoneNumber");
System.out.println("Enter a Phone Number (Default ist " + defaultNumber + "):");
String number = reader.readLine();
if(number.equals(" "))
number = defaultNumber;
System.out.println("Sending to " + number + " ...");
try
{
code = api.doRpcCallNonAuth(new TLRequestAuthSendCode(number, 0, Moin.api_id, Moin.api_hash, "en"));
}
catch (RpcException e)
{
if (e.getErrorCode() == 303)
{
int destDC = 0;
if (e.getErrorTag().startsWith("NETWORK_MIGRATE_"))
{
destDC = Integer.parseInt(e.getErrorTag().substring("NETWORK_MIGRATE_".length()));
}
else if (e.getErrorTag().startsWith("PHONE_MIGRATE_"))
{
destDC = Integer.parseInt(e.getErrorTag().substring("PHONE_MIGRATE_".length()));
}
else if (e.getErrorTag().startsWith("USER_MIGRATE_"))
{
destDC = Integer.parseInt(e.getErrorTag().substring("USER_MIGRATE_".length()));
}
else
{
e.printStackTrace();
}
api.switchToDc(destDC);
code = api.doRpcCallNonAuth(new TLRequestAuthSendCode(number, 0, Moin.api_id, Moin.api_hash, "en"));
}
else
e.printStackTrace();
}
String hash = code.getPhoneCodeHash();
System.out.println("Please Enter the Code:");
String smsCode = reader.readLine();
TLAuthorization auth = api.doRpcCallNonAuth(new TLRequestAuthSignIn(number, hash, smsCode));
api.getState().setAuthenticated(api.getState().getPrimaryDc(), true);
//This is where I get the Error
TLExportedAuthorization test = api.doRpcCall(new TLRequestAuthExportAuthorization(api.getState().getPrimaryDc()));
System.out.println(test.getId());
FileOutputStream stream = new FileOutputStream(Paths.get("").toAbsolutePath().toString() + File.separator + "test.txt");
try
{
stream.write(test.getBytes().getData());
}
finally
{
stream.close();
}
TLState state = api.doRpcCall(new TLRequestUpdatesGetState());
System.out.println(state.getDate() + " | " + state.getPts() + " | " + state.getQts() + " | " + state.getUnreadCount());
TLAbsUser user = auth.getUser();
}
And here the Error:
TelegramApi#1001:Timeout Iteration
TelegramApi#1001:RPC #3: Timeout (14999 ms)
TelegramApi#1001:Timeout Iteration
org.telegram.api.engine.TimeoutException
at org.telegram.api.engine.TelegramApi.doRpcCall(TelegramApi.java:364)
at org.telegram.api.engine.TelegramApi.doRpcCall(TelegramApi.java:309)
at org.telegram.api.engine.TelegramApi.doRpcCall(TelegramApi.java:400)
at org.telegram.api.engine.TelegramApi.doRpcCall(TelegramApi.java:396)
at at.nonon.telegram.telegram.Telegram.login(Telegram.java:165)
at at.nonon.telegram.telegram.Telegram.startApi(Telegram.java:105)
at at.nonon.telegram.telegram.Telegram.<init>(Telegram.java:54)
at at.nonon.telegram.telegram.ApiManager.startNew(ApiManager.java:21)
at at.nonon.telegram.Moin.onApiStart(Moin.java:31)
I took alot of the Code from the telegram-bot (https://github.com/ex3ndr/telegram-bot) but even if I copy paste his Code, it still doesn't work...
Thanks in Advance
this problem happened to me, too. I assume you are on a debian or some other linux box. The problem is Oracle JDK's use of the linux random number generator.
In order to make it work, run your application like this:
java -Djava.security.egd=file:/dev/./urandom -jar foo.jar
... meaning, you have to specify the java.security.egd parameter.
Details can be found here: https://github.com/ex3ndr/telegram-api/issues/9#issuecomment-38175765
and here:
http://www.virtualzone.de/2011/10/javas-securerandomgenerateseed-on-linux.html
It started working for me after I changed the server IP address in MemoryApiState as shown below
public void start(boolean isTest) {
connections = new HashMap<>();
connections.put(1, new ConnectionInfo[]{
new ConnectionInfo(1, 0, isTest ? "149.154.175.10" : "149.154.175.50", 443),
});
}
Your timeout might happen for several reasons - please see my answer to TimeoutException on telegram java client

Java codes to run program from detected drive

I have a program that I want to run from a detected USB drive (removable storage such as a USB), and this was done by creating two classes: external.java and DetectDrive.java as follows:
external.java
public class external
{
public static void main(String args[]) throws InterruptedException
{
DetectDrive d = new DetectDrive();
String DetectDrive = d.USBDetect();
BufferedWriter fileOut;
String filePath = DetectDrive;
System.out.println(filePath);
try
{
fileOut = new BufferedWriter(new FileWriter("F:\\external.bat"));
fileOut.write("cd "+ filePath +"\n");
fileOut.write("external.exe"+"\n");
fileOut.close(); //close the output stream after all output is done
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cmd /c start" +DetectDrive+ "\\external.bat");
p.waitFor();
} catch (IOException e){
e.printStackTrace();
}
}
}
DetectDrive.java
import java.io.*;
import java.util.*;
import javax.swing.filechooser.FileSystemView;
public class DetectDrive
{
public String USBDetect()
{
String driveLetter = "";
FileSystemView fsv = FileSystemView.getFileSystemView();
File[] f = File.listRoots();
for (int i = 0; i < f.length; i++)
{
String drive = f[i].getPath();
String displayName = fsv.getSystemDisplayName(f[i]);
String type = fsv.getSystemTypeDescription(f[i]);
boolean isDrive = fsv.isDrive(f[i]);
boolean isFloppy = fsv.isFloppyDrive(f[i]);
boolean canRead = f[i].canRead();
boolean canWrite = f[i].canWrite();
if (canRead && canWrite && !isFloppy && isDrive && (type.toLowerCase().contains("removable") || type.toLowerCase().contains("rimovibile")))
{
//log.info("Detected PEN Drive: " + drive + " - "+ displayName);
driveLetter = drive;
break;
}
}
/*if (driveLetter.equals(""))
{
System.out.println("Not found!");
}
else
{
System.out.println(driveLetter);
}
*/
//System.out.println(driveLetter);
return driveLetter;
}
}
The problem now is that there are no errors when I run the external.java (main). However, the output only shows the detected drive which is F:\ but it doesn’t run the specified program which is external.exe and it also mentioned that the program got terminated. Can someone please help me point out where I went wrong and what the correct codes should be like? I am new to Java.
I got the DetectDrive codes from http://www.snip2code.com/Snippet/506/Detect-USB-removable-drive-in-Java which I believe is now in maintenance.
Is it possible to change the new FileWriter("F:\external.bat") to detect the USB drive directory instead? For example letting the program detect the usb drive and automatically put in the correct directory instead of us typing the F:\ manually. I have no answer for this yet. Please help!
It seems problem is with command :
"cmd /c start" +DetectDrive+ "\\external.bat"
It should have a {space} after start:
"cmd /c start " +DetectDrive+ "\\external.bat"

How to get the Desktop path in java

I think this will work only on an English language Windows installation:
System.getProperty("user.home") + "/Desktop";
How can I make this work for non English Windows?
I use a french version of Windows and with it the instruction:
System.getProperty("user.home") + "/Desktop";
works fine for me.
I think this is the same question... but I'm not sure!:
In java under Windows, how do I find a redirected Desktop folder?
Reading it I would expect that solution to return the user.home, but apparently not, and the link in the answer comments back that up. Haven't tried it myself.
I guess by using JFileChooser the solution will require a non-headless JVM, but you are probably running one of them.
This is for Windows only. Launch REG.EXE and capture its output :
import java.io.*;
public class WindowsUtils {
private static final String REGQUERY_UTIL = "reg query ";
private static final String REGSTR_TOKEN = "REG_SZ";
private static final String DESKTOP_FOLDER_CMD = REGQUERY_UTIL
+ "\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\"
+ "Explorer\\Shell Folders\" /v DESKTOP";
private WindowsUtils() {}
public static String getCurrentUserDesktopPath() {
try {
Process process = Runtime.getRuntime().exec(DESKTOP_FOLDER_CMD);
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
String result = reader.getResult();
int p = result.indexOf(REGSTR_TOKEN);
if (p == -1) return null;
return result.substring(p + REGSTR_TOKEN.length()).trim();
}
catch (Exception e) {
return null;
}
}
/**
* TEST
*/
public static void main(String[] args) {
System.out.println("Desktop directory : "
+ getCurrentUserDesktopPath());
}
static class StreamReader extends Thread {
private InputStream is;
private StringWriter sw;
StreamReader(InputStream is) {
this.is = is;
sw = new StringWriter();
}
public void run() {
try {
int c;
while ((c = is.read()) != -1)
sw.write(c);
}
catch (IOException e) { ; }
}
String getResult() {
return sw.toString();
}
}
}
or you can use JNA (complete example here)
Shell32.INSTANCE.SHGetFolderPath(null,
ShlObj.CSIDL_DESKTOPDIRECTORY, null, ShlObj.SHGFP_TYPE_CURRENT,
pszPath);
javax.swing.filechooser.FileSystemView.getFileSystemView().getHomeDirectory()
Seems not that easy...
But you could try to find an anwser browsing the code of some open-source projects, e.g. on Koders. I guess all the solutions boil down to checking the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Desktop path in the Windows registry. And probably are Windows-specific.
If you need a more general solution I would try to find an open-source application you know is working properly on different platforms and puts some icons on the user's Desktop.
You're just missing "C:\\Users\\":
String userDefPath = "C:\\Users\\" + System.getProperty("user.name") + "\\Desktop";
public class Sample {
public static void main(String[] args) {
String desktopPath =System.getProperty("user.home") + "\\"+"Desktop";
String s = "\"" + desktopPath.replace("\\","\\\\") + "\\\\" +"satis" + "\"";
System.out.print(s);
File f = new File(s);
boolean mkdir = f.mkdir();
System.out.println(mkdir);
}
}
there are 2 things.
you are using the wrong slash. for windows it's \ not /.
i'm using RandomAccesFile and File to manage fles and folders, and it requires double slash ( \\ ) to separate the folders name.
Simplest solution is to find out machine name, since this name is only variable changing in path to Desktop folder. So if you can find this, you have found path to Desktop. Following code should do the trick - it did for me :)
String machine_name = InetAddress.getLocalHost().getHostName();
String path_to_desktop = "C:/Documents and Settings/"+machine_name+"/Desktop/";

Need a way to check status of Windows service programmatically

Here is the situation:
I have been called upon to work with InstallAnywhere 8, a Java-based installer IDE, of sorts, that allows starting and stopping of windows services, but has no built-in method to query their states. Fortunately, it allows you to create custom actions in Java which can be called at any time during the installation process (by way of what I consider to be a rather convoluted API).
I just need something that will tell me if a specific service is started or stopped.
The IDE also allows calling batch scripts, so this is an option as well, although once the script is run, there is almost no way to verify that it succeeded, so I'm trying to avoid that.
Any suggestions or criticisms are welcome.
here's what I had to do. It's ugly, but it works beautifully.
String STATE_PREFIX = "STATE : ";
String s = runProcess("sc query \""+serviceName+"\"");
// check that the temp string contains the status prefix
int ix = s.indexOf(STATE_PREFIX);
if (ix >= 0) {
// compare status number to one of the states
String stateStr = s.substring(ix+STATE_PREFIX.length(), ix+STATE_PREFIX.length() + 1);
int state = Integer.parseInt(stateStr);
switch(state) {
case (1): // service stopped
break;
case (4): // service started
break;
}
}
runProcess is a private method that runs the given string as a command line process and returns the resulting output. As I said, ugly, but works. Hope this helps.
You can create a small VBS on-th-fly, launch it and capture its return code.
import java.io.File;
import java.io.FileWriter;
public class VBSUtils {
private VBSUtils() { }
public static boolean isServiceRunning(String serviceName) {
try {
File file = File.createTempFile("realhowto",".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set sh = CreateObject(\"Shell.Application\") \n"
+ "If sh.IsServiceRunning(\""+ serviceName +"\") Then \n"
+ " wscript.Quit(1) \n"
+ "End If \n"
+ "wscript.Quit(0) \n";
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec("wscript " + file.getPath());
p.waitFor();
return (p.exitValue() == 1);
}
catch(Exception e){
e.printStackTrace();
}
return false;
}
public static void main(String[] args){
//
// DEMO
//
String result = "";
msgBox("Check if service 'Themes' is running (should be yes)");
result = isServiceRunning("Themes") ? "" : " NOT ";
msgBox("service 'Themes' is " + result + " running ");
msgBox("Check if service 'foo' is running (should be no)");
result = isServiceRunning("foo") ? "" : " NOT ";
msgBox("service 'foo' is " + result + " running ");
}
public static void msgBox(String msg) {
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, msg, "VBSUtils", javax.swing.JOptionPane.DEFAULT_OPTION);
}
}
Based on the other answers I constructed the following code to check for Windows Service status:
public void checkService() {
String serviceName = "myService";
try {
Process process = new ProcessBuilder("C:\\Windows\\System32\\sc.exe", "query" , serviceName ).start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
String scOutput = "";
// Append the buffer lines into one string
while ((line = br.readLine()) != null) {
scOutput += line + "\n" ;
}
if (scOutput.contains("STATE")) {
if (scOutput.contains("RUNNING")) {
System.out.println("Service running");
} else {
System.out.println("Service stopped");
}
} else {
System.out.println("Unknown service");
}
} catch (IOException e) {
e.printStackTrace();
}
}
I have been dealing with installers for years and the trick is to create your own EXE and call it on setup. This offers good flexibility like displaying precise error messages in the event an error occurs, and have success-based return values so your installer knows about what happened.
Here's how to start, stop and query states for windows services (C++):
http://msdn.microsoft.com/en-us/library/ms684941(VS.85).aspx
(VB and C# offers similar functions)
I have had some luck in the past with the Java Service Wrapper. Depending upon your situation you may need to pay in order to use it. But it offers a clean solution that supports Java and could be used in the InstallAnywhere environment with (I think) little trouble. This will also allow you to support services on Unix boxes as well.
http://wrapper.tanukisoftware.org/doc/english/download.jsp
A shot in the dark but take a look at your Install Anywhere java documentation.
Specifically,
/javadoc/com/installshield/wizard/platform/win32/Win32Service.html
The class:
com.installshield.wizard.platform.win32
Interface Win32Service
All Superinterfaces:
Service
The method:
public NTServiceStatus queryNTServiceStatus(String name)
throws ServiceException
Calls the Win32 QueryServiceStatus to retrieve the status of the specified service. See the Win32 documentation for this API for more information.
Parameters:
name - The internal name of the service.
Throws:
ServiceException
Here's a straignt C# / P/Invoke solution.
/// <summary>
/// Returns true if the specified service is running, or false if it is not present or not running.
/// </summary>
/// <param name="serviceName">Name of the service to check.</param>
/// <returns>Returns true if the specified service is running, or false if it is not present or not running.</returns>
static bool IsServiceRunning(string serviceName)
{
bool rVal = false;
try
{
IntPtr smHandle = NativeMethods.OpenSCManager(null, null, NativeMethods.ServiceAccess.ENUMERATE_SERVICE);
if (smHandle != IntPtr.Zero)
{
IntPtr svHandle = NativeMethods.OpenService(smHandle, serviceName, NativeMethods.ServiceAccess.ENUMERATE_SERVICE);
if (svHandle != IntPtr.Zero)
{
NativeMethods.SERVICE_STATUS servStat = new NativeMethods.SERVICE_STATUS();
if (NativeMethods.QueryServiceStatus(svHandle, servStat))
{
rVal = servStat.dwCurrentState == NativeMethods.ServiceState.Running;
}
NativeMethods.CloseServiceHandle(svHandle);
}
NativeMethods.CloseServiceHandle(smHandle);
}
}
catch (System.Exception )
{
}
return rVal;
}
public static class NativeMethods
{
[DllImport("AdvApi32")]
public static extern IntPtr OpenSCManager(string machineName, string databaseName, ServiceAccess access);
[DllImport("AdvApi32")]
public static extern IntPtr OpenService(IntPtr serviceManagerHandle, string serviceName, ServiceAccess access);
[DllImport("AdvApi32")]
public static extern bool CloseServiceHandle(IntPtr serviceHandle);
[DllImport("AdvApi32")]
public static extern bool QueryServiceStatus(IntPtr serviceHandle, [Out] SERVICE_STATUS status);
[Flags]
public enum ServiceAccess : uint
{
ALL_ACCESS = 0xF003F,
CREATE_SERVICE = 0x2,
CONNECT = 0x1,
ENUMERATE_SERVICE = 0x4,
LOCK = 0x8,
MODIFY_BOOT_CONFIG = 0x20,
QUERY_LOCK_STATUS = 0x10,
GENERIC_READ = 0x80000000,
GENERIC_WRITE = 0x40000000,
GENERIC_EXECUTE = 0x20000000,
GENERIC_ALL = 0x10000000
}
public enum ServiceState
{
Stopped = 1,
StopPending = 3,
StartPending = 2,
Running = 4,
Paused = 7,
PausePending =6,
ContinuePending=5
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class SERVICE_STATUS
{
public int dwServiceType;
public ServiceState dwCurrentState;
public int dwControlsAccepted;
public int dwWin32ExitCode;
public int dwServiceSpecificExitCode;
public int dwCheckPoint;
public int dwWaitHint;
};
}
I improvised on the given solutions, to make it locale independent.
Comparing the string "RUNNING" would not work in systems with non-english locales as Alejandro González rightly pointed out.
I made use of sc interrogate and look for the status codes returned by it.
Mainly, the service can have 3 states:-
1 - Not available
[SC] OpenService FAILED 1060: The specified service does not exist as an installed service.
2 - Not running
([SC] ControlService FAILED 1062: The service has not been started)
3 - Running
TYPE : 10 WIN32_OWN_PROCESS
STATE : 2 START_PENDING
(NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x7d0
PID : 21100code here
So using them in following code, gives us the desired result :-
public static void checkBackgroundService(String serviceName) {
Process process;
try {
process = Runtime.getRuntime().exec("sc interrogate " + serviceName);
Scanner reader = new Scanner(process.getInputStream(), "UTF-8");
StringBuffer buffer = new StringBuffer();
while (reader.hasNextLine()) {
buffer.append(reader.nextLine());
}
System.out.println(buffer.toString());
if (buffer.toString().contains("1060:")) {
System.out.println("Specified Service does not exist");
} else if (buffer.toString().contains("1062:")) {
System.out.println("Specified Service is not started (not running)");
} else {
System.out.println("Specified Service is running");
}
} catch (IOException e) {
e.printStackTrace();
}
}
During startup, create a file with File.deleteOnExit().
Check for the existence of the file in your scripts.
Simply call this method to check the status of service whether running or not.
public boolean checkIfServiceRunning(String serviceName) {
Process process;
try {
process = Runtime.getRuntime().exec("sc query " + serviceName);
Scanner reader = new Scanner(process.getInputStream(), "UTF-8");
while(reader.hasNextLine()) {
if(reader.nextLine().contains("RUNNING")) {
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

Categories