From: Brandon P. <bp...@gm...> - 2010-09-30 19:22:08
|
Hi all, We are running a somewhat customized version of Jython and I am trying to dynamically load a jar from the file system and import a class. I do: sys.path.append(jar_file) import com.vmware.vim25.mo.ServiceInstance This throws an ImportError saying it cannot import the name ServiceInstance. However, if I do: sys.path.append(jar_file) import com.vmware.vim25.mo print dir(com.vmware.vim25.mo) It will list ServiceInstance as available....I am thoroughly confused as to how this is possible. I am sure it has to do with how we are running jython since this works using a regular jython script, but I am not sure what to look for. Is there some property we arent setting when we initialize the jython interpreter instance? is there some other way to add the jar or notify the package manager of the new jar and process it like it does on startup? I realize this may be a long shot without knowing exactly how we are using jython but thanks for any help, -Brandon |
From: Brandon P. <bp...@gm...> - 2010-09-30 19:40:32
|
So I was trying different things and I realized that actually doing sys.path.append(jar_file) doesnt work in this case at all. I actually was messing around with different things and did Py.getSystemState().packageManager.addJar(jar_file, True) and it cached the package (which made it appear that sys.path.append was working)... But I guess addJar() doesn't actually help which causes the symptoms below, so the question still stands, if adding to sys.path does not work, how else can I import a jar file dynamically? -Brandon On Thu, Sep 30, 2010 at 1:21 PM, Brandon Pedersen <bp...@gm...> wrote: > Hi all, > > We are running a somewhat customized version of Jython and I am trying > to dynamically load a jar from the file system and import a class. I > do: > > sys.path.append(jar_file) > import com.vmware.vim25.mo.ServiceInstance > > This throws an ImportError saying it cannot import the name > ServiceInstance. However, if I do: > > sys.path.append(jar_file) > import com.vmware.vim25.mo > print dir(com.vmware.vim25.mo) > > It will list ServiceInstance as available....I am thoroughly confused > as to how this is possible. > > I am sure it has to do with how we are running jython since this works > using a regular jython script, but I am not sure what to look for. Is > there some property we arent setting when we initialize the jython > interpreter instance? is there some other way to add the jar or notify > the package manager of the new jar and process it like it does on > startup? > > I realize this may be a long shot without knowing exactly how we are > using jython but thanks for any help, > > -Brandon > |
From: Alex G. <ale...@ne...> - 2010-09-30 19:44:20
|
30.9.2010 22:21, Brandon Pedersen kirjoitti: > Hi all, > > We are running a somewhat customized version of Jython and I am trying > to dynamically load a jar from the file system and import a class. I > do: > > sys.path.append(jar_file) > import com.vmware.vim25.mo.ServiceInstance > > This throws an ImportError saying it cannot import the name > ServiceInstance. However, if I do: > > sys.path.append(jar_file) > import com.vmware.vim25.mo > print dir(com.vmware.vim25.mo) > > It will list ServiceInstance as available....I am thoroughly confused > as to how this is possible. > > I am sure it has to do with how we are running jython since this works > using a regular jython script, but I am not sure what to look for. Is > there some property we arent setting when we initialize the jython > interpreter instance? is there some other way to add the jar or notify > the package manager of the new jar and process it like it does on > startup? > > I realize this may be a long shot without knowing exactly how we are > using jython but thanks for any help, What happens if you do "from com.vmware.vim25.mo import ServiceInstance"? That's how I usually import java classes. > -Brandon > > ------------------------------------------------------------------------------ > Start uncovering the many advantages of virtual appliances > and start using them to simplify application deployment and > accelerate your shift to cloud computing. > http://p.sf.net/sfu/novell-sfdev2dev > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users |
From: Brandon P. <bp...@gm...> - 2010-09-30 20:01:21
|
2010/9/30 Alex Grönholm <ale...@ne...>: > What happens if you do "from com.vmware.vim25.mo import ServiceInstance"? Same thing. If I did Py.getSystemState().packageManager.addJar() it will say 'cannot import name 'ServiceInstance'' If I just try adding the jar to sys.path it will just say 'no module named vmware' -Brandon |
From: Brandon P. <bp...@gm...> - 2010-09-30 20:02:24
|
I also had a look at http://www.jython.org/jythonbook/en/1.0/appendixB.html#working-with-classpath and the classPathHacker doesnt seem to do anything either 2010/9/30 Brandon Pedersen <bp...@gm...>: > 2010/9/30 Alex Grönholm <ale...@ne...>: >> What happens if you do "from com.vmware.vim25.mo import ServiceInstance"? > > Same thing. If I did Py.getSystemState().packageManager.addJar() it > will say 'cannot import name 'ServiceInstance'' > > If I just try adding the jar to sys.path it will just say 'no module > named vmware' > > -Brandon > |
From: Alex G. <ale...@ne...> - 2010-09-30 20:28:03
|
30.9.2010 23:01, Brandon Pedersen kirjoitti: > I also had a look at > http://www.jython.org/jythonbook/en/1.0/appendixB.html#working-with-classpath > and the classPathHacker doesnt seem to do anything either > You must be doing something else wrong. Importing dynamically from jar works fine for me (2.5.2b2 here): >>> import sys >>> sys.path.append('jfd-loader.jar') >>> from com.jformdesigner.runtime import FormCreator >>> FormCreator <type 'com.jformdesigner.runtime.FormCreator'> If I don't add the jar to sys.path: >>> from com.jformdesigner.runtime import FormCreator ImportError: No module named jformdesigner Did you also take into account that if the Java class has unsatisfied dependencies, it may cause the same exception to be raised? > 2010/9/30 Brandon Pedersen<bp...@gm...>: >> 2010/9/30 Alex Grönholm<ale...@ne...>: >>> What happens if you do "from com.vmware.vim25.mo import ServiceInstance"? >> Same thing. If I did Py.getSystemState().packageManager.addJar() it >> will say 'cannot import name 'ServiceInstance'' >> >> If I just try adding the jar to sys.path it will just say 'no module >> named vmware' >> >> -Brandon >> |
From: Brandon P. <bp...@gm...> - 2010-09-30 23:02:40
|
2010/9/30 Alex Grönholm <ale...@ne...>: > 30.9.2010 23:01, Brandon Pedersen kirjoitti: >> I also had a look at >> http://www.jython.org/jythonbook/en/1.0/appendixB.html#working-with-classpath >> and the classPathHacker doesnt seem to do anything either >> > You must be doing something else wrong. Importing dynamically from jar > works fine for me (2.5.2b2 here): > > >>> import sys > >>> sys.path.append('jfd-loader.jar') > >>> from com.jformdesigner.runtime import FormCreator > >>> FormCreator > <type 'com.jformdesigner.runtime.FormCreator'> > > If I don't add the jar to sys.path: > >>> from com.jformdesigner.runtime import FormCreator > ImportError: No module named jformdesigner Right, like I said, we are using a somewhat customized version of Jython 2.5.1....doing it from jython directly works OK....we are running it as sort of an agent process where we send requests to our agent process which launches a JythonInterpreter instance and runs the script. Perhaps there is something wrong with the way we are initializing the interpreter? > Did you also take into account that if the Java class has unsatisfied > dependencies, it may cause the same exception to be raised? Hmm, didn't think about that...perhaps there is something I am missing..but I dont believe so, I am trying to use the vijava project libraries from http://vijava.sourceforge.net/ and he says all that is needed is the vijava jar and the domj4 jar and I am adding both. I was able to get it somewhat working by doing the following (its a little more complex but this is the cut down version): jars = ['vijava2120100824.jar','dom4j-1.6.1.jar'] classloader = URLClassLoader([File(path).toURL() for path in jars], ClassLoader.getSystemClassLoader()) ServiceInstance = classloader.loadClass('com.vmware.vim25.mo.ServiceInstance') Not exactly a nice solution so if there are any other ideas, let me know |
From: Alex G. <ale...@ne...> - 2010-09-30 23:11:29
|
1.10.2010 2:02, Brandon Pedersen kirjoitti: > 2010/9/30 Alex Grönholm<ale...@ne...>: >> 30.9.2010 23:01, Brandon Pedersen kirjoitti: >>> I also had a look at >>> http://www.jython.org/jythonbook/en/1.0/appendixB.html#working-with-classpath >>> and the classPathHacker doesnt seem to do anything either >>> >> You must be doing something else wrong. Importing dynamically from jar >> works fine for me (2.5.2b2 here): >> >> >>> import sys >> >>> sys.path.append('jfd-loader.jar') >> >>> from com.jformdesigner.runtime import FormCreator >> >>> FormCreator >> <type 'com.jformdesigner.runtime.FormCreator'> >> >> If I don't add the jar to sys.path: >> >>> from com.jformdesigner.runtime import FormCreator >> ImportError: No module named jformdesigner > Right, like I said, we are using a somewhat customized version of > Jython 2.5.1....doing it from jython directly works OK....we are > running it as sort of an agent process where we send requests to our > agent process which launches a JythonInterpreter instance and runs the > script. Perhaps there is something wrong with the way we are > initializing the interpreter? > It would seem the you have a classloader hierarchy problem here. I'm afraid I can't help you any further since I'm unfamiliar with your setup. >> Did you also take into account that if the Java class has unsatisfied >> dependencies, it may cause the same exception to be raised? > Hmm, didn't think about that...perhaps there is something I am > missing..but I dont believe so, I am trying to use the vijava project > libraries from http://vijava.sourceforge.net/ and he says all that is > needed is the vijava jar and the domj4 jar and I am adding both. > > I was able to get it somewhat working by doing the following (its a > little more complex but this is the cut down version): > > jars = ['vijava2120100824.jar','dom4j-1.6.1.jar'] > classloader = URLClassLoader([File(path).toURL() for path in jars], > ClassLoader.getSystemClassLoader()) > ServiceInstance = classloader.loadClass('com.vmware.vim25.mo.ServiceInstance') > > Not exactly a nice solution so if there are any other ideas, let me know > > ------------------------------------------------------------------------------ > Start uncovering the many advantages of virtual appliances > and start using them to simplify application deployment and > accelerate your shift to cloud computing. > http://p.sf.net/sfu/novell-sfdev2dev > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users |
From: Alan K. <jyt...@xh...> - 2010-10-01 17:26:24
|
[Brandon] > We are running a somewhat customized version of Jython and I am trying > to dynamically load a jar from the file system and import a class. I > do: > > sys.path.append(jar_file) > import com.vmware.vim25.mo.ServiceInstance > > This throws an ImportError saying it cannot import the name > ServiceInstance. Two suggestions. 1. What is the access modifier on ServiceInstance? Is it public, protected or private? 2. With the recent classloader changes, I'm not sure if this is still necessary, but in order to use jars dynamically, it used to be case that adding the jar to sys.path was not enough. You also have to to sys.add_package(packageName). For example, for your requirement, you would need to do import sys sys.path.append(jar_file) sys.add_package("com.vmware.vim25.mo") Or perhaps the root of the class hierarchy, i.e. sys.add_package("com.vmware") or sys.add_package("com.vmware.vim25") This issue is documented in the modjy documentation, under "Using external java packages" http://opensource.xhaus.com/projects/modjy/wiki/ModjyConfiguration HTH, Alan. |
From: Brandon P. <bp...@gm...> - 2010-10-01 21:07:46
|
On Fri, Oct 1, 2010 at 11:26 AM, Alan Kennedy <jyt...@xh...> wrote: > Two suggestions. > > 1. What is the access modifier on ServiceInstance? Is it public, > protected or private? public > For example, for your requirement, you > would need to do > > import sys > sys.path.append(jar_file) > sys.add_package("com.vmware.vim25.mo") Still gives me 'cannot import name ServiceInstance' I think Alex was right and we may have a classloader issue. I know that we are messing with the class loader a little bit...will have to do some more investigation with that Thanks |
From: Brandon P. <bp...@gm...> - 2010-10-03 03:43:11
|
Looks like the problem was a classloader issue. We were changing the classloader of the interpreter which was causing this error (still no idea why...cant find out why that was done, maybe something to do with the fact that we were using jython 2.1 not too long ago and it could have needed that). Once we removed that it started working just fine. Thanks, -Brandon On Fri, Oct 1, 2010 at 3:07 PM, Brandon Pedersen <bp...@gm...> wrote: > On Fri, Oct 1, 2010 at 11:26 AM, Alan Kennedy <jyt...@xh...> wrote: >> Two suggestions. >> >> 1. What is the access modifier on ServiceInstance? Is it public, >> protected or private? > > public > >> For example, for your requirement, you >> would need to do >> >> import sys >> sys.path.append(jar_file) >> sys.add_package("com.vmware.vim25.mo") > > Still gives me 'cannot import name ServiceInstance' > > I think Alex was right and we may have a classloader issue. I know > that we are messing with the class loader a little bit...will have to > do some more investigation with that > > Thanks > |