拒绝访问 ("java.io.FilePermission" "execute")

access denied ("java.io.FilePermission" "execute")

我是beginner.it是我写的第一个小程序

我想要 运行 带有小程序的 exe 应用程序

java代码

 package appletexample;

import java.io.*;
import java.awt.*;
import java.applet.Applet;

public class Welcome extends Applet {

    public void init() {
        String execommand = "C:\windows\notepad.exe" ;
        try {
            Process proc =  Runtime.getRuntime().exec(execommand) ;
        }
        catch(IOException ieo) {
            System.out.println("Problem starting " + execommand) ;
        }
    }
} 

java.policy.applet

grant {
  permission java.security.AllPermission;
};

i 运行 eclipse Run As->Java Applet 中的这段代码有效并打开了 NotePade 但是当 Export->Jar File(with .classPath,.project,java.policy.applet) 并在

中使用

Html

<applet archive="test.jar" code="appletexample/Welcome.class"  width=550 height=300>

在 firefox 中说错误访问被拒绝 ("java.io.FilePermission" "execute")? 如何解决这个问题? download my java and Html code

作为初学者,您应该从简单得多的东西开始。当您使用 Applets 时,并非所有安全规则都适用。但是当你来到现实世界(在你的情况下是浏览器,或者换句话说,沙箱)时,安全规则是有效的,以防止你的代码损害主机。

您在做什么 - 当客户端使用 Applet 打开您的网页时,您是 运行 客户端计算机上的某个程序。那是什么病毒。人们不会愿意的。

当然,您可以使用 Signed Applet approach or other ways to run program on another computer, but is it your goal? If it is to learn basics, then run easy stuff. Eventually, you will understand JNLP (Java Web Start) 和其他对您和您的客户有用的方法。

我假设你只是想练习如何编写一个小程序。 出于开发目的,您可以创建一个密钥库,然后使用它来签署您的 applet.jar.

开始:开始菜单 > 执行 > cmd.exe

输入:

cd /
keytool -genkey -dname "cn=CN, ou=OU, o=O, l=L, st=ST, c=C" -alias mykey -keypass mypass -keystore mystore -validity 3650 -storepass mypass
jarsigner -keystore c:\mystore -storepass mypass C:\path\to\applet.jar mykey

然后:

刷新您的 HTML 页面。