我有一个 Java Applet,它使用 Java 7 的早期版本运行良好,但现在在 Java 8 上我遇到了权限问题。这是我遇到的具体错误:
May 28, 2015 12:57:15 PM [com.sun.xml.internal.ws.assembler.MetroConfigLoader] init
WARNING: MASM0010: Unable to unmarshall metro config file from location [ jar:file:/C:/Program%20Files/Java/jre1.8.0_45/lib/resources.jar!/com/sun/xml/internal/ws/assembler/jaxws-tubes-default.xml ]
java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "accessDeclaredMembers")
似乎由于权限问题,它无法访问 xml 文件来加载配置。我已经尝试更新我的 java 策略文件以允许该权限,但它似乎没有帮助。在我的 JDK 和 JRE 的 javaws.policy 和 java.policy 中,我在“grant {”和top inside 'grant codeBase "file:${{java.ext.dirs}}/*"{':
permission java.lang.RuntimePermission "accessDeclaredMembers";
虽然上面的行似乎没有任何区别。
我还使用 Ant 构建脚本签署我的主 JAR 和所有支持库 JAR。这是我的 Ant 的一部分
<mkdir dir="H:/Eclipse Projects/myProgram/dist/myProgram_lib"/>
<copy file="D:/Users/user/.ivy2/cache/org.apache.wink/wink-client/jars/wink-client-1.1-incubating.jar" todir="H:/Eclipse Projects/myProgram/dist/myProgram_lib"/>
<copy file="D:/Users/user/.ivy2/cache/org.apache.wink/wink-common/jars/wink-common-1.1-incubating.jar" todir="H:/Eclipse Projects/myProgram/dist/myProgram_lib"/>
<copy file="D:/Users/user/.ivy2/cache/org.slf4j/slf4j-api/jars/slf4j-api-1.5.11.jar" todir="H:/Eclipse Projects/myProgram/dist/myProgram_lib"/>
<copy file="D:/Users/user/.ivy2/cache/org.slf4j/slf4j-jdk14/jars/slf4j-jdk14-1.5.11.jar" todir="H:/Eclipse Projects/myProgram/dist/myProgram_lib"/>
<copy file="D:/Users/user/.ivy2/cache/javax.xml.bind/jaxb-api/jars/jaxb-api-2.1.jar" todir="H:/Eclipse Projects/myProgram/dist/myProgram_lib"/>
<copy file="D:/Users/user/.ivy2/cache/javax.xml.stream/stax-api/jars/stax-api-1.0-2.jar" todir="H:/Eclipse Projects/myProgram/dist/myProgram_lib"/>
<copy file="D:/Users/user/.ivy2/cache/javax.activation/activation/jars/activation-1.1.jar" todir="H:/Eclipse Projects/myProgram/dist/myProgram_lib"/>
<copy file="D:/Users/user/.ivy2/cache/com.sun.xml.bind/jaxb-impl/jars/jaxb-impl-2.1.4.jar" todir="H:/Eclipse Projects/myProgram/dist/myProgram_lib"/>
<copy file="D:/Users/user/.ivy2/cache/javax.ws.rs/jsr311-api/jars/jsr311-api-1.1.jar" todir="H:/Eclipse Projects/myProgram/dist/myProgram_lib"/>
<copy file="D:/Users/user/.ivy2/cache/commons-codec/commons-codec/jars/commons-codec-1.3.jar" todir="H:/Eclipse Projects/myProgram/dist/myProgram_lib"/>
<copy file="D:/Users/user/.ivy2/cache/xpp3/xpp3_min/jars/xpp3_min-1.1.4c.jar" todir="H:/Eclipse Projects/myProgram/dist/myProgram_lib"/>
<signjar alias="xxxxxxxxxxxxxxxxx" keystore="xxxxxxxxxxxxxx" storepass="xxxxxx" lazy="true">
<path>
<fileset dir="dist" includes="**/*.jar" />
</path>
</signjar>
我在更新 list 文件的 Ant 脚本部分中得到了这个:
<manifest>
<attribute name="Permissions" value="all-permissions"/>
<attribute name="Main-Class" value="myProgram.MainFrame"/>
</manifest>
有谁知道我可以做什么来摆脱这个权限问题,以便我的代码可以获得必要的配置文件并运行?
谢谢!
最佳答案
在 Java 8 中,您确实需要使用有效证书对所有 JAR 进行签名。你是说你正在用 ANT 做这件事,但是请用下面的命令检查它们是否真的被签名了。
jarsigner -verify yourjar.jar
尝试向 list 中添加更多属性。
<codebase>http://location.of.your.jar/</codebase>
<permissions>all-permissions</permissions>
<Application-Library-Allowable-Codebase>http://location.of.your.jar/</Application-Library-Allowable-Codebase>
<Manifest-Version>1.0</Manifest-Version>
<Implementation-Title>App Name</Implementation-Title>
<Implementation-Version>0.1.0</Implementation-Version>
<Application-Name></Application-Name>
<Created-By>1.8.0_45</Created-By>
<Main-Class>package.YourClass</Main-Class>
<mode>development (or production)</mode>
<url>url of the application</url>
除此之外,您还需要添加一些与安全相关的代码才能正常工作,请查看下面的示例:
package io.github.ulymarins;
import java.applet.Applet;
import java.security.AccessController;
import java.security.PrivilegedAction;
/**
*
* @author Ulysses Marins <marins.ulysses@gmail.com>
*/
public final class TestApplet extends Applet
{
private static final long serialVersionUID = 1L;
String ret;
@SuppressWarnings({ "rawtypes", "unchecked" })
public String signFile(final String pmessage)
{
AccessController.doPrivileged(new PrivilegedAction()
{
public Object run()
{
try
{
String sl = "{\"success\":true," + "\"message\":\"" + pmessage + "\"}";
ret = sl;
}
catch (Exception e)
{
String sl = "{\"success\":false," + "\"message\":\"" + e.getMessage() + "\"}";
ret = sl;
System.out.println(sl);
}
return null;
}
});
return ret;
}
public void init(){
// Here you can put the code which will be executed when the applet starts
}
public void destroy(){
// Here you can put the code which will be executed when the applet stops
}
}
关于Java 8 Applet 问题 ("java.lang.RuntimePermission" "accessDeclaredMembers"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30515722/