You probably want to fully-qualify paths here (especially if this is a web application):
exec("javac test.java");
exec("java test");
Something more like:
exec("/path/to/javac /and/the/path/to/test.java");
exec("/path/to/java /and/the/path/to/test");
It's possible that it's "running" but the working directory isn't correct, so maybe it can't find the javac
and java
executables or can find them but can't find test.java
. When you run these things manually the executables are in your path and the file is in your working directory, but both of those may not be true for the PHP context.
Aside from that, it's also good practice to fully-qualify things anyway. It's more explicit and less prone to error (or even exploit, if a user were to somehow upload a javac
executable of their own to your website they could execute it with elevated permissions using your script).