Forum Moderators: coopster

Message Too Old, No Replies

Java Jar File + PHP

php, jar, java

         

rodriguez1804

9:36 am on May 1, 2010 (gmt 0)

10+ Year Member



Hey guys,

Here's my problem: I have a java jar file that is working correctly and all, but when the file is run, it asks the user for some input, then based on that input, does some calculations and prints out the results using System.out.print to the screen.

What I would like is to call the jar file from a php script, but I don't know how to pass the values to the jar file for it to do the calculations? Any ideas? Here's how I am calling the jar file from php:


<?php
$output = array(); //array to store output
$return_var = 0;
exec('java -jar className.jar', $output, $return_var); //actual call to jar file
$output_str = join($output,"\n");

echo $output_str."\n".$return_var; //supposed to output data, but since no input was //given, no data is returned
?>


Whenever I call this as it is, it just returns a 1, I guess meaning it got called successfully? but without input? Thanks for any help.

rocknbil

8:02 pm on May 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This would largely depend on the argument vectors accepted by the .jar itself. It may be that no ARGS are allowed. Let me try that in English . . .

(I'm going to explain this in Perl-ese, because I've done it a lot in Perl, but have never done anything of this sort with PHP.)

We can often use argument vectors to pass to a program, if it's written to accept them, that is,

<form method ="post" action="yourscript.pl">
<input type="text" name="yourname" id="yourname">
<input type="text" name="myname" id="myname">
</form>

can be

exec `yourscript.pl yourname=test myname=bill`;

and yourscript.pl may or may not be able to "recognize" $ARGV[0] as yourname=test and $ARGV[1] as myname=bill. But still, even Perl won't "do anything" with @ARGV if I haven't written it into the program to do something with it. The default variable in Perl to accept command line input (not field input) is @ARGV (for PHP coders, that translates, with differences, to $argv [php.net])

foreach $v (@ARGV) {
## split on =, do something with it
}

And that is my point. When you post directly from within the .jar/applet whatever, it's expecting that data from itself as user input, but may not be expecting argument vectors via external sources.

rodriguez1804

10:55 pm on May 2, 2010 (gmt 0)

10+ Year Member



Interesting! I ll give it a try using the method you mentioned above. Do you guys think this is the best approach to go about this if I MUST make a call to a jar file with some input?

rocknbil

11:31 pm on May 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well sure, but you should be looking at the documentation for the Java program, if there is any. I imagine it wouldn't matter if it's nested in PHP or Perl or whatever, the question is doe the Java program accepts arguments.