1

I want to access sign applet method from javascript; hence I create a html file like:

html file:

<script type="text/javascript">
function uploadFileApp(){   
    document.applets[0].FileCooserApp();
    document.uploadAppletFile.FileCooserApp();      
}
</script>
<html>
<h1>Applet Demo</h1>
<body>
<applet name="uploadAppletFile" code="TestApplet.class" archive="FileUpload.jar" width="400" height="300"></applet>
<input type="button" name="button" onclick="uploadFileApp();" value="Button"/>
</body>
</html>

Applet Class:

public class TestApplet extends JApplet{
    public TestApplet() {
    }   
    public String FileCooserApp(){
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File file = chooser.getSelectedFile();
        String path = file.getAbsolutePath();
        return path;
    }   
 }

as I shown in my html file I used both way to access applet method but when i click on button nothing action going to perform. and console shows nothing.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Balasaheb
  • 625
  • 2
  • 12
  • 33
  • The access from Javascript to Java methods is not possible. Both languages use their own workingspace. (Java takes a seperate plugin) – Reporter Aug 17 '12 at 11:45
  • @reporter no, its possible to access applet method from javascript, I was able to access that method but I don't know whats going wrong here that I am unable to access that method. – Balasaheb Aug 17 '12 at 11:55
  • I looked up that in the Internet and apparently you're right. – Reporter Aug 17 '12 at 12:17
  • *"console shows nothing."* Make the console show something. -- Add `System.out.println("FileChooserApp invoked");` as the first line of the method. Flush the class cache, reload the applet and check the string prints in the console. Report back. – Andrew Thompson Aug 17 '12 at 20:39
  • @AndrewThompson applet loads successfully at first time when program run but when am trying to call same method it gives error java.security.AccessControlException: access denied (java.util.PropertyPermission user.dir read) at java.security.AccessControlContext.checkPermission(Unknown Source) at javax.swing.JFileChooser.getIcon(Unknown Source) so what should i do? – Balasaheb Aug 23 '12 at 13:50
  • Calling that method from JS takes some extra work. Calls to trusted methods need to be wrapped in an [`AccessController.doPrivileged(...)`](http://docs.oracle.com/javase/7/docs/api/java/security/AccessController.html#method_summary) method. – Andrew Thompson Aug 23 '12 at 14:25
  • @AndrewThompson I already written my code in AccessController.doPrivileged(...) but still same problem... – Balasaheb Aug 23 '12 at 14:28
  • Did you flush the cache? Put a `System.out.println("Test 1");` in the method to check you are seeing the new code. Are you prompted to accept the digitally signed code? – Andrew Thompson Aug 23 '12 at 14:30

1 Answers1

0

all in html

<object id="uploadAppletFile"
   classid="java:com/company/package/TestApplet.class"
   type="application/x-java-applet" 
   archive="FileUpload.jar"  
   height="0" width="0">
   <param name="code" value="com/company/package/TestApplet.class" /> 
   <param name="archive" value="FileUpload.jar" />
</object> 




<script type="text/javascript">
function uploadFileApp(){   
    uploadAppletFile.FileCooserApp();
}
</script>
Ilya
  • 29,135
  • 19
  • 110
  • 158
  • @answered I write a code which you have given, when i run a program applet loads successfully and filechooser function gets call from init() but when i am trying to call same method from javascript its throwing error java.security.AccessControlException: access denied (java.util.PropertyPermission user.dir read) at java.security.AccessControlContext.checkPermission(Unknown Source) at javax.swing.JFileChooser.getIcon(Unknown Source) So what should i do? – Balasaheb Aug 23 '12 at 13:46