java & php

This one has me stumped. I’m trying to run a java class through php, but all it tells me is Error: Could not find or load main class public_html.admin.files.Augoingestion
[php]echo shell_exec('java ‘/public_html/admin/files/Autoingestion 2>&1’);[/php]
The class does work because i can run it fine through putty. The file location is correct. I guess i should know that this is running on a vps from godaddy (which could be 99% of the problem).

is it a permission issue web server software is it allowed to run that java

I can run it through putty, so i don’t think its a permission issue, i’m thinking its got more to do with it replacing the / with a .

is that quote after java not a syntax error?

I’m just guessing but…

Most likely the problem is your path. Because PHP does not run under the same user that you log in with via putty. It would run under a system user like “apache”

So /public_html would not be a valid absolute path for PHP. It would be something like /www/username/www.domain.com/public_html/

Perhaps you can place a PHP file inside /public_html and do a print_r($_SERVER) to find the full absolute path.

I just did that and got, i also added the /home part in the code and it didn’t work.

[code]Array
(
[DOCUMENT_ROOT] => /home/venzo/public_html
[GATEWAY_INTERFACE] => CGI/1.1
[HTTP_ACCEPT] => text/html, application/xhtml+xml, /
[HTTP_ACCEPT_ENCODING] => gzip, deflate
[HTTP_ACCEPT_LANGUAGE] => en-US
[HTTP_CONNECTION] => Keep-Alive
[HTTP_COOKIE] =>xxxxx
[HTTP_DNT] => 1
[HTTP_HOST] => venzodigital.com
[HTTP_USER_AGENT] => Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
[PATH] => /bin:/usr/bin
[QUERY_STRING] =>
[REDIRECT_STATUS] => 200
[REMOTE_ADDR] => 108.11.168.160
[REMOTE_PORT] => 50597
[REQUEST_METHOD] => GET
[REQUEST_URI] => /cron.php
[SCRIPT_FILENAME] => /home/venzo/public_html/cron.php
[SCRIPT_NAME] => /cron.php
[SERVER_ADDR] => 50.63.148.120
[SERVER_ADMIN] => [email protected]
[SERVER_NAME] => venzodigital.com
[SERVER_PORT] => 80
[SERVER_PROTOCOL] => HTTP/1.1
[SERVER_SIGNATURE] => Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/1.0.0-fips mod_bwlimited/1.4 Server at venzodigital.com Port 80

[SERVER_SOFTWARE] => Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/1.0.0-fips mod_bwlimited/1.4
[PHP_SELF] => /cron.php
[REQUEST_TIME] => 1352266064
[argv] => Array
    (
    )

[argc] => 0

)[/code]

I’ve never messed with these kinds of scripts, so i’m not really sure what’s going on, but from what i’ve been reading, it seems to be that shell_exec is replacing the slashes with periods, but i’ve tried passthru and exec and they both do the samething, so i don’t know.

So this gives you the same error?

[php]
$res = java /home/venzo/public_html/admin/files/Autoingestion <parameters>;
echo “

” . $res . “
”;
[/php]

Also, does your java use the full absolute paths as well? Not just /public_html?

all that code did was print out what’s in $res, didn’t actually do anything. When i add -classpath after java, it gives me

Error: Could not find or load main class [email protected]

skipping the path completely.

Sorry I’m happy to troubleshoot with you but I’m mostly just guessing :slight_smile:

Any luck using escapeshellarg()? e.g.

[php]
$res = shell_exec(‘java ’ . escapeshellarg(’/home/venzo/public_html/admin/files/Autoingestion’));
echo “

” . $res . “
”;
[/php]

Also, in putty if you type ‘which java’ it should give you the full path. Try specifying the full path instead of just ‘java’ e.g. ‘/path/to/java’

typing which java didn’t do anything and that code didn’t give me any output at all. I do appreciate your help though. The only reason i’m doing it like this is because i wanted to get the path right because doing it as a regular cron job was getting me no where - course it doesn’t help that i’m 3 hours ahead of the server.

It’s been 10 years since I used Java and I never tried to use it from PHP. From the google searches I did it seems like people are only executing jar files. e.g. ‘java -jar /path/to/script.jar’

Sorry I couldn’t help

Its ok, at least you tried. I’ll do some more research on it. I guess apple is just behind on the times :slight_smile:

If you just google “execute java through php” there are quite a few threads from stackoverflow with good information. Your problem is simply a matter of trial an error - there is no specific solution. If you do figure it out, please post the solution here :slight_smile:

I got it. I saw something when i was looking at a function that got me thinking of doing it another way. This is what i came up with
[php]echo shell_exec(“cd public_html
cd admin
cd files
java Autoingestion
cd”);[/php]
I didn’t think it would work, but it did the trick.

Hmm… so likely you just needed some quotes. Do either of these work?

[php]
$res = shell_exec(“java '/home/venzo/public_html/admin/files/Autoingestion '”);
echo “

” . $res . “
”;
[/php]

[php]
$res = shell_exec("cd /home/venzo/public_html/admin/files; java Autoingestion ");
echo “

” . $res . “
”;
[/php]

Whatever the case, I’m glad you figured it out :slight_smile:

Well, now i’m having a lot of issues with part 2 of this project. Now that its downloading the files, i have to decompress the files, parse it and then import it into a table (the parsing and importing i can already do). I was reading up on how to extract it and since phar is loaded, i’m lef to learn now to how use it. This is what i have so far [php]foreach(glob(“files/.txt.gz") as $filename) {
$file = explode(’.’, basename($filename));
$nfile = $file[0].".gz";
rename($filename, ‘files/itunes/’.$nfile);
unset($filename);
}
foreach(glob("files/itunes/
.gz”) as $filename) {
// i got this from another website
try {
$phar = new PharData($filename);
$phar->extractTo(‘files/itunes/’); // extract all files
} catch (Exception $e) {
echo $e;
}
}[/php] and this is the error i’m getting

exception ‘UnexpectedValueException’ with message ‘internal corruption of phar “/home/venzo/public_html/admin/files/itunes/S_D_80045583_20121106.gz” (__HALT_COMPILER(); not found)’ in /home/venzo/public_html/admin/cron.php:17 Stack trace: #0 /home/venzo/public_html/admin/cron.php(17): PharData->_construct('files/itunes/S…’) #1 {main}

Originially i thought it was because the files download as txt.gz, so i renamed the files to just .gz, which changed the error message to what’s above. Hopefully i’m on the right track.

nevermind, i got it. I got fed up with using phar, so i went with good 'ol gunzip and shell_exec. Why change what works :slight_smile:

Well, it works as i have it if i run the script manually, but if i do it from a cron, it gives me errors on the directory changing. I’m trying the other ways that were suggested above, and method 1 doesn’t work :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service