1

I recently wanted to create my own copy of GregorianCalendar to add some System.out.println to help me understand how it works. I copied the source and renamed the class, but it wouldn't compile because the source references package-private names in other classes. So I tried adding package java.util; to the source and putting it in a java\util subdirectory of my working directory. I got the program to compile, but it is throwing java.lang.SecurityException: Prohibited package name: java.util.

After reading through the tutorial on Security, I tried running policytool to give myself permission to define a class in java.util. I now have a file policy1 that looks like:

grant {
  permission java.lang.RuntimePermission "defineClassInPackage.java.util";
};

(plus the DO NOT EDIT comment). This is in my work directory, same as my test program. I ran (in a Command Prompt window):

java -Djava.security.manager -Djava.security.policy=policy1 Test35

But it's still not working.

Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.util
    at java.lang.ClassLoader.preDefineClass(ClassLoader.java:659)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:758)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at Test35.main(Test35.java:13)

Did I do something wrong, or miss a step?

ajb
  • 31,309
  • 3
  • 58
  • 84
  • You might have better luck loading it in an IDE, and just setting breakpoints. – Gus May 28 '14 at 18:50
  • @Gus There are some debugging jobs for which debuggers are not appropriate. Say I wanted to run a program to call one of the methods a few hundred times. What would you prefer to do ... gather a few hundred lines of output in a file that could perhaps be processed with a script, or sit there waiting to hit Continue Continue Continue hundreds of times in a debugger? Anyway, I no longer actually need to look at the class, but I want to know how to get this kind of thing to work for next time. – ajb May 28 '14 at 18:55
  • why does it need to be in java.util? If its your own copy of the class you should create it in your own package. – yate May 28 '14 at 19:01
  • @yate As I explained in my question ... if it's not in `java.util` then it won't compile because it relies on symbols with package visibility. – ajb May 28 '14 at 19:07
  • Even then, I'm pretty sure you can set up breakpoint actions that also log in eclipse. http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Ftasks%2Fcdt_t_add_brk_action.htm – Gus May 29 '14 at 00:25
  • ever get a solution to this? – adapt-dev Sep 08 '15 at 04:17
  • 1
    @adapt-dev no, I didn't. – ajb Sep 08 '15 at 14:52

0 Answers0