Google Answers Logo
View Question
 
Q: Java Reflection problem in applet under Sun plugin and Netscape, MS works fine. ( No Answer,   3 Comments )
Question  
Subject: Java Reflection problem in applet under Sun plugin and Netscape, MS works fine.
Category: Computers > Programming
Asked by: jpdeckers-ga
List Price: $25.00
Posted: 25 Apr 2002 00:12 PDT
Expires: 28 May 2002 23:37 PDT
Question ID: 5065
Java Reflection problem in applet under Sun plugin and Netscape. 
 
I want to be able to call a showDocument using reflection, i.e.: 
-- 
Object o  = getAppletContext(); 
Method myMethod  = o.getClass().getMethod("showDocument",  
                                          new Class[]{URL.class}); 
myMethod.invoke(o, new Object[]{new URL(myUrl)}); 
--- 
 
Under Internet Explorer w/ MS JVM, this works fine, however,  
under Netscape, I'll get the following error: 
java.lang.IllegalAccessException: netscape/applet/MozillaAppletContext
 
With the plugin, I'll get: 
java.security.AccessControlException: access denied 
(java.lang.RuntimePermission 
accessClassInPackage.sun.plugin.viewer.context) 
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:270)
    at java.security.AccessController.checkPermission(AccessController.java:401)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:542)
    at java.lang.SecurityManager.checkPackageAccess(SecurityManager.java:1513)
    at sun.applet.AppletSecurity.checkPackageAccess(AppletSecurity.java:166)
    at java.lang.Class.checkMemberAccess(Class.java:1408) 
    at java.lang.Class.getMethod(Class.java:950) 
 
How can I make this work under the Sun plugin and Netscape (MS already
works fine), without having users change any setting etc (so run out 
of the box). 

Clarification of Question by jpdeckers-ga on 28 Apr 2002 23:17 PDT
The reflection is needed for an obfuscation-step 
Answer  
There is no answer at this time.

Comments  
Subject: Re: Java Reflection problem in applet under Sun plugin and Netscape, MS works fine.
From: not-ga on 25 Apr 2002 05:36 PDT
 
This is a security issue. In order to get around it you will need to
install a SecurityManager (more info at:
http://java.sun.com/j2se/1.3/docs/api/java/lang/SecurityManager.html )
that allows access to the packages you require.
Even more info here:
http://java.sun.com/j2se/1.3/docs/guide/security/smPortGuide.html
Subject: Re: Java Reflection problem in applet under Sun plugin and Netscape, MS works fi
From: greenrd-ga on 27 Apr 2002 13:31 PDT
 
The previous answer is incorrect. You cannot install a security
manager by default, because Netscape won't let you.

Why do you need to use reflection?

Here's what you could do. Suppose, for example, you want to call a
variety of different methods depending on the value of some string
variable. You should be able to simulate reflection, using something
like:

if (methodName.equals ("showDocument")) getAppletContext
().showDocument (myUrl);
else if (methodName.equals ("foo")) foo (bar);

etc.

It's not elegant, but it works.

Again, if this doesn't help, could you please explain why you need to
use reflection.
Subject: Re: Java Reflection problem in applet under Sun plugin and Netscape, MS works fine.
From: jinsookang-ga on 24 Jun 2002 10:14 PDT
 
This happens because when you get the AppletContext object you are
getting a sun implementation of it with a sun.* package name. 
Permission to access classes is denied to applets in the JRE
java.security file.  This makes your code work though:

Object o  = getAppletContext();
Class c = java.applet.AppletContext.class;
Method myMethod  = c.getMethod("showDocument", new
Class[]{URL.class});
myMethod.invoke(o, new Object[]{new URL(myUrl)});

because you are forcing the issue to access a java package class.

Alternatively, you can sign the applet and grant it:
RuntimePermission (accessClassInPackage.sun.)

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy