Issue228540

classification
Title: Importing java classes, constructing java classes
Type: Severity: normal
Components: Core Versions:
Milestone:
process
Status: closed Resolution: later
Dependencies: Superseder:
Assigned To: Nosy List: bckfnn, pedronis
Priority: normal Keywords:

Created on 2001-01-12.11:23:33 by anonymous, last changed 2001-01-12.12:51:54 by pedronis.

Messages
msg253 (view) Author: Nobody/Anonymous (nobody) Date: 2001-01-12.11:23:33
In my app I don't use CLASSPATH much 'cause it gonna be somewhat big (about 50 jars)
Instead of this I use wildcard as argument to my app-launcher, which parses wildcard and finds every jar i need (builds net of ClassLoaders etc.) and Class.forName("anyClass"); works just fine in any piece of app.

Jython package manager(SysPackageManager.java) uses "java.class.path" property alot when huntin' for jars.
In my app-launcher expand-java.class.path code was added
someth' like that 

String currPath =  System.getProperty("java.class.path");
String separator = ";";
currPath += separator + "myPackage.jar";
System.setProperty("java.class.path", currPath);

After that modification alot of 
*sys-package-mgr*: processing new(modified) jar, 'X:\a\b\c.jar'
was shown at the Jython initialization /Py.initPython();/ assuming everithing Ok.

BUT!
"from a.b.c.d import myClass" - ImportError: cannot import name myClass
"from a.b.c.d import *" - Ok
"dir()" - names of classes with myClass among... Hm?????
"d = new myClass()" -
java.lang.NullPointerException
        at java.lang.Class.isAssignableFrom(Native Method)
        at org.python.core.PyJavaClass.init__class__(PyJavaClass.java:143)
        at org.python.core.PyJavaClass.init(PyJavaClass.java:214)
        at org.python.core.PyJavaClass.initLazy(PyJavaClass.java:78)
        at org.python.core.PyJavaClass.initialize(PyJavaClass.java:94)
        at org.python.core.PyJavaClass.initConstructors(PyJavaClass.java:629)
        at org.python.core.PyJavaClass.__call__(PyJavaClass.java:781)
        at org.python.core.PyObject.__call__(PyObject.java:260)
        at org.python.pycode._pyx4.f$0(<console>)
        at org.python.pycode._pyx4.call_function(<console>)
        at org.python.core.PyTableCode.call(PyTableCode.java:155)
        at org.python.core.Py.runCode(Py.java:1050)
        at org.python.core.Py.exec(Py.java:1071)
        at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:124)

When I put jar containing myClass.class in CLASSPATH explicitly - everithing perfect.

Try to put something like 

String currPath =  System.getProperty("java.class.path");
String separator = ";";
currPath += separator + "myPackage.jar";
System.setProperty("java.class.path", currPath);

at the beginning of org.python.util.jython and don't put myPackage.jar at CLASSPATH and HAVE FUN ;)

andrew@ibis.odessa.ua
msg254 (view) Author: Finn Bock (bckfnn) Date: 2001-01-12.12:12:17
The "*sys-package-mgr*: processing ..." are done only to support the dir() and "from .. import *" on java packages. The actual loading of the class is performed by a basic Class.forName(..).

So it is no surprise that dir() works, but that import doesn't.

However, this this more of a feature request than a bug. A task have created to describe the feature:

 "Loading java classes from other sources" (#24502)

The bug report will be closed, but rest assured that the feature is important and that Jython will improve in this area.

msg255 (view) Author: Samuele Pedroni (pedronis) Date: 2001-01-12.12:51:54
Hi.

Maybe this was not clear:
clearly the technique does not work in pure java (at least under sun jvm) either.
Consider:

* M.java
public class M {

 public static void main(String[] args) {
        
  String currPath =  System.getProperty("java.class.path");
  String separator = java.io.File.pathSeparator;
  currPath += separator + "jar1.jar";     

  System.out.println(currPath);

  System.setProperty("java.class.path", currPath); 

  try {                
   Class c = Class.forName("C");
  } catch(Throwable e) {
   System.err.println(e);
  }
                                                                                       
 }

}


* C.java (then compiled to C.class, which is put in jar1.jar")
public class C {}


Running java M one gets:
.:jar1.jar
java.lang.ClassNotFoundException: C


Setting classpath after java init has no effect (!).

regards, Samuele Pedroni.
History
Date User Action Args
2001-01-12 11:23:33anonymouscreate