Calling Java application via PHP

Asked

Viewed 4,457 times

5

I created a Java application here in my company but they want to call this application in Java via a site made in PHP. I wonder if it is possible to make a PHP code that calls a Java application, no matter the shape if it is by link per 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.

3 answers

4


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

But caring for, 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 format the machine.

Calling a program in Java can be done like this:

<?php exec("java -jar arquivo.jar argumentos", $saida); ?>

4

You can use the function exec() PHP 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');

Run the file testar.php

$ php testar.php
Olá mundo!

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

2

One way to consume a Java application from PHP is by using Zend Java Bridge. It allows you to consume your classes created in Java from your PHP script.

Follow the stream that runs to run this Java codeen.

inserir a descrição da imagem aqui

An example of code using this feature would be:

<?php

// cria o objeto Java
$stock = new Java("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";
}

It is worth remembering that this feature is only available in the Zend Server Port version, which is not cheap.

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

  • hmm nice friend however make my java application in php is out of the question until then because it is java web and so my application consists of taking the macaddress of the machine of who accesses to be able to make a registration and in php it is not possible to do this kind of thing until where I know and clear

  • Then you need the Java application running from the client, running it from the server will get only the server mac, not the client.

  • Maybe this question can help you: http://stackoverflow.com/questions/1420381/how-can-i-get-the-mac-and-the-ip-address-of-a-connected-client-in-php

Browser other questions tagged

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