1

I've a hash map like : Map gen = HashMap<Integer, MyObj>

When I execute AccessController.doPrivileged(gen), it throws exception as follows. Can anyone help me on this.

java.security.PrivilegedActionException: java.io.NotSerializableException: java.util.HashMap
    at java.security.AccessController.doPrivileged(Native Method)...

This code is getting executed in Weblogic environment.

Sastrija
  • 3,284
  • 6
  • 47
  • 64
  • 1
    This question makes no sense. AccessController.doPrivileged doesn't take a Map as argument, but a PrivilegedAction or PrivilegedExceptionAction. This code shouldn't even compile. – JB Nizet Nov 09 '11 at 12:08
  • oh... yes... I forgot... I've a class which will handle all these and I'm passing the object of this class as parameter to `AccessController.doPrivileged()` method. – Sastrija Nov 09 '11 at 12:22

3 Answers3

6

The objects you store in the Map (in your case MyObj) should implement the interface java.io.Serializable

Sastrija
  • 3,284
  • 6
  • 47
  • 64
Daniel Albert
  • 757
  • 5
  • 22
  • I still have same problem after making `MyObj` as serializable. What about the `Key`? – Sastrija Nov 09 '11 at 11:28
  • Make sure that MyObj implements Serializable and you generated a random serialVersionUID property on MyObj. – Daniel Albert Nov 09 '11 at 11:34
  • I've another `Map` inside `MyObj`. What about this case? – Sastrija Nov 09 '11 at 11:47
  • I'm checking documentation, and I think you are calling AccessController.doPriviliged with a Map param when you should pass a param that implements PrivilegedAction interface. http://download.oracle.com/javase/1.4.2/docs/api/java/security/PrivilegedAction.html – Daniel Albert Nov 09 '11 at 11:55
4

Your key is Integer which is Serializable by default. What is inside MyObj? I mean are there any objects inside MyObj?

Sastrija
  • 3,284
  • 6
  • 47
  • 64
mprabhat
  • 20,107
  • 7
  • 46
  • 63
  • Two `String` objects and a `Map` inside `MyObj` – Sastrija Nov 09 '11 at 11:51
  • 1
    what is the content of that Map ? If I get it correctly your MyObj is like this MyObj { string, string, Map}. If your Map inside MyObj contains anything which is not serializable then you wont be able to serialize it, try placing transient on your Map (inside MyObj) and see if you still get this exception – mprabhat Nov 09 '11 at 12:05
  • As I said, `MyObj` have 2 `String` objects and a `Map`. That `Map` also have a collection of objects which is already `Serializable`. – Sastrija Nov 09 '11 at 12:08
  • As per your comment, adding transient keyword to the variables which are not needed to serialize, made my code working. Thanks @mprabhat – Sastrija Nov 09 '11 at 12:30
1

MyObj should implement java.io.Serializable.

Hleb
  • 295
  • 1
  • 4
  • 15