title: Running a Java File from PHP
body: <p>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.</p>
  
  <pre><code>shell_exec("javac /folder/Test.java"); // works
  echo shell_exec("/usr/bin/java folder.Test"); // nothing is returned
  </code></pre>
  
  <p>I decided to use the full path to java when I read that it solved the problem for someone else. When I run:</p>
  
  <pre><code>/usr/bin/java folder.Test
  </code></pre>
  
  <p>from the command line it works.</p>
  
  <p>The Test.java file looks like this:</p>
  
  <pre><code>package folder;
  public class Test{
   public static void main(String[] args){
    System.out.println("testing");
   }
  }
  </code></pre>
  
  <p>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)</p>
  
  <pre><code>echo shell_exec("java -classpath ./folder/ Test 2&gt;&amp;1");
  </code></pre>
  
  <p>I get the same error as without -classpath:</p>
  
  <pre><code>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. 
  </code></pre>
  
  <p>IT WORKS:
  Just had to remove the period from</p>
  
  <pre><code>echo shell_exec("java -classpath ./folder/ Test 2&gt;&amp;1"); //error
  echo shell_exec("java -classpath /folder/ Test 2&gt;&amp;1"); //works
  </code></pre>