Cannot run shell function using PHP - java

I encountered some problems with the shell function in php. I want to execute a java program on the server by running a php function on a php webpage.
The java program writes some chars to a local file on the server.
test.php
<?php
$WshShell = new COM("WScript.Shell");
$cmd = ' "C:\\Program Files\\Java\\jdk1.6.0_14\\bin\\java" Importer 1 2 updated.txt 7';
$WshShell->exec($cmd);
echo "okay";
?>
When test.php is executed via command line on the server,
c:\php test.php
the java program runs.
However, if I executed it through web browser, the Java program is not called.
http://127.0.0.1/test.php
Is it because the Apache user is not allowed to use the command line functionality on windows?
System configuration:
Microsoft windows XP, Professional X64 edition, Version 2003, service pack 2
PHP version: 5.2.6.6
Apache 2.2
IIS 6

Why don't you use ordinary PHP 'exec' function?

i found the solution here
Calling MySQL exe using PHP exec doesn't work
it is not the OS problem, but "quotes" problem.

What error do you got ?
Not really knowing php i do however have a question:
You dont fully qualify the file that the Java program needs to update, you sure its not written but under some directory where your php server is installed,

Your problem could be due to PHP running with Apache under "safe_mode"...You can check this in your php.ini file.

Related

Execute shell script on remote windows server without any third party installation

I want to execute shell script on remote windows server through java code without any server setup and installations like SSH. My script file is already there on server. I just want to execute it on server cmd from my machine. Any possible solution?
According to definition of shell script from wiki:
"A shell script is a computer program designed to be run by the Unix shell"
Taking into consideration that you want to run a shell script on a windows server it will simply not work. I would suggest you to use some kind of environment as it is provided by cygwin or any other alternatives.
You can also check the following question.

How to execute a java -jar file using PHP, and load a file created by the .jar to the server?

I am trying to execute a .jar file from a PHP script using the exec() command, more exactly using:
<?php
$command = 'java -jar myfile.jar';
exec( $command, $output);
?>
both the php script, and the jar are in the same directory on the server. The myfile.jar produces an excel spreadsheet, and I want this spreadsheet to be created or stored in the same directory or another directory for that matter on the server. When I execute myfile.jar in the terminal of my pc, the program works fine and outputs the file to the directory where the .jar is located.
Any ideas?
Best Regards.
If I understand correctly, you want to execute the myfile.jar file, which returns some data and save it to a file? If you don't need to edit the data, you can use:
exec ( 'java -jar myfile.jar > output.xml' );
Ok, so I installed PHP on my windows, and am using EasyPHP to test as if I had the server on my own computer.
The php code is working fine, but from the $output variable I could notice that the program suddenly gave a Fatal Error.
I managed to dechipher that this error was from the time limit PHP has for running scripts.
I used set_time_limit(0); and corrected this. Now the java program finishes its run, and no problem there.
However, the excel file is being created and comes out empty. I suspect this has something to do with the FileOutputStream that the java code uses to generate the spreadsheet.
Can you point me in the right direction ?

Running java program on web server

I need to run a single java program (which will change the content of some text files) on my new website. What's the easiest way to do that for example using php code?
Do I need to install JVM or any other software in order for this to work?
Please guide on how to do this step by step if possible.
You have to install a Java Runtime Environment (JRE) on your server to be able to run a java program.
http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads-1880261.html
To execute a java program in php, you can simply use the exec() function :
exec("java -jar yourProgram.jar");
http://php.net/manual/en/function.exec.php

Run Java class file in PHP (running successfully in command line but not PHP)

I know the approach to run external programs in PHP is to use exec() or shell_exec(). I have tried them both, even system(), but none of these seem to be working.
I have added all the required jar files to my "jre/ext" folder and running the java class file is successful in command line.
So in command line, after compiling, my command to run is "java MyProgram". In PHP, I tried echo exec('java MyProgram') and shell_exec('java MyProgram').
I have a simple sample Hello_World Java file and PHP is able to run that successfully. I am thinking may be the issue can be I have Apache POI jar files that are needed for the java class, but I don't know how should I resolve or explore further now.
Thanks in advance!

Printing barcodes using java library withing PHP

I have a html button linked to php page. The php page calls a jar file which has to print barcodes on the barcode printer on the server (the default and the only network printer installed).
I'm calling it as within my php.
$out = system("java -jar C:\\wamp\\bprint\\bprint.jar ABC1234");
The jar works fine in the command line (and prints the barcodes) but it doesn't print anything within my php page. I know the jar is being called in my php page because a System.Out.Println(); within my jar executes fine. Other executable are working too within system()
I am running WAMP on Windows XP and Apache is running as a Admin user.
My question is, it the printing part being blocked by PHP or Java or Apache. How should I overcome it?
Thanks
system only returns the last line of stdout. Try using exec with the array &$output parameter.
$out = array();
exec("java -jar C:\\wamp\\bprint\\bprint.jar ABC1234", $out);
var_dump($out);

Categories

Resources