I can run java file using command line:
java HelloWorld
Now I am trying to run a HelloWorld.class file in php script using exec(). The problem is, when I run commands like:
<?php exec("which java", $output);
print_r($output); ?>
it print correct result, but when I tried to run java file:
<?php exec("java HelloWorld", $output);
print_r($output); ?>
it only print out Array()
.
The content of HelloWorld.java is:
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World!!!!");
}
}
And I have already create the HelloWorld.class file using command line javac HelloWorld
.
So how can I fix it?
PS. I changed the command into:
<?php exec("java HelloWorld 2>&1", $output);
print_r($output); ?>
and I got Array ( [0] => Error: Could not find or load main class HelloWorld )
Does anyone know what the problem is?
Ok, I've fixed it.
I forget to set classpath when I use php exec().
So, the code should be:
<?php exec("java -cp '/var/www/html/run' HelloWorld 2>&1", $output);
print_r($output);?>
Hello World!!!!
or just to print outjava HelloWorld
– Zach Jul 20 '14 at 16:05javac HelloWorld
and thenjava HelloWorld
so that the program you wrote compiles first – Zach Jul 20 '14 at 16:10