Unable to reference sun.*

Andrew John Hughes gnu_andrew at member.fsf.org
Sat May 10 15:31:45 PDT 2008

On 10/05/2008, Brendon McLean <java.net at twistedprotein.com> wrote:
> Hi,
> I've been developing an application on the Mac, but the target audience will
> be cross platform.  I've just discovered that the application doesn't
> compile on either Windows or Linux on Java 6.  The compiler is complaining
> that one of the sun.* package I'm importing doesn't exist.  I'm very well
> aware that linking to these packages is strongly discouraged, but I was
> wondering whether perhaps it is now enforced?  I have the following file:
> import sun.reflect.misc.MethodUtil;
> public class Test {
>  public static void main(String[] args) throws NoSuchMethodException {
>  MethodUtil.getMethod(Test.class, "main", new Class[]{String[].class});
>  }
> }
> This file will:
> * Compile on all platforms under Java 5
> * Compile on all platforms when compiled in IntelliJ IDEA
> * Compile on Java 6 using javac on the Mac.
> * Fail to compile on Linux and Windows using Java 6 javac.
> The package is located in classes.jar on the Mac and in rt.jar on all other
> platforms.  In other words, this class should compile without any classpath
> argument.  Also of interest, is that referencing classes in the com.sun.*
> packages will work but generate a warning.  I can only conclude that there
> is something about the Java 6 compiler that is actively preventing me from
> linking to sun.* packages.
> Is this true?  Or have I missed something so obvious I'm going to have to
> give up programming and pursue farming?
> Brendon McLean.

Not only is this non-portable and thus contrary to your goals, but the
correct way is shorter:

Test.class.getMethod("main", String[].class);

so why do it at all???
Andrew :-)

Support Free Java!
Contribute to GNU Classpath and the OpenJDK

PGP Key: 94EFD9D8 (http://subkeys.pgp.net)
Fingerprint: F8EF F1EA 401E 2E60 15FA  7927 142C 2591 94EF D9D8

More information about the compiler-dev mailing list