• 0

Importing .jar files in Jython (Mac OS)


Question

Hi,

 

It's rare that I get frustrated but I am not so sure why something so simple and mundane like below is just not working. I tried different .jar files but still no luck. I keep getting "ImportError: No module named *"

I am executing:

java -jar jython.jar Tester.py
My Output:

['/Users/<User>/<ProjectLocation>/<ProjectName>', '/Users/<User>/<ProjectLocation>/<ProjectName>/Lib', '/Users/<User>/<ProjectLocation>/<ProjectName>/jython.jar/Lib', '__classpath__', '__pyclasspath__/', '/Users/<User>/<ProjectLocation>/<ProjectName>/Lib/Beach.java']

Traceback (most recent call last):
  File "Tester.py", line 6, in <module>
    import Beach
ImportError: No module named Beach
 

Here is my project

 

/Tester.py

/jython.jar

/Lib/Beach.java

Tester.py

import sys
sys.path.append('/Users/<Username>/<ProjectLocation>/<ProjectName>/Lib/Beach.java')

print sys.path

import Beach
beach = Beach("Cocoa Beach","Cocoa Beach")
beach.getName()
print beach.getName()
 

Beach.java

public class Beach {

    private String name;
    private String city;


    public Beach(String name, String city){
        this.name = name;
        this.city = city;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

}
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

I figured out the problem. Jython has a bug that doesn't allow it to load .jar files using the sys.path.append() command. But from the command line it works fine :p

java -jar jython.jar -Dpython.path=/full/path/to/my.jar filename.py
  • Like 1
Link to comment
Share on other sites

This topic is now closed to further replies.