3

Help please. I can't figure out what the problem is with running java files thru proc_open(). It worked on C programs and I was able to get the output per line so I think nothing is wrong with fetching the output stream this way:

$ctr = 0;
$score_ctr = 0;
$out2 = "";



    while (!feof($pipes[1])) {

     $out2[$ctr]= fgets($pipes[1]);
     $ctr++;

    }

    fclose($pipes[1]);  

For running Java in PHP, I am using these codes:

Checking the extension, then compile if java(this was successful since it was able to produce the CLASS file in the same directory:

if($ext == "java" || $ext =="JAVA"){ //case for java
        exec('cd \xampp\htdocs\ci_user\uploads & javac '.$file);
}

enter image description here


To execute the program,

if($ext == "java" || $ext =="JAVA"){ //case for java

    //Removes '.java' extension for cmd
    $name2 = preg_replace("/\\.[^.\\s]{3,4}$/", "", $name);

   //Command to be executed
    $p = 'cd \xampp\htdocs\ci_user\uploads & java '.$name2;


    $process = proc_open($p, $descriptorspec, $pipes);
}

I have tried running this command in Window's cmd, and it worked (was able to run java and print "Hello, World"), so I am sure there's nothing wrong with it.

cd \xampp\htdocs\ci_user\uploads & java HelloWorld

I am getting this error in my error log file:

        java.lang.UnsupportedClassVersionError: HelloWorld : Unsupported major.minor version 51.0
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClassCond(Unknown Source)
            at java.lang.ClassLoader.defineClass(Unknown Source)
            at java.security.SecureClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.access$000(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
        Could not find the main class: HelloWorld.  Program will exit.

I don't know if PHP's using another java source path in executing java thru proc_open. I assumed that since it is using my Window's cmd, there will be no library loading issues or any 'version' related problems.

What could be the problem here? Thank you so much! I would appreciate any reply.

user2800050
  • 93
  • 11
  • what is your Java version? – Michał Rybak Sep 28 '13 at 12:50
  • @MichałRybak java version "1.7.0_07" Java(TM) SE Runtime Environment (build 1.7.0_07-b11) Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode) – user2800050 Sep 28 '13 at 12:53
  • and javac version? your issue may be related to incompatibility beetween your version of JRE and your class file. [more here](http://stackoverflow.com/questions/10382929/unsupported-major-minor-version-51-0) – Michał Rybak Sep 28 '13 at 12:57
  • 1
    if it worked in Windows cmd, you may want to check which file is used as 'java' in PHP, using `readlink -f java` – Michał Rybak Sep 28 '13 at 13:04
  • @MichałRybak Alright sir. I am checking it now. Thanks for this lead. – user2800050 Sep 28 '13 at 13:10
  • @MichałRybak, THANK YOU SO MUCH! I was able to make it work. :) I've got the solution posted, tho i know there is another solution (maybe better). But, at least, i was able to run it. THANK YOU SIR! – user2800050 Sep 28 '13 at 13:55

1 Answers1

0

OMG. Thanks a lot Sir Michal Rybak (@MichalRybak) for helping me get to this solution. Since I cannot figure out how to change what java version proc_open() or PHP uses, I just compiled the java programs and make the classes compatible to lower versions.

I changed this code:

exec('cd \xampp\htdocs\ci_user\uploads & javac '.$file);

to this:

exec('cd \xampp\htdocs\ci_user\uploads & javac -source 1.4 -Xlint:-options '.$file);

THANK YOU SO MUCH!!!!!!!!!! You really helped me a lot to get to this answer sir! :D

user2800050
  • 93
  • 11