0

A few days ago I published this thread, and it was solved succesfully on Unix.

Now I moved to Windows and I have the same problem.

I need to execute a command from the web application. It must execute a .jar stored in the system. I looked on Google and found some changes that I needed to do.

The function to execute the command is:

<?php
if($_POST["name"] == "")
    echo "name is empty";
else{
    $path = $_POST["name"];
    //$command = 'DISPLAY=:0 java -jar '.$path'; -> Used in Unix
    //$command = 'java -jar ../../../../simulaciones/tanqueCalentamiento.jar';
        $command = 'java -jar ..\..\..\..\simulaciones\tanqueCalentamiento.jar';
    //system($command);
    exec($command);
}
?>

The comments are different options I have tried. $path is the argument, but decided to put directly the path, to clarify my question:

Do I need a kind of "trick" as I did for Unix?

Blanca Hdez
  • 3,513
  • 19
  • 71
  • 93

3 Answers3

2

The one I use:

function _exec($cmd)
{
   $WshShell = new COM("WScript.Shell");
   $oExec = $WshShell->Run($cmd, 0,false);
   error_log($cmd);
   return $oExec == 0 ? true : false;
}

/!\ If your application has an interface, and if you are using vista or 7, your server can't be run as a service!

Whiler
  • 7,998
  • 4
  • 32
  • 56
  • My applicacion has an interface and I am using 7... I used your function and it returns 1. As I don't understand very well your code, I don't know neither what it really means... Sorry, completely newbie in this field... – Blanca Hdez Sep 04 '11 at 21:06
  • The application has probably been launched, but you can't see it. Which server are you using? apache, IIS? – Whiler Sep 04 '11 at 21:09
  • At least now the process ends, before it was just stack. I'm using apache, do you need more information?? – Blanca Hdez Sep 04 '11 at 21:09
  • Actually I have the apache running in a xampp server – Blanca Hdez Sep 04 '11 at 21:10
  • Try to uninstall the Apache service, and then, launch manually apache. Then, check again if it's working. – Whiler Sep 04 '11 at 21:11
  • 1
    @Blanca WScript.Shell provides direct access to the shell via COM, I have mentioned it previously [here](http://stackoverflow.com/questions/7093510/exec-waiting-for-a-response-in-php/7093638#7093638) with links to the official docs which explain what it does/how it works. – DaveRandom Sep 04 '11 at 21:11
  • @Blanca Are you trying to launch a GUI app here? – DaveRandom Sep 04 '11 at 21:12
  • @Whiler +1 for being someone who actually knows how to use COM to launch programs asynchronously. I have seen so many ugly solutions involving 20 calls `start` and other horribleness... – DaveRandom Sep 04 '11 at 21:13
  • 1
    Yes.. She is... she should start apache from the user, not from the _session Console_ – Whiler Sep 04 '11 at 21:14
  • BTW, as it returns 1, this means it works as expected. The issue is only linked to Windows' sessions. – Whiler Sep 04 '11 at 21:16
  • With xampp, you should have a file like this somewhere: \xampp\apache\apache_uninstallservice.bat – Whiler Sep 04 '11 at 21:17
  • @Whiler If you like PHP/have lost the file, you can also do it with [win32service](http://php.net/manual/en/book.win32service.php). Obviously, it is a PITA to have to manually start Apache everytime - could you start it with a scheduled task set to run as the user, with 'Only run if logged on...' checked? Or does this still launch in such a way that it wouldn't work? – DaveRandom Sep 04 '11 at 21:21
  • It's strange, because I can not stop apachee. Not using Ctrl+C in the console (I started it using xampp_start), not from the control panel. I even restarted my computer and it was already running... could be the problem here? – Blanca Hdez Sep 04 '11 at 21:24
  • @DaveRandom: Personnally, I use the _Windows tasks scheduler_... with the trigger on _session open_ (and as I have an autologin, it starts when my computer starts ;o) ; But let's see if Blanca can get his java application works from a manual start before the next step ;o) – Whiler Sep 04 '11 at 21:24
  • @Blanca: Open the services (run _services.msc_) and find apache or xamp (I don't have xamp.. don't really now how it manages all the stuff (apache, mysql, ..) ; then stop the apache service. If you can't, stop the process Apache.exe (with the task manager). – Whiler Sep 04 '11 at 21:26
  • :) Thanks. If I type: java -jar C:\xampp\htdocs\blanca\simulaciones\tanqueCalentamiento.jar it is executed with no problem – Blanca Hdez Sep 04 '11 at 21:27
  • ok... but.. if you stop the apache service... uninstall the apache service (just this service, not xamp), and then launch apache manually... when you call your webpage, does it work? – Whiler Sep 04 '11 at 21:31
  • I can not stop apache, not even uninstall it, no even uninstall xampp. The reason is that I have a process running (apache) and no permits to stop it. Not even as runas /noprofile /user:Administrator cmd – Blanca Hdez Sep 04 '11 at 21:46
  • Do you see the Apache service when you run _services.msc_? If yes, could you edit it and choose _Desactivate_ for its start? If no, I don't have any more ideas ;o( – Whiler Sep 04 '11 at 21:51
  • Aftter restaarting my computer several times, I managed to kill apache process. Then I uninstall the whole xampp server, installed again, but the result is the same... :( I'm still getting 1 fromj your function, but the jar file is not execute. Thanks again – Blanca Hdez Sep 04 '11 at 22:20
  • 1
    and does Apache starts as a service? if yes, this could **not work** better... it must **NOT** start as a **service** as you have a **GUI** – Whiler Sep 04 '11 at 22:27
  • The problem was how I was starting xampp. i post an answer to clarify it better, Thans for all your comments – Blanca Hdez Sep 05 '11 at 21:04
0

You're just starting 'java'. That means that the Java executable (java.exe) must exist, and must be able to be executed. Since you don't specify a path, it must be in a directory that is in the %PATH% environment variable.

To test, simply start cmd and type java<enter>. If java gets executed, you'll know at least you'll have java installed and startable. If it doesn't, check if you have java and specify the correct path to the java executable.

You can also run the .jar from the command line to see if it works. If it works like this, it is likely to work in PHP too.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • I have jaava installed, and if I run thee same command it runs with no problem. Then I guess, the problemm must be in another place... – Blanca Hdez Sep 04 '11 at 21:01
0

In windows vista, 7 (I don't know if in others too), there is a problemm with xampp.

The apache does not stop, doesn't matter what you do, even switch the computer off. At least for me, the solution was:

  1. Uninstall xamp and reinistall it.
  2. Start/stop it always as administrator:

    path\xampp\xampp_start
    path\xampp\xampp_stop

Right click: run as administrator.

In this way apache is finished properly, and I can run my command with no probleems anymore

I hope this helps http://drupal.org/node/224342

Blanca Hdez
  • 3,513
  • 19
  • 71
  • 93