How to access Java classes using Python

Source: Internet
Author: User

I'm glad to introduce you to a Pyjnius project. This is a Python library that can be used on the desktop or Android to access Java classes.

Source code: github.com/kivy/pyjnius

Document: pyjnius.readthedocs.org

There are also some other libraries, such as JPype or Py4j, which are not very good in design and availability. Using Jython is not another option, because we want to use python to develop Android projects.

Now let me tell you how to simply use Pyjnius:

 
 
  1. >>> from jnius import autoclass  
  2. >>> Stack = autoclass('java.util.Stack')  
  3. >>> stack = Stack()  
  4. >>> stack.push('hello')  
  5. >>> stack.push('world')  
  6. >>> stack.pop()  
  7. 'world' 
  8. >>> stack.pop()  
  9. 'hello' 

In the above Code, we use the autoclass function to create a type proxy, which corresponds to all the methods and field attributes of the Java. util. Stack class in java.

OK. Maybe you want an Android-related example. Here:

 
 
  1. from jnius import autoclass  
  2. from time import sleep  
  3.    
  4. MediaRecorder = autoclass('android.media.MediaRecorder')  
  5. AudioSource = autoclass('android.media.MediaRecorder$AudioSource')  
  6. OutputFormat = autoclass('android.media.MediaRecorder$OutputFormat')  
  7. AudioEncoder = autoclass('android.media.MediaRecorder$AudioEncoder')  
  8.    
  9. # Record the Microphone with a 3GP recorder  
  10. mRecorder = MediaRecorder()  
  11. mRecorder.setAudioSource(AudioSource.MIC)  
  12. mRecorder.setOutputFormat(OutputFormat.THREE_GPP)  
  13. mRecorder.setOutputFile('/sdcard/testrecorder.3gp')  
  14. mRecorder.setAudioEncoder(AudioEncoder.ARM_NB)  
  15. mRecorder.prepare()  
  16.    
  17. # Record 5 seconds  
  18. mRecorder.start()  
  19. sleep(5)  
  20. mRecorder.stop()  
  21. mRecorder.release() 

Now, you can get more examples from the document.

We can map Java/Python types, native arrays, and support method overloading. We use Cython + JNI internally, so the minimum consumption performance is.

At the same time, the Python for android library has been completed and can be obtained from github.

OSChina. NET Translation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.