How can I use JAXB from an unsigned applet (without signing it)?

后端 未结 3 1029
生来不讨喜
生来不讨喜 2021-01-22 12:50

I would like to marshall Java objects into XML and vice-versa from within an Unsigned Applet and I can\'t change any of the security permission/policy files, or sign the applica

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-22 13:07

    This may solve your problem. I know it solved mine :)

    public void actionPerformed(ActionEvent e) {
        try {
        JAXBContext jc = AccessController.doPrivileged(new PrivilegedExceptionAction() {
    
            public JAXBContext run() throws JAXBException {
    
                // needs to run here otherwise throws AccessControlException
                return JAXBContext.newInstance(SimpleObject.class);
            }
        });         
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    
            SimpleObject object = new SimpleObject();
            object.setSampleText("Hello");
    
            marshaller.marshal(object, System.out);
        }
        catch (JAXBException e1) {
            throw new RuntimeException(e1);
        }
        } catch (PrivilegedActionException e2) {
        throw new RuntimeException(e2);
        }
    }
    

    Hope it helps

提交回复
热议问题