Domanda

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.

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top