我有一个使用早期版本的Java 7运行良好的Java Applet但现在在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策略文件以允许该权限但它似乎没有帮助。在 javaws.policy &我的JDK和JRE的 java.policy 我在' grant {'和'strong> grant codeBase'文件的顶部添加了以下内容:$ {{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>
我在更新清单文件的Ant脚本部分内部有这个:
<manifest>
<attribute name="Permissions" value="all-permissions"/>
<attribute name="Main-Class" value="myProgram.MainFrame"/>
</manifest>
有谁知道我可以摆脱这个权限问题,以便我的代码可以获得必要的配置文件并运行?
谢谢!
答案 0 :(得分:2)
在Java 8中,您确实需要使用有效证书对所有JAR进行签名。你说你是用ANT做的,但如果他们真的签了,请查看下面的命令。
jarsigner -verify yourjar.jar
尝试为清单添加更多属性。
<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
}
}