user2723490
user2723490

Reputation: 2100

Calling Java from PHP vs shell - java.lang.NoClassDefFoundError:

I have two java classes that reside in .../jtest/StepRegTest directory and include "package StepRegTest;" directive.

Option 1. I call it directly from shell:

[jtest]# java StepRegTest.Main

Everything works fine.

Option 2. I call java via PHP exec:

<?php
echo getcwd() . "\n";
exec("java StepRegTest.Main 2>&1", $output);
print_r( $output );
?>

The PHP file is in the same directory jtest as for the Option 1, when I call it from the shell. However, it returns an error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/quinncurtis/matpackjava/DMatBase

The jar archive that refers to these missing classes is in the CLASSPATH. The import reference for that class is:

import com.quinncurtis.matpackjava.DDMat;

Note that everything works fine when I execute the code from the shell.

What is the problem here? Why does it work from the shell, but not from PHP?

Thanks!

Upvotes: 0

Views: 433

Answers (1)

ash
ash

Reputation: 5165

If the CLASSPATH environment variable is being used, verify it's set at the time the php exec() call is made; it may be getting lost.

One easy way to check is this:

<?php
echo getcwd() . "\n";
exec("env", $output);
print_r( $output );
?>

To set the CLASSPATH variable in the php script, the following can be done:

putenv("CLASSPATH=/path/to/lib.jar");

Here's a page that describes putenv():

http://php.net/manual/en/function.putenv.php

Upvotes: 1

Related Questions