0

I start an Apache HTTP server within my Java program. Unfortunately, I cannot stop the server from the program through the "httpd.exe -k stop" command. The error message is "(OS 5)Access is denied. : Failed to open the Apache2.2 Service". My OS is Windows 7. How can I stop the server from my Java program? (Stopping manually the server is not an option.)

UPDATE: I cannot change the Windows permissions.

s106mo
  • 1,243
  • 2
  • 14
  • 20

4 Answers4

2

A dirty--but plain java--solution is to call:

Runtime.getRuntime().exec("taskkill /F /IM httpd.exe");
stealthyninja
  • 10,343
  • 11
  • 51
  • 59
s106mo
  • 1,243
  • 2
  • 14
  • 20
  • To kill a specific httpd process and all its child processes call `Runtime.getRuntime().exec("taskkill /F /t /PID _PIDNUMBER_");` Moreover, you should wait that all child processes are really terminated by calling for example `Thread.sleep(1000);`. Sounds strange, but it is the way it is...try it ;) – s106mo May 19 '11 at 07:19
1

It might be possible to write a CGI script, deploy it into Apache and call that from Java.

In theory the script would run with the same privileges as Apache itself and could contain the code necessary to stop the server ( apachectl -k stop ).

For security , the CGI script would only accept localhost connections and would require a password. If all requirements are met, it would issue the call above.

Ray Hulha
  • 10,701
  • 5
  • 53
  • 53
0

1). Goto Start 2). In the search box type "services" 3). Click on "services" and you will find your apache installed from the list. 4). select apache and you will find the options of start/restart/stop on the left of your screen or you will find the options of start/restart/stop by right clicking on the apache. 5). After this it is self explanatory. Note 1:- In case changes such as start/restart/stop are not taken up immediately , you may have to restart windows. Note 2:- This is an alternate solution to your problem . It is a best practice to restart via Apache application itself. Nevertheless, the above mentioned method mostly works at least as of now.

0

Looks like you need to run your program as an administrator. Take a look at this question: Java: run as administrator

Community
  • 1
  • 1
Saggio
  • 2,212
  • 6
  • 33
  • 50
  • It's unfortunately not an option. I cannot run a program as an administrator nor can configure anything regarding windows. – s106mo May 16 '11 at 13:18