0

I am thinking if I can bridge java and php without Tomcat installed. I just installed jdk17, I set up the PATH to C:\jdk17\bin and my first program is like this

<html>
<body>
<?php
  $system = new Java("java.lang.System");
  print "Java version=".$system->getProperty("java.version")." <br>\n";
  print "Java vendor=".$system->getProperty("java.vendor")." <p>\n\n";
  print "OS=".$system->getProperty("os.name")." ".
              $system->getProperty("os.version")." on ".
              $system->getProperty("os.arch")." <br>\n";

  $formatter = new Java("java.text.SimpleDateFormat","EEEE, 
    MMMM dd, yyyy 'at' h:mm:ss a zzzz");
  print $formatter->format(new Java("java.util.Date"))."\n";

?>
</body>
</html>

and fact is it doesn't work. nothing is shown on the browser. I think I need to add something else into the php.ini but don't know what and where in the file I can do that.
Sorry I am dumb about all of these, thank you for any help.

Edit: What can java do with php ? Some companies use these two although I am opposed to it thinking ASP and C# should do better. Do they think LINUX hosting and storage are UNHACKABLE ?

Edit: By the way, I am using IIS as my webserver not traditional Apache, thanks for your concern.

Edit: I think I am in a complete confusion now about java world (Tomcat, apache, xxxsuits, ant, soap, xxxprotocol....). Could someone post a short post to summarize all of these ? [something easy to understand like an micky course lecture] please.

2
  • what are you trying to do? i assume that this is what you're looking for: php.net/manual/en/book.java.php Commented Jan 14, 2012 at 3:51
  • Why are you thinking companies are deciding what languages to use based on "unhackability"? Clearly there are several advantages to use linux servers and open source products that have nothing to do with security at all. And obviously if you have a dozen java and php programmers you won't tell them to learn C# before starting the newest project. In practice there are lots of things that matter much more than some minor technical details. Commented Jan 14, 2012 at 4:42

2 Answers 2

3

Here is your problem:

new Java("java.text.SimpleDateFormat","EEEE, 

I'm unaware of any sort of object called "Java" on PHP that you'll get out of the box by simply installing Java. I think you're trying to execute the Java Bridge integration which means you'll need to download the Java Bridge distribution and execute the Java Bridge server before executing your PHP page.

Java is different than PHP because it is compiled. PHP is interpreted which means you don't compile it. The PHP interpreter reads the source code at runtime and interprets the source code to make it execute instructions the computer really understands. This means you deploy the source code to the production servers. It also means you can simply change the source code and rerun the file and it will change its behavior.

Java is different. The source code is parsed at compile time when the developer is writing the code. Compiling translates the source code into instructions prior to deployment. That means the source code isn't deployed. Therefore, Java doesn't spend any extra time at runtime to convert source code into instructions. That's done prior to deployment. Java also converts the bytecode (java instructions) into native instructions using something called the JIT (just in time compilation). The JIT is crazy fast. Java is much faster than PHP in execution time. But, if you want to change the behavior you have to change the source and recompile it. Ant is a tool that helps compile, package, and deploy Java code. There is also Maven, but it has a higher learning curve. Much of this is the same story for C# too. It is also a compiled language.

Java support structure is massive. There is tons of tooling and libraries for Java that just aren't available to other stacks. Things like Lucene, Spring, Hadoop, Tomcat, PDF generation, etc. What Java brings to the table is access to lots of code that isn't always possible with PHP only. For example, I have a PHP website that I'm generating barcodes and PDF files of those barcodes. I have to use Java to generate this and have PHP call it remotely through Tomcat. Generating barcodes, PDFs, are easier in Java because libraries exist for this. Working with Java is easier than using C (and safer). PHP is great at fast integration because it will run anything.

If you wanted to do quick integration with Java you can execute the java executable from the command line. PHP can exec() any program, and Java is not different from the view of the OS. It's just an executable. The upside is you don't need extra tooling like Tomcat, Servlets, etc. The downside is the Java VM can take up a fair amount of memory and time to startup. If you are exec()'ing lots of Java VMs you could easily eat up memory on your server. That's where something like Tomcat can help because it would run a long running program within a single Java VM that you can interact with either through sockets, HTTP, etc. That single program can serve lots of requests at once. There are plenty of options for integration.

http://php.net/manual/en/function.exec.php

Sign up to request clarification or add additional context in comments.

Comments

1

I think best way to do that is to use Zend Java Bridge.It's a part of Zend Server CE (freeware).

Everything is pre-configured there and it's working.

http://files.zend.com/help/Zend-Server/working_with_the_java_bridge.htm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.