Issue1692579

classification
Title: import java.awt fails in standalone mode if not bootstrapped
Type: Severity: normal
Components: Documentation Versions:
Milestone:
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: fwierzbicki Nosy List: cgroves, fwierzbicki, mr_tines, otmarhumbel
Priority: normal Keywords:

Created on 2007-04-01.22:36:31 by mr_tines, last changed 2008-09-13.21:40:45 by fwierzbicki.

Messages
msg1545 (view) Author: Mr. Tines (mr_tines) Date: 2007-04-01.22:36:31
Jython 2.2b1, installed in standalone mode i.e. all of /Lib inside the jar, and cachedir being skipped.

This affects more than just .awt in java; plus all of javax.

The following code works with Jython launched from the jar in the standard install:

import java.awt
f = java.awt.Frame()
f.title = "Hello AWT"
f.visible = True

but when run with Jython.jar from the standalone install the code gives

C:\jython2.2b1-standalone>java -jar jython.jar
Jython 2.2b1 on java1.6.0 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> import java.awt
Traceback (innermost last):
  File "", line 1, in ?
ImportError: no module named awt
>>> ^Z

WORKROUND

You need to nudge the system to do what caching would have done:-

import sys
sys.packageManager.makeJavaPackage("java.awt", "Window", None)
import java.awt
f = java.awt.Frame()
f.title = "Hello AWT"
f.visible = True

NOT WORKROUND

This does not work: 

try:
  import java.awt
except ImportError:
  import sys
  sys.packageManager.makeJavaPackage("java.awt", "Window", None)
  import java.awt

-- the second import fails as if the makeJavaPackage didn't stick (scoping effect?)


msg1546 (view) Author: Charlie Groves (cgroves) Date: 2007-04-23.04:21:07
Jython is only able to import Java packages directly with the cache system on.  That's essentially what the caching system is for.  It runs through all of the jars, determines which packages exist and what classes belong to them.  I don't think there's any way to determine java packages without that.
msg1547 (view) Author: Mr. Tines (mr_tines) Date: 2007-04-23.22:52:37
Waking this one back up to remark that adding a note about the necessary idiom somewhere in the documentation would not hurt -- reinventing the wheel and all that.
msg1548 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2007-04-24.17:18:04
Grabbing this to document.
msg1549 (view) Author: Oti Humbel (otmarhumbel) Date: 2007-06-12.15:58:38
The workaround (introduced after 2.2b1) would be here:

Jython 2.2b3248 on java1.6.0_01
Type "copyright", "credits" or "license" for more information.
>>> import java.awt.Frame
>>> f = java.awt.Frame()
>>> f
java.awt.Frame[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,title=,resizable,normal]
>>> 

Importing packages only ('import java.awt' style) is still sort of a russian roulette in standalone mode: If the package is already loaded into the JVM, it will succeed, otherwise not. But the situation is getting better with JDK 1.6:

Jython 2.2b3248 on java1.6.0_01
Type "copyright", "credits" or "license" for more information.
>>> import javax
>>> javax
<java package javax 1>
>>> 


as opposed to JDK 1.5:

Jython 2.2b3248 on java1.5.0_11
Type "copyright", "credits" or "license" for more information.
>>> import javax
Traceback (innermost last):
  File "<console>", line 1, in ?
ImportError: no module named javax
>>> 
msg3539 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2008-09-13.21:40:45
So as Oti noted, in standalone you must do full imports to succeed. 
Closing.
History
Date User Action Args
2008-09-13 21:40:45fwierzbickisetstatus: open -> closed
resolution: invalid -> wont fix
messages: + msg3539
2007-04-01 22:36:31mr_tinescreate