Run Java program with PHP and return Output from Console

0

I need to run a Java program that is on my server, for example Teste.java and return the console output resulting from the execution, be it an error, or a phrase like Olá Mundo! , using PHP.

I already used commands like exec("java Teste", $output) and system("java Teste", $output) , and could actually execute the program, but could never return the console output that this program generated.

Is there any way to do this program execution, and return the output of it?

    
asked by anonymous 19.10.2016 / 12:32

1 answer

0

Basically it would look like this:

<html>

<head>
    <meta charset="utf-8" />
    <title>Java Execute</title>
</head>

<body>
    <form action="#" method="post">

        <input name="executar" value="executar" type="submit"/>
    </form>

    <?php

    if(count($_POST) > 0) {
        $path = "/home/leonardo/workspace/Java/src/";
        $class = "HelloWorld";

        if(isset($_POST['executar'])) {
            echo "Saída: " . shell_exec("cd {$path} && java {$class}"); 

        }

    }

    ?>
</body>
</html>

If your java class is properly programmed and compiled it will work without much trouble. I used shell_exec because it is simpler, it passes the command to it, that is, by entering the path of my class and executing the% comp_configured file of it.

Q: I see that you have not compiled your class, run the command .class to compile.

The class used in the example was:

public class HelloWorld 
{

        public static void main(String[] args) 
        {
                System.out.println("Ola, Stack Overflow!");

        }

}
    
19.10.2016 / 14:58