Call Java Methods from Python Script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hofsoc20
    New Member
    • Feb 2009
    • 2

    Call Java Methods from Python Script

    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
    Code:
    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
    Code:
    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...
    Last edited by bvdet; Mar 15 '09, 11:32 PM. Reason: Add code tags
  • kaer
    New Member
    • Mar 2009
    • 1

    #2
    Originally posted by hofsoc20
    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:\Pr ogram Files\Java\jdk1 .6.0_12\jre\bin \client", "-ea", "-Djava.class.pat h=%s" % classpath)
    com =JPackage("Call JavaFromPython" )
    jp = com.Jpype1()
    jp.printArgumen t("XXX")
    shutdownJVM()

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

    Jpype1.class

    package CallJavaFromPyt hon;

    public class Jpype1 {

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

    public void printArgument(S tring arg){
    System.out.prin tln(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.

    ;-)

    Comment

    Working...