By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 935 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

problems using javaclass.classhook, HELP!

P: 1
I would like to import Java class in Python.

For this class:

SampleJavaClass.java
Expand|Select|Wrap|Line Numbers
  1. public class SampleJavaClass {
  2.     public static void main(String args[]) {
  3.          System.out.println("Hello world");
  4.     }
  5. }
  6.  
everything is OK. What I do is:
javac SampleJavaClass.java
java SampleJavaClass (works OK)

then I can access this class in Python, like this:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2.  
  3. import javaclass.classhook
  4. import SampleJavaClass
  5.  
  6. class JavaWrapper(object):
  7.     def __init__(self):
  8.         pass
  9.  
  10.     def main(self):
  11.         hook = SampleJavaClass.SampleJavaClass()
  12.         hook.main([])
  13.  
  14.  
  15. if __name__ == '__main__':
  16.     wrapper = JavaWrapper()
  17.     wrapper.main()
  18.  
but:

when my Java class (.java) uses some imports, lets say:

SampleJavaClass.java
Expand|Select|Wrap|Line Numbers
  1. import java.sql.*;
  2.  
  3. public class SampleJavaClass {
  4.     public static void main(String args[]) {
  5.         try {
  6.             // just for tests
  7.             throw new SQLException();
  8.         }
  9.         catch(SQLException e) {
  10.             System.err.println("Got exception: " + e.getMessage());
  11.         }
  12.         System.out.println("Hello world");
  13.     }
  14. }
  15.  
then after doing exactly the same thing as in the first case (the same python code) I get the following error:

-----
Traceback (most recent call last):
File "testjava.py", line 8, in <module>
import SampleJavaClass
File "/usr/lib64/python2.5/ihooks.py", line 407, in import_module
q, tail = self.find_head_package(parent, str(name))
File "/usr/lib64/python2.5/ihooks.py", line 444, in find_head_package
q = self.import_it(head, qname, parent)
File "/usr/lib64/python2.5/ihooks.py", line 500, in import_it
m = self.loader.load_module(fqname, stuff)
File "/usr/local/lib64/python2.5/site-packages/javaclass-0.1-py2.5.egg/javaclass/classhook.py", line 351, in load_module
obj = __import__(external_module_name, global_names, {}, [])
File "/usr/lib64/python2.5/ihooks.py", line 408, in import_module
m = self.load_tail(q, tail)
File "/usr/lib64/python2.5/ihooks.py", line 462, in load_tail
raise ImportError, "No module named " + mname
ImportError: No module named java.sql
-----

Can anyone help me? My CLASSPATH is set and points to the rt.jar, I also tried in python code to sys.path.append(<rt.jar>) but it did not help.

Thank You for any hints.
Marcin
Jan 12 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.