Hi.
I don't know whether this is the right place asking this question or no.

I want to know, is it possible to use PHP code into JAVA code?

if yes how?

Dani AI

Generated

Short answer: you cannot drop PHP tags into a .java source file and have the JVM execute them. Java source is compiled to bytecode; PHP is parsed and run by a PHP interpreter. The only time that PHP text inside a Java string will "run" is if Java hands that text to a PHP interpreter at runtime (or you use a PHP implementation that runs on the JVM).

and pointed at the two common directions: either run PHP under the JVM (projects exist that implement PHP on Java) or call a PHP interpreter from Java. Which approach to pick depends on compatibility, performance and maintenance constraints:

  • If you need tight integration and can accept some PHP feature gaps, a JVM-based PHP runtime keeps everything inside the JVM.
  • If you need full PHP extension support or identical behavior to your production PHP, call the PHP process (or a PHP web endpoint) from Java and exchange data (JSON, XML, plain HTML).
  • If reusing logic is the goal, consider porting critical functions to Java or exposing PHP functionality via an HTTP/JSON service.

Example: start a PHP script and capture its output from Java (use this pattern rather than naive Runtime.exec to avoid deadlocks):

String[] cmd = {"php", "-f", "/path/to/script.php", "arg1"};
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true);
Process p = pb.start();
try (BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream(), java.nio.charset.StandardCharsets.UTF_8))) {
  String line;
  while ((line = r.readLine()) != null) {
    System.out.println(line);
  }
}
int exit = p.waitFor();

Practical cautions: read both stdout and stderr (or redirect), mind working directory and environment, watch encoding and timeouts, and remember JVM PHP runtimes often lack some C-based PHP extensions. For inter-process integration prefer structured outputs (JSON) rather than embedding raw HTML, and profile for process-start overhead if you will spawn PHP frequently.

Recommended Answers

All 4 Replies

Hi.
I don't know whether this is the right place asking this question or no.

I want to know, is it possible to use PHP code into JAVA code?

if yes how?

There are quite a few implementations of PHP in pure Java.

See:


http://www.sun.com/download/products.xml?id=45c26bc9

If you need something simple, you can just built the PHP executable on your Java Server, and then when you need to run some PHP, execute a system command within Java code that has the PHP executable execute some PHP for you. (write the command to stdin and read stdout)
eg:
http://www.wellho.net/resources/ex.php4?item=j714/Discfree.java
http://www.devdaily.com/java/edu/pj/pj010016/pj010016.shtml

the command would look like:

(on linux)

/usr/bin/php -r "echo 'hello from php binary';"

where "echo 'hello from php binary';" is the command sent to php.

Thanks very much for the information, still I am confused.

I want to know it is possible to use something like the following in a .java file.

System.out.println("<?php echo "<h1> Welocme</h1>" ?>");

Thanks very much for the information, still I am confused.

I want to know it is possible to use something like the following in a .java file.

System.out.println("<?php echo "<h1> Welocme</h1>" ?>");

It can be done. But you'd have to look at the documentation for the PHP interpreters written in Java to see if they support that.

If they don't they you could still write your own Java code that would reparse the output from Java with the PHP interpreter before it is sent to HTTP.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.