dcsimg
Class.forName() fails for sun.awt.windows.WPrintDialogPeer and sun.awt.windows.WEmbeddedPrintGraphics
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   A_G
Posted On:   Wednesday, June 6, 2001 01:15 AM

I am trying to create a Class Instance for WPrintDialogPeer and WEmbeddedPrintGraphics in the JDK1.2.2 rt.jar. But it throws up an exception. import java.lang.reflect.*; public class Temp{ public void check(){ try{ Class oClass = Class.forName("sun.awt.windows.WPrintDialogPeer"); System.out.println("Class Name is" + " "+oClass.getName()); } catch(Exception ex){ ex.printStackTrace(); } } public static void main(String[] args){ Temp otemp = new Temp(); otemp.check(); } } Stack Trace: Exception in thread "main" java.lang.IllegalAccessError: cannot acce   More>>

I am trying to create a Class Instance for WPrintDialogPeer and
WEmbeddedPrintGraphics in the JDK1.2.2 rt.jar. But it throws up an exception.

import java.lang.reflect.*;

public class Temp{
public void check(){
try{
Class oClass = Class.forName("sun.awt.windows.WPrintDialogPeer");
System.out.println("Class Name is" + " "+oClass.getName());
}
catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args){
Temp otemp = new Temp();
otemp.check();
}
}


Stack Trace:

Exception in thread "main" java.lang.IllegalAccessError: cannot access
superclas
s sun/awt/windows/WWindowPeer from class sun/awt/windows/WPrintDialogPeer
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:448)
at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:10
1)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:248)
at java.net.URLClassLoader.access$1(URLClassLoader.java:216)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:191)
at java.lang.ClassLoader.loadClass(ClassLoader.java:296)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:283)
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:124)
at Temp.class$(Temp.java:4)
at Temp.check(Temp.java:7)
at Temp.main(Temp.java:16)

   <<Less

Re: Class.forName() fails for sun.awt.windows.WPrintDialogPeer and sun.awt.windows.WEmbeddedPrintGraphics

Posted By:   Finlay_McWalter  
Posted On:   Friday, June 15, 2001 05:17 PM

Interesting question!


I tried the same thing on JDK1.4 (win32) and saw roughly the same exception. The reason for your problem, and the solution, requires us to explore the murky depths of the AWT toolkit...


The proximal problem your seeing is that class WPrintDialogPeer isn't loaded, so calling classForName on it makes the classloader go off and load it. The problem you're seeing is that loading the class fails. In your case it's a security exception, and on JDK1.4 it's an unsatisfiedLinkError - but the root cause is the same.


I believe the underlying problem is that a library (probably a DLL, but theoretically it could just be an external JAR) hasn't been loaded - so the JVM doesn't have the resources it needs to successfully load WPrintDialogPeer, and thus fails.


Now you're probably asking "but if I write a regular program that makes a print dialog, surely WPrintDialogPeer will be created - how come it isn't now?". The reason is that normal programs have (probably unbeknownst to the programmer) initialised the AWT toolkit, which makes sure that all the relevant local libraries are properly installed.


The toolkit is started the first time you create an AWT object. At this time the peerset is initialised, and (on most platforms) some local initialisation code is run. I believe that in Windows' case this includes loading a library. It's the lack of this library that's causing the load of WPrintDialogPeer to fail (as no non-naughty program would ever be touching peers before the toolkit was initialised).


So, the short answer is "you need to create an AWT object (anything will do) to make AWT initialise the peerset - then WPrintDialogPeer will load correctly".


I rewrote your example to do this:



import java.awt.Frame;

public class testclass {
public static final void main(String [] args) {
Frame f = new Frame(); // ADDED - starts peerset, toolkit.

try {
Class oClass = Class.forName("sun.awt.windows.WPrintDialogPeer");
System.out.println("Class Name is" + " " +oClass.getName());
}
catch (Exception ex){
ex.printStackTrace();
}
}
}


and it shows the correct result. Build it with the Frame constructor line commented out, and you'll see it fail.

About | Sitemap | Contact