How to add (read) java.io.FilePermission to my /usr/share/java/mysql-connector-java.jar for a java applet using MySQL on Ubuntu?
I am trying to make my java program work in a web-browser. The java program is using a MySQL database and I have successfully turned it into a java-applet that works just as it should in the Eclipse IDE. However, when I try to view the applet in a browser it doesnt work, and when i try to run it with appletviewer, i get the error:
"Exception in thread "Game thread" java.security.AccessControlException: access denied (java.io.FilePermission /usr/share/java/mysql-connector-java.jar read)"
I know that it is better to put a servlet between the applet and the server for safety, but right now I just want the applet to work on my computer when i run it with appletviewer and in the browser.
I have searched around on forums and I have checked the following common issues:
1) I have set the classpath correctly (as it works when i dont run it as an applet, and since i dont get a classNotFound error),
2) I think I have the html file correct since it loads the graphics and the com.mysql.jdbc.Driver correctly, it looks like this:
<HTML>
<HEAD>
<TITLE> MyAppletTitle </TITLE>
</HEAD>
<CENTER>
<APPLET
code="appletname"
archive="/usr/share/java/mysql-connector-java.jar"
WIDTH=1300 HEIGHT=700>
</APPLET>
</CENTER>
</HTML>
3) I have tried all kinds of variations in the java.policy file in "/etc/java-6-openjdk/security" -folder and it reacts to what i write in there so i know its the right java version (not java-7...), but no matter what permissions i grant, i get the same access denied. I have tried:
grant codeBase "file:/usr/share/java/*" {
permission java.security.AllPermission;
};
grant codeBase "file:/usr/share/java/mysql-connector-java.jar" {
permission java.security.AllPermission;
};
grant codeBase "file:/usr/share/java/mysql-connector-java.jar" {
permission java.util.PropertyPermission "java.home", "read";
};
grant codeBase "file:/home/myusername/PathToApplet/*" {
permission java.security.AllPermission;
};
grant codeBase "file:/home/myusername/PathToApplet/*" {
permission java.util.PropertyPermission "java.home", "read";
};
4) I have tried to sign the .jar files that are used, but here i run into some problems: -I cannot sign jar files located in /usr/share/java/ (and i shouldnt have to i think?), -My applet is not a runnable jar file so Im not sure what good it does to sign the extracted .jar file that I create using Eclipse.
5) I have checked the MySQL database grants (privileges) to the database that I create and it is as it should be, since it works in eclipse and when i dont run the java program as an applet (but as JFrame), unless the appletviewer tries to connect as something else than what i tell the program to connect as on some weird port or something?
6) I have seen that you can wrap your code with some doPrivileged stuff, but I dont know how to do this. The access denied error occurs when I try to load the com.mysql.jdbc.Driver with the command Class.forName(dbClassName); The codesnippet looks like this:
private static int[] GetWordIDs(int[] userinput) throws ClassNotFoundException, SQLException {
String dbClassName = "com.mysql.jdbc.Driver";
String url ="jdbc:mysql://127.0.0.1/DataBase1";
// Problems here, surround with doPrivileged something??
Class.forName(dbClassName);
.
.
.
How would I wrap some doPrivileged thingy around this?
7) Eclipse is doing something that makes the applet work without any java.io.Filepermission problems, and ive looked at the .classpath and .project files that it creates, but I have no clue what to do with these, maybe I have to use these somehow?
8) I have even copied the mysql-connector-java-5.1.16-bin.jar file to my classpath but that also didnt help.
I've banged my head on these problems for a while now and I would appreciate any help you can give!