Announcing the Saviynt Knowledge Exchange unifying the Saviynt forums, documentation, training,
and more in a single search tool across platforms. Read the announcement here.

Custom JAVA Jar NoClassDefFoundError in User Update Rule

rahul_p
Regular Contributor II
Regular Contributor II

Hello Team,

I am updating the user using user update rule for rehire case, I prepared JAVA code having 2 method, 1 is to take the token and 2nd to connect and update the user info.

When I am creating the object of OkHttpClient, its throwing Caused by: java.lang.NoClassDefFoundError

OkHttpClient client = new OkHttpClient();

When I am running this JAVA code directly without deploying, then its working. When I am deploying on Saviynt then its giving NoClassDefFoundError.

Is there any specific jar/library need by Saviynt as I added almost all OkHttpClinet jars.

 

Code which I am using:

package <packageName>;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.saviynt.custom.domain.UpdateResponse;
import com.saviynt.custom.domain.User;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

import org.json.JSONException;
import org.json.JSONObject;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.*;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

@SuppressWarnings("unused")
public class rehireUser {

public void updateDetails(String userJson) throws JSONException {
rehireUser obj=new rehireUser();
String methodName="updateDetails";
JSONObject json = new JSONObject(userJson);
String response=null;
String userid=null;
String token=null;
try {
Object username =  json.get("username");
userid=username.toString();
token=obj.getToken();
obj.updateUserInfo(token,userid);
}
catch(Exception e) {
System.out.println("Exception occurred "+e);
}
}

  public String getToken() throws IOException, JSONException 
  { 
  String methodName="getToken";
  String token="";
  OkHttpClient client = new OkHttpClient();  //This is where we are getting NoClassDefError
  MediaType mediaType =   MediaType.parse("text/plain");
  RequestBody body = RequestBody.create(mediaType, "{\"username\":\"<username>\",\"password\":\"<passwd>\"}");
  Request request = new Request.Builder().url("<URL>/ECM/api/login").method("POST",body).build(); 
  Response response = client.newCall(request).execute();
  String jsonData = response.body().string();
    JSONObject Jobject = new JSONObject(jsonData);
    Object resp=Jobject.get("access_token");
    token=resp.toString();
 
  return token; 
  }
  
  public void updateUserInfo(String token,String username) throws IOException, JSONException {
  String methodName="updateUserInfo";
    OkHttpClient client = new OkHttpClient();
  //String token=getToken();
  String jsonString=null;
  MediaType mediaType = MediaType.parse("application/json");
JSONObject obj=new JSONObject();
obj.put("username", username);
obj.put("customproperty1", "REHIRECASE");
jsonString=obj.toString();
RequestBody body = RequestBody.create(mediaType,jsonString);
Request request = new Request.Builder().url("<URL>/ECM/{path}/updateUser")
  .method("POST", body)
  .addHeader("Authorization", "Bearer "+token)
  .addHeader("Content-Type", "application/json")
  .build();
Response response = client.newCall(request).execute();
  
  }
  
}

 

Please help.
Regards,
Rahul
7 REPLIES 7

rushikeshvartak
All-Star
All-Star

Required Jar files are uploaded ? And applicationis restarted from server ? ( UI does not work sometimes)


Regards,
Rushikesh Vartak
If you find the response useful, kindly consider selecting Accept As Solution and clicking on the kudos button.

rahul_p
Regular Contributor II
Regular Contributor II

Hi @rushikeshvartak ,

Yeah, I have added required jars in the JAVA project and after deploying into Saviynt we restarted the Saviynt application as well, still its giving the NoClassDefError.

Note: When I am running this code as standalone from Eclipse by hardcoding any username, then JAVA programs getting token and updating the user in Saviynt as expected.

Regards,

Rahul

Try adding dependencies outside jar 


Regards,
Rushikesh Vartak
If you find the response useful, kindly consider selecting Accept As Solution and clicking on the kudos button.

rahul_p
Regular Contributor II
Regular Contributor II

Hi @rushikeshvartak ,

Outside of jar means? Upload those 3-4 dependent JAR to Saviynt as external Jar? Please confirm?

Regards,

Rahul

Yes


Regards,
Rushikesh Vartak
If you find the response useful, kindly consider selecting Accept As Solution and clicking on the kudos button.

Hi @rahul_p ,

  As we dicussed in call, please pack all your dependednt jar in your custom jar itself then upload it. Better use any build tool in your IDE like maven or ant for packing the jar. And make sure that size of teh jar doesn't exceed 10MB.

 

Kishore 

rahul_p
Regular Contributor II
Regular Contributor II

Hello Everyone,

As I was using third-party OkHttpClient  client to invoke the REST API, so the Saviynt expecting those Jars to be loaded while execution.

So to avoid this, we used HttpURLConnection and JSON object only to execute the REST API calls.

Please find below code snippet which can be used to invoke the Saviynt REST API:

public String getToken() {
String updateresponse = "";
try {
String tokengen="Bearer "+"<TOKENTOESTABLISHFIRSTCONNECTION>";
System.out.println("Got the token "+tokengen);
String URL ="<URL>/ECM/api/login";
URL updateurl= new URL(URL);
System.out.println(updateurl);
String readLine = null;
JSONObject serverDetailsObject = new JSONObject();
serverDetailsObject.put("username", "<username>");
serverDetailsObject.put("password", "<password>");
String jsonObjectString = serverDetailsObject.toString();
System.out.println("jsonObjectString ==> " + jsonObjectString);
HttpURLConnection conection = (HttpURLConnection) updateurl.openConnection();
conection.setDoInput(true); conection.setDoOutput(true);
conection.setRequestMethod("POST");
conection.setRequestProperty("Accept", "*/*");
conection.setRequestProperty("Content-Type", "application/json");
// String userName = "";
//String userCredentials =
//userName + ":" + userPassword;
// System.out.println(userName);
//LOGGER.info("Username ==> " + userName);
//System.out.println(userPassword); // LOGGER.info("UserPassowrd ==> " +
//userPassword); //String basicAuth = "Basic " +
//Base64.getEncoder().encodeToString(userCredentials.getBytes());

conection.setRequestProperty("Authorization", tokengen);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(conection.getOutputStream());
outputStreamWriter.write(jsonObjectString);
//System.out.println("outputStreamWriter ==> " + outputStreamWriter);
//LOGGER.info("outputStreamWriter ==> " + outputStreamWriter);
outputStreamWriter.flush();
System.out.println("Server object data ==> " + serverDetailsObject);
System.out.println("Connection\n" + conection + "\n");
int responseCode = conection.getResponseCode();
System.out.println("RESPONSE CODE = " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new
InputStreamReader(conection.getInputStream()));
StringBuffer response = new StringBuffer();
while ((readLine = in.readLine()) != null) {
response.append(readLine);
}
in.close();
updateresponse = response.toString();
JSONObject obj=new JSONObject(response.toString());

ftoken=obj.get("access_token");
System.out.println(ftoken); // }

} else {

System.out.println("POST NOT WORKED"); }

} catch (Exception e) {

updateresponse = e.getMessage(); e.printStackTrace();

} finally {


}

return ftoken.toString();

}

 

 

Thanks,

Rahul