Call Java application via PHP

5

I created a Java application here in my company however they want to call this application in Java via a website made in PHP. I would like to know if it is possible for me to make a PHP code that calls a Java application, no matter the form if it is for link by button or any other way.

So far what needs to be done and call my Java application via PHP and would like to know if this is possible to do and how this can be done.

    
asked by anonymous 09.06.2015 / 15:27

3 answers

4

You can use the exec () function to call a Java program or any other program that is on your server.

But care , this function can be dangerous if you let the user execute any command, for example, it can call a function that corrupts your files or even formats the machine .

Calling a Java program can be done like this:

<?php exec("java -jar arquivo.jar argumentos", $saida); ?>
    
09.06.2015 / 15:45
4

You can use PHP's exec() function to run your Java:

File testar.java

class testar {
    public static void main(String[] args) {
        System.out.println("Olá mundo!");
    }
}

File testar.php

echo exec('java testar');

Running the file testar.php

$ php testar.php
Olá mundo!

Note:
You have to keep in mind the path to the Java file, otherwise you should have no problems.

    
09.06.2015 / 15:47
2

One way to consume a Java application from PHP is to use the Zend Java Bridge . It allows you to consume your Java-created classes from your PHP script.

The flow that runs to execute this Java code in follows.

Anexamplecodeusingthisfeaturewouldbe:

<?php//criaoobjetoJava$stock=newJava("com.ticker.JavaStock");

// consome os métodos em Java
$news = $stock->get_news($_GET['ticker']);

// Exibe os resultados
foreach($news as $news_item) {
    print "$news_item<br>\n";
}

Note that this feature is only available in the Enterprise version of Zend Server, not cheap .

Perhaps rewriting this Java application in PHP or port it to a web interface makes more sense.

    
09.06.2015 / 16:21