1

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
Ryan
  • 1,135
  • 1
  • 13
  • 28

2 Answers2

3

I'm very uncertain this will make a difference. But just to be sure, have you tried adding 2>&1 after your command?

echo shell_exec("/usr/bin/java folder.Test 2>&1");

I don't think system.out.println is using stderr to display its output, but it might be worth trying anyway...

Tchoupi
  • 14,560
  • 5
  • 37
  • 71
  • It did make a difference, I get output. But it's an exception and when ran from the command line it executes correctly. Here's the exception: – Ryan Feb 25 '12 at 20:57
  • Exception in thread "main" java.lang.NoClassDefFoundError: javafiles/Test Caused by: java.lang.ClassNotFoundException: javafiles.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:javafiles.TestCould not find the main class: javafiles.TestProgram willexit – Ryan Feb 25 '12 at 20:58
  • Oh that's why it was going to stderr. I guess the PHP process cannot access the system variable $classpath. You can either try w0lrdart suggestions, or set the class path in your command line with -classpath if I remember correctly. – Tchoupi Feb 25 '12 at 21:03
  • Got it, just needed to change what I put in the -classpath option. Thanks. – Ryan Feb 25 '12 at 23:12
1

If you want to use Test class from folder package then Test.class needs to be saved in a folder named folder. Then you need to have folder's parent directory on the CLASSPATH.

So if your configuration is the following: /rootdir/foo/folder/Test.class then you can start Test like this: java -classpath /rootdir/foo folder.Test.

I think this behavior is the same if you use java from php's shell_exec.

rics
  • 5,494
  • 5
  • 33
  • 42
  • I came back to say I fixed it, and this is what I ended up doing. Maybe I should have checked back here sooner so I could have used your advice. Thanks anyways. – Ryan Feb 25 '12 at 23:16