0

Hi I am attempting to execute a jar from PHP 5

Here is the code I am executing:

$command = '"java -jar WEB-INF\\lib\\FileReceiver.jar '.$address.' '.$service_port.' \"'.$command.'\" \"'.$filePath.'\""';
exec($command, $out);

But the jar is not being executed. I had logged the $command variable in firebug and took the output and inserted into the code such as:

exec("java -jar WEB-INF\lib\FileReceiver.jar 127.0.1.1 2018 \"docs/document.txt \" \"C:\\apache-tomcat-7.0.26\\webapps\\test\\downloads\\doument.txt\"", $out);

gives the correct output. I dont understand why hard coding it works but the variable which contains the same information does not.

Could someone help me out?

Thanks

3
  • are you sure $adress $service_port $command and $filepath have the right values? Commented Apr 4, 2012 at 2:33
  • you need to show \" , you should write \\" Commented Apr 4, 2012 at 2:33
  • @Shingetsu yes I am sure, I literally copied whats in the variable $command from firePHP console and pasted it. Commented Apr 4, 2012 at 2:47

2 Answers 2

1

try like this

$command = escapeshellarg($address).' '.escapeshellarg($service_port).' '.escapeshellarg($command).' '.escapeshellarg($filePath).' ';

exec('java -jar WEB-INF\\lib\\FileReceiver.jar '.$command, $out);
Sign up to request clarification or add additional context in comments.

6 Comments

same issue if I hard code what is contained in the variable its worksm but if I pass the variable, the jar is not executing. Thank you for your quick reply. Do you have any other suggestions?
I tried it and the result is correct. I replaced $command with the echo result in exec and it works correctly. I am totally confused why having the variable in the exec will not execute.
@ßee , try with escapeshellarg
I tired that too nothing...it just adds a space after the double quotes at the beginning and end and removed the quotes in the middle which would make it worse. Either way the jar did not execute
Thank you so much for your help. I just figured out the problem. the $command had a \n character which stopped it from executing in the jar file.
|
0

You're over-escaping the double quotes in your first snippet.

You're using single quotes for the literal string delimiter, so you don't need to escape the double quote.

Also, the final '\""' should be '"'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.