I found a couple other questions with my problem, but the solutions did not work. I have a web page on my local server that you can input text for a whole java class. When submit is clicked I want the output to be displayed. Using shell_exec I can compile the java file. I get nothing when I try to run it, though.
shell_exec("javac /folder/Test.java"); // works
echo shell_exec("/usr/bin/java folder.Test"); // nothing is returned
I decided to use the full path to java when I read that it solved the problem for someone else. When I run:
/usr/bin/java folder.Test
from the command line it works.
The Test.java file looks like this:
package folder;
public class Test{
public static void main(String[] args){
System.out.println("testing");
}
}
EDIT: I could not get w0rldart's to work. I can actually see something when I try Mathieu's. I tried adding the -classpath option (I am not sure if I am using it correctly)
echo shell_exec("java -classpath ./folder/ Test 2>&1");
I get the same error as without -classpath:
Exception in thread "main" java.lang.NoClassDefFoundError: Test Caused by:
java.lang.ClassNotFoundException: Test at
java.net.URLClassLoader$1.run(URLClassLoader.java:217) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:205) at
java.lang.ClassLoader.loadClass(ClassLoader.java:321) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at
java.lang.ClassLoader.loadClass(ClassLoader.java:266) Could not find the main class:
Test. Program will exit.
IT WORKS: Just had to remove the period from
echo shell_exec("java -classpath ./folder/ Test 2>&1"); //error
echo shell_exec("java -classpath /folder/ Test 2>&1"); //works