-1

im trying to run a java file through php and shell_exec() is behaving in a strange way.

<?php 
shell_exec("javac Driver.java");
echo shell_exec("java Driver");
?>

This code causes the contents of the java file to be displayed in my web browser and i have no idea why as i am new to php. Any insight would be appreciated.

Blake
  • 151
  • 2
  • 11

1 Answers1

1

It could be either because there is a problem during compilation and so no program to execute or maybe because of redirection of output.

To resolve the issue.

Step 1 : Compile the java program from commandline and make sure java program-name command is giving desired output. Also use absolute path to java file whenever required
Step 2 : Then if that is correct you should check if redirection of output is correct. system.out.println may not be using stderr   . Try adding adding 2>&1 after your command.
Step 3 : Check your classpath.

Please look at the link below. It has a solution to same issue you are facing.

Running a Java File from PHP

Community
  • 1
  • 1
Raj
  • 1,945
  • 20
  • 40