0

I'm trying to open a console and run a PHP script, but after running it I need to print stuff to the console to get data from my Java program to the script, and the other way around.

I know there are already questions on here that ask kinda the same thing, but they don't mention how to get variables from the program to the script.

  • 2
    use webservice for that. – Amy Sep 07 '16 at 17:04
  • AJAX is your friend here. – Marc Giroux Sep 07 '16 at 17:07
  • I strongly recommend to make an API written on PHP and make calls from java application to php application in order to get your data. You just need to understand, that having to separate applications that communicate with each other is normal, but running php scripts directly from java - is not. – oleksandr.kazimirchuk Sep 07 '16 at 17:27
  • I think you will find the answer over here: http://stackoverflow.com/questions/12897070/how-can-i-run-php-code-within-a-java-application . Also, see http://docs.oracle.com/javase/7/docs/technotes/guides/scripting/programmer_guide/#scriptvars to see about how to pass vars into the script. – Jamie Sep 07 '16 at 17:31
  • @Jamie that doesn't state how to get the data to Java or from Java to the script. –  Sep 07 '16 at 17:34
  • http://stackoverflow.com/a/30915191/6797531 ? – CatalinB Sep 07 '16 at 18:12
  • @coolnickname, the Java Script Programming Guide section "Script Variables" shows an example of accessing a Java variable from the script, and even accessing a method from the script. You could write getters and setters, or probably even directly access field members. If that is not what you want, you could always feed a JSONObject (or XML, or whatever) into the script, and wriite another one back out (to be read by Java) to manage the input/output of the script. – Jamie Sep 07 '16 at 20:12

1 Answers1

-1

That sounds like a terrible idea, but who am I to judge... You can try j-php http://j-php.net

In the other direction you can try http://php-java-bridge.sourceforge.net/pjb/index.php

You could also use IPC (inter process communication).

Also, if the PHP or Python application has an output that you need on your java application you could save it in a file or in a database and read it from Java. Difficult to say without knowing more about the use case.

DrKarl
  • 356
  • 3
  • 8
  • I know this sounds awfull, but I'm making a Java program and I need to use an API only available to Python and PHP. Are there any other options? –  Sep 07 '16 at 17:21