ERROR: java.lang.NoClassDefFoundError: Could not initialize class sun.security.provider.NativePRNG
search cancel

ERROR: java.lang.NoClassDefFoundError: Could not initialize class sun.security.provider.NativePRNG

book

Article ID: 296170

calendar_today

Updated On:

Products

VMware Tanzu Greenplum

Issue/Introduction

Symptoms:

Executing a function created using PL/JAVA fails can fail with an error similar to below:

Executing the function 1st time gives an error similar to:
cbl=# select java_get_key('abc');
ERROR: java.lang.ExceptionInInitializerError (JNICalls.c:70)

Subsequent execution indicates NoClassDefFoundError exception:
cbl=# select java_get_key('abc'); 
ERROR: java.lang.NoClassDefFoundError: Could not initialize class sun.security.provider.NativePRNG (JNICalls.c:70)

 

Environment


Cause

PL/Java is a TRUSTED language. PostgreSQL stipulates that a language marked as trusted has no access to the filesystem and PL/Java enforces this. Any user can create and access functions or triggers in a trusted language.

PL/Java also installs a language handler for the language "javaU". This version is not trusted and only a superuser can create new functions that use it. Any user can still call the functions.
More details on https://github.com/tada/pljava/wiki

Database logs available under $MASTER_DATA_DIRECTORY/pg_log must be searched for any error during the execution of the function. In this case, the below error message indicates that /dev/random file can't be read during the execution of the function.

2014-07-04 11:10:16.113551 PDT,,,p480393,th699058016,,,,0,,,seg-1,,,,,"LOG","00000","3rd party error log:
 at com.pivotal.cv.common.PivotalCrypto.encrypt(PivotalCrypto.java:41)",,,,,,,,"SysLoggerMain","syslogger.c",520,
 2014-07-04 11:10:16.113654 PDT,,,p480393,th699058016,,,,0,,,seg-1,,,,,"LOG","00000","3rd party error log:",,,,,,,,"SysLoggerMain","syslogger.c",520,
 2014-07-04 11:10:16.114333 PDT,,,p480393,th699058016,,,,0,,,seg-1,,,,,"LOG","00000","3rd party error log:
 Caused by: java.lang.SecurityException: read on /dev/random",,,,,,,,"SysLoggerMain","syslogger.c",520,

/dev/random is a special file which provides an interface to the kernel's random number generator and cannot be used by trusted languages. Any function which uses system files like /dev/random should be created using an untrusted version of the language, ex. PL/JAVAU.

 

Resolution

Step 1. Verify if PL/JAVAU is already available, if not, create PL/JAVAU language.

Below output indicates it's already installed.
gpadmin=# select * from pg_language where lanname = 'pljavau';
 lanname | lanispl | lanpltrusted | lanplcallfoid | lanvalidator | lanacl
---------+---------+--------------+---------------+--------------+--------
 pljavau | t f | 16562 | 0 |
If pljavau is not available, create it using below:
psql -d <dbname> -c "CREATE LANGUAGE pljavau"

Step 2: Recreate the function using PLJAVAU as a superuser.
Example:

CREATE OR REPLACE FUNCTION java_get_key(bytea)
RETURNS bytea AS 'com.pivotal.cv.common.PivotalCrypto.rawEncrypt'
LANGUAGE pljavau VOLATILE;

Step 3: Verify by executing the function again.
Example:

gpadmin=# select java_get_key('abc');
java_get_key ---------------------------
5M\x0C\x12&gHo\x10c