-->

Unable to make an applet work in IntelliJ

2020-08-04 04:35发布

问题:

I am a complete noob. I am just learning to program using java. I followed a tutorial and created the following applet in IntelliJ Idea in my brother's computer:

import javax.swing.JApplet;
import java.awt.Graphics;
public class MyFirstApplet extends JApplet {
    public void paint(Graphics g) {
        g.drawString("To climb a ladder, start at the bottom rung.", 20, 90);
    }
}

When i try to run this , the IDE shows:

"Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\Users\Abhirup_2\AppData\Local\Temp\AppletPage6525114136327939228.html" "read")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:372)
    at java.security.AccessController.checkPermission(AccessController.java:559)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at java.lang.SecurityManager.checkRead(SecurityManager.java:888)
    at java.io.File.isDirectory(File.java:838)
    at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:82)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
    at sun.applet.AppletViewer.parse(AppletViewer.java:1103)
    at sun.applet.AppletViewer.parse(AppletViewer.java:1074)
    at sun.applet.Main.run(Main.java:156)
    at sun.applet.Main.main(Main.java:98)
    at sun.applet.AppletViewer.main(AppletViewer.java:1257)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Process finished with exit code 1

However it runs well in my friend's computer in eclipse. Please help.

UPDATE: There is a single account and that is my brother's acoount. I am using that and being the only account it should have admiistrator rights if I am not wrong.

回答1:

You need to use an applet policy file which grants permission to run as a local applet and set it in your Run/Debug configuration in the "Policy File" field.

It should have at minimum the following:

grant {
    permission java.security.AllPermission;
}

Or you can use the default one provided by IntelliJ in the application bin directory called appletviewer.policy