Reputation: 543
I'm a student learning computer engineering.
today, I ran a runnable jar file from command line.
the runnable jar file contains only 5 line code like following.
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
and I could see "Hello World" from command line.
but when I ran the same runnable jar file from PHP I got only empty output.
this is the code from php
<?php
echo shell_exec("java -jar /Users/Test/Desktop/test.jar");
?>
How can I get output after running the jar file on PHP ?
Thanks in advance for your answer and sorry for my poor english. If you don't understand my question please make a comment.
Upvotes: 2
Views: 1729
Reputation: 1150
I was able to get shell output, running commands with phpseclib. It's a bit more tricky but allows you to execute commands under another user (and also in another server if needed).
"phpseclib/phpseclib": "~2.0"
to your
composer.json and make composer update or use some other way to make it available to your code
use phpseclib\Crypt\RSA;
use phpseclib\Net\SSH2;
$key = new RSA();
$key->loadKey(file_get_contents('ssh_private_key_location'));
$ssh = new SSH2('server address/ip');
if (!$ssh->login('username'), $key)) {
echo 'Login Failed';
} else {
echo $ssh->exec('java -jar /path/to/test.jar');
}
If this code doesn't work out of the box, you can practice your investigation skills ;)
Upvotes: 0
Reputation: 55
You will have to put your Jar File inside the WWW folder and you will be get output.
<?php
exec('java -jar search.jar -query "find my dog"', $output);
print_r($output);
?>
Upvotes: 3
Reputation: 479
You could redirect your output of the jar into a textfile like in this post:
How Can I Pipe the Java Console Output to File Without Java Web Start?
So e.g.
java -jar /Users/Test/Desktop/test.jar >file.txt 2>&1
Then you can use busy waiting, to see whether the file is created or not, by calling :
while (! file_exists ( "/Users/Test/Desktop/file.txt" ) ){
//do nothing
}
//now you can access the output of the file
This should work for your use case, but still you should reconsider your application design, because in a productive environment this will not scale.
Cheers
Upvotes: 2