Run Java program with PHP and return Console Output

Asked

Viewed 695 times

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, either an error, or a phrase like Olá Mundo!, using PHP.

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

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

  • With the command system("java Teste", $output) it is possible to display in the console the return of your java code. It tries to run a java Teste right on the console to check if it returns an error. You compiled your class ??

  • After doing what @Oliveira suggested, try. exec("java Test", $output) var_dump($output);

  • @Oliveira, yes I compiled my class, via system("javac C:\Users\Desktop\Hello.java", $saida);, I also compiled via console just to make sure. But when I try to run code with system("java C:\Users\Desktop\Hello", $saida);, and print the output variable, only the number is printed 1

  • @Marcosxavier also used the exec(), but I was unsuccessful :(

  • @Gamen probably had not compiled its class when running 'system("java C: Users Desktop Hello", $output);' the return was the value 1, if the class had been compiled it would display the value 0.

1 answer

0


Basically it would be 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 too many problems. I used the shell_exec for being simpler, passes to it the command, that is, entering the path of my class and running the file .class compiled from her.

P.S.: I see you have not compiled your class, run the command javac Classe.java to compile.

The class used in the example was:

public class HelloWorld 
{

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

        }

}
  • @Ivcs Kra, your answer is almost perfect, really when I run the program, I get the output, thank you very much. But assuming I was trying to compile a code with some error, could I return that error? Because using your example, if I use the javac in a class with some problem, let’s say the lack of a;, the output is zero. But again, thanks for the solution!

  • @Gamen You can use 2>&1 at the end of each command to force a response

  • @Ivics Again, thank you very much, it worked perfectly! Man, I have no words to describe how much you helped, vlw even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.