Page 1 of 1

Call Java Program using PHP

Posted: Tue Oct 14, 2008 8:31 pm
by madzman23
Hi guyz, do you this is possible? :roll:
I am using LAMP, then my new prototype project required to fire up a Java program that will get data from a database and make it available to the webpage for the user to download the file. Well, Ive done the Java code and the php code. My problem now is I dont know how to call the Java program in my PHP code. Can you help me with this guyz?

Ive try the exec() function but it wont work, but when I try to exec() some shell command, it works but if I tried to call a executable program, it wont work. I tried the exec("firefox"); to call the firefox to run, but it wont :? . The programs that I am trying to put in the exec() function are working in shell command line.
Is there any other function that I can be used to call such external application?

Thank you.

Re: Call Java Program using PHP

Posted: Tue Oct 14, 2008 8:57 pm
by dyrathror
Hi,

I see three possibilities to attach a java prog to your php:
  • wrap a webservice around your java class and use soap to communicate with it
D.

Re: Call Java Program using PHP

Posted: Thu Oct 16, 2008 8:47 pm
by madzman23
Thanks for you response dyra, but I already did what your three suggestions.

1. I used the exec(), shell_exec(), passthru() and system() function to run the java and other external but none of it works.
Like for example, I want to run the firefox using the functions above, but no one there can run the firefox, I think it should be similar when I want to run a java application. It seems that the functions above only work for simple shell command like "ls -l", "pwd", and some other basic shell command. Have you tried to run any external operations using those functions? If yes, can you please send it to my email. (mad.sumaya23@gmail.com)

2. I dont know how to use soap :wink: , I also dont know what will be the output with this. Sorry. :(

3. I think the PHP/Java bridge is not necessary, because I just need to call the java program so that it will run. I will not use any variables or any java codes in my php, so I think this PHP/Java bridge is too complex for what I need. I just need to make the Java run and determine if its finish using PHP and then go on to the next script.

Re: Call Java Program using PHP

Posted: Fri Oct 17, 2008 2:06 am
by alex.barylski
I used the exec(), shell_exec(), passthru() and system() function to run the java and other external but none of it works.
Why doesn't it work, or do you not know?

I would guess that those functions are only good for invoking shell script or similar CLI utilities. Lauching an applicaiton that reuqire a desktop/window manager or whatever it's called in Linux. Hmmmm. Maybe the process control functions in PHP?

http://ca.php.net/pcntl

How do you launch Window'ed applications from linux command line normally? Do you need to start the XWindows/KDE/GNome servers first?

I've often wondered hos this worked under linux so hopefully someone else chimes in. :)

Re: Call Java Program using PHP

Posted: Fri Oct 17, 2008 2:33 pm
by panic!
madzman23 wrote:Hi guyz, do you this is possible? :roll:
I am using LAMP, then my new prototype project required to fire up a Java program that will get data from a database and make it available to the webpage for the user to download the file. Well, Ive done the Java code and the php code. My problem now is I dont know how to call the Java program in my PHP code. Can you help me with this guyz?
Why not use php to get the data from the database
madzman23 wrote: exec("firefox");
...sigh

Re: Call Java Program using PHP

Posted: Mon Oct 20, 2008 2:10 am
by madzman23
PCSpectra wrote:
I used the exec(), shell_exec(), passthru() and system() function to run the java and other external but none of it works.
Why doesn't it work, or do you not know?
I really dont know, Im new in PHP and I never used those function yet. :oops:
PCSpectra wrote: I would guess that those functions are only good for invoking shell script or similar CLI utilities. Lauching an applicaiton that reuqire a desktop/window manager or whatever it's called in Linux. Hmmmm. Maybe the process control functions in PHP?

http://ca.php.net/pcntl
Yeah, I also thinks that those functions can only work on basic shell script.
PCSpectra wrote: How do you launch Window'ed applications from linux command line normally? Do you need to start the XWindows/KDE/GNome servers first?

I've often wondered hos this worked under linux so hopefully someone else chimes in. :)
If you want to execute an application using the shell, you will just type in the name of the application (but if its set in the bin) ex. If you want to run the firefox, you will just type the firefox and then the mozilla firefox will pop up, so I thought if I do that in those function it will have the same output but its not. You can also run the java program in the shell using the command for ex. "java myJavaProgram" or execute its jar. But it all wont work in those file function.
panic! wrote: Why not use php to get the data from the database
Yeah, I know it would be easier to do that, but this project tends to make a prototype that will allow PHP to run and use a Java Program that it can be use in so many ways. Well, as I see and from your reactions and suggestions, I think my prototype is a failure. :( :cry:

Anyways, thanks for your responses, I hope there can be other that can help me out here.
I know it would be not that hard, its just a one simple command. :mrgreen:

Re: Call Java Program using PHP

Posted: Mon Oct 20, 2008 9:44 am
by panic!
It is very possible but I think you need to understand the exec command a bit better on *nix based systems.

It's not like something like visualbasic where you can just call 'firefox.exe' and it will launch that exe.

When you call a command it isn't in the same session as your desktop it's in a completely new instance of your environment that isn't visible to your desktop session.

So you could call something like "mkdir poopoo" (provided you had the permissions) and see a directory that has been made. You could also call a shell script and get a value from it. But you can't (as far as I know) interact with your current desktop session unless your application is a GTK desktop application. I could be wrong on that, but I'm pretty sure that's how it works.

Re: Call Java Program using PHP

Posted: Mon Oct 20, 2008 2:21 pm
by shiznatix
i use exec all the time to rerun some php scripts from an admin section. i think you need to just do something like exec('java programThing'); just give it a try hommie

Re: Call Java Program using PHP

Posted: Wed Oct 22, 2008 2:20 am
by madzman23
shiznatix wrote:i use exec all the time to rerun some php scripts from an admin section. i think you need to just do something like exec('java programThing'); just give it a try hommie
Yeah, I did this buddy but it doesn't works for me.
Is there any configuration or something that you do for it to run?

Re: Call Java Program using PHP

Posted: Thu Oct 23, 2008 9:33 am
by shiznatix
I remember having difficulties at first but what I think I did to fix it was give an absolute path to the program to run. Also, I had to make sure I was using the correct command because even though from the command line I could do "php /path/to/script.php" without problems, I couldn't do it using exec(), I had to do "php5 /path/to/script.php". Example of what I am doing right now:

Code: Select all

$command = escapeshellcmd('php5 /var/www/cron/'.$room.' '.$year.' '.$month.' '.$day.' '.$final);
exec($command, $output);
and this works for me without any problems. Just make sure you are giving an absolute path and that you are using the last or at least correct command.

Re: Call Java Program using PHP

Posted: Fri Oct 24, 2008 2:32 am
by madzman23
waaaaaa...
I manage to run it using the PHP files, but when I used it on the java files, it won't work. :cry: :cry: :cry:

Re: Call Java Program using PHP

Posted: Fri Oct 24, 2008 4:01 am
by madzman23
Yeah, I did it bro. :lol: Thank you very much. :drunk:

I just run its jar file, it doesn't run when you just called the java file.
Thanks a lot bro.

8)