Connecting Tech Pros Worldwide Help | Site Map

Call Java Methods from Python Script

Newbie
 
Join Date: Feb 2009
Posts: 2
#1: Feb 18 '09
Hi,

I am trying to call java methods from a python script. Jython is not an option. I have looked at using Jpype. Below is my code thus far:

Jpype.py
Expand|Select|Wrap|Line Numbers
  1. from jpype import *
  2.  
  3. startJVM("C:\Program Files\Java\jdk1.6.0_12\jre\bin\client", "-ea", "-Djava.class.path=%s" % classpath)
  4. com =JPackage("CallJavaFromPython")
  5. jp = com.Jpype1()
  6. jp.printArgument("XXX")
  7. shutdownJVM()
  8.  
================================================== =

Jpype1.class
Expand|Select|Wrap|Line Numbers
  1. package CallJavaFromPython;
  2.  
  3. public class Jpype1 {
  4.  
  5. public static void main(String args[]){
  6. System.out.println(args[0]);
  7. }
  8.  
  9. public void printArgument(String arg){
  10. System.out.println(arg);
  11. }
  12.  
  13. }
  14.  
================================================== =

Error message i am getting when I try to run the python script is :

NameError: name 'startJVM' is not defined


has anyone come across this before?

Thanks in advance...
Newbie
 
Join Date: Mar 2009
Posts: 1
#2: Mar 15 '09

re: Call Java Methods from Python Script


Quote:

Originally Posted by hofsoc20 View Post

Hi,

I am trying to call java methods from a python script. Jython is not an option. I have looked at using Jpype. Below is my code thus far:

Jpype.py

from jpype import *

startJVM("C:\Program Files\Java\jdk1.6.0_12\jre\bin\client", "-ea", "-Djava.class.path=%s" % classpath)
com =JPackage("CallJavaFromPython")
jp = com.Jpype1()
jp.printArgument("XXX")
shutdownJVM()

================================================== =

Jpype1.class

package CallJavaFromPython;

public class Jpype1 {

public static void main(String args[]){
System.out.println(args[0]);
}

public void printArgument(String arg){
System.out.println(arg);
}

}

================================================== =

Error message i am getting when I try to run the python script is :

NameError: name 'startJVM' is not defined


has anyone come across this before?

Thanks in advance...

It's probably because you named your own program Jpype.py which is not a good idea. Thus the line:

from jpype import *

does not do what you expect.

;-)
Reply