Abhinav
Abhinav

Reputation: 8168

How to run Java Program through shell_exec command in PHP?

I have a PHP Project with the name itext. Inside this folder I have a workspace folder. So the Path to the Java Class file is like this:

itext/workspace/sampleproject/checkFonts.java

I have an index.php file in itext folder. How can I execute the shell_exec command in the index.php for the java file located in the above path?

UPDATE:

As per the answer I tried out stuffs and executed the below code and it works but I am getting the below error:

$output = array();
exec('java workspace/itext/src/itext/CheckFonts 2>&1',$output);
print_r($output);

Error:

Array ( [0] => Error: Could not find or load main class workspace.itext.src.itext.CheckFonts )

Upvotes: 0

Views: 2009

Answers (1)

Trung Nguyen
Trung Nguyen

Reputation: 529

You can specify any path relative to the file your code is running from. So from your index.php the path would be workspace/sampleproject

If you want to use the full explicit directory you can use realpath() function:

realpath('workspace/sampleproject');

Here's a guide to running Java at the command line for windows environment: http://www.skylit.com/javamethods/faqs/javaindos.html

Your example code might be:

$javaPath = realpath('workspace/sampleproject');
$output = shell_exec('C:\ProgramData\Oracle\Java\javapath\java.exe '
                     . $javaPath . DIRECTORY_SEPARATOR . 'checkFonts');`

Upvotes: 1

Related Questions