0

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!

1 Answers1

0

java.security.AccessControlException: access denied (java.io.FilePermission /usr/share/java/mysql-connector-java.jar read

That means that you need to either:

  1. Add an AllPermission covering that file or directory to the .policy file that is active when the applet executes.
  2. Add an explicit java.io.FilePermission read ditto.

    And it doesn't mean anything else. Most of your post is therefore irrelevant: specifically, your points 1, 2, 5, 6, and 7.

  3. Sign the JAR file. This is by far the best solution. It solves that problem and many others. The fact that your 'applet is not a runnable jar file' has nothing to do with whether you should sign it.
user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks for your answer, but as I described in my post, I have done point 1, 2 and 3 that you suggest, and it doesnt help or Im somehow doing it wrong. Therefore I have tried points 1,2,5,6 and 7 that I described in my post, so I fail to understand the significance of your answer in general. Sorry, I dont mean to be rude, but if you could be more specific it might help me? – Rob Coeglin Aug 03 '13 at 15:19
  • Maybe I wasnt clear on the signing part, as I could only extract all referenced libs from eclipse into a jar file that I COULD sign, the mysql-connector-java.jar file cant be signed apparantly, not even when i try to sudo sign it, but I should not have to since that jar file should be auto trusted I THINK? – Rob Coeglin Aug 03 '13 at 15:25