Jython and Intellij: Auto-Completion With Java Jar Dependencies

Using java with your jython project? Here is a quick tutorial on how to import java jar’s into the project for auto completion and an overall better experience with intellij.

Get Jython

First Off, install your jython interpreter from here:

http://www.jython.org/downloads.html

2017-07-11 19_00_52-www.jython.org_downloads.html

For this project, I used the installer because it was easier, however the standalone jar will work perfectly fine, but some of the steps below will be slightly different.

Download the installer and select the ‘Standard’ install. As usual, you’ll need to accept some license agreements. For the install path, I usually put these right in my C:\ directory because I have a few different ones installed on my machine and this makes it easier.

After the install, you’ll have the jython files in the C directory:

2017-07-11 19_02_59-jython2.7.0

Create the Jython Project and Setup

Open up intellij and create a new project. Go to File – New – Project…

2017-07-11 19_05_45-AssistExportDemo - [C__Users_lucas.crandle_workspace_AssistExportDemo] - [Assist

Then we’ll just select an empty project in case the python interpreter is not setup.

2017-07-11 19_05_27-New Project

Just name the project whatever you want and continue with the process.

It will prompt you to add a module after creation since we chose an empty project. Here is where we will need to add the jython sdk to the project.

Go the project option on the left, and then click the ‘New…’ button.2017-07-11 19_12_01-Project Structure.png

Then select ‘Python Sdk’.

2017-07-11 19_13_02-Project Structure.png

And if asked say ‘Add Local’.

Now we have to find the Jython files we installed. Select the bin/jython.exe file and click ‘Ok’

2017-07-11 19_14_46-Select Python Interpreter

Now you’ll have jython ready to go and you can write basic python in intellij.

2017-07-11 19_19_54-Project Structure

Adding in Java Libraries

The main advantage of Jython over Python is the ability to use java libraries in the code. However, we’ll need to tell intellij where to find these libraries because jython capabilities aren’t standard.

Go to the ‘Libraries’ section of the Project Settings and click the green ‘+’ symbol

2017-07-11 19_24_07-Project Structure

Now you’ll have to find your java JDK on your system. If you don’t have one, then install it from oracle on this page:

Install Java SE

(If you know what you’re doing here, feel free to only install the JRE. It’s just easier to install the jdk and jre.)

For me, I throw all of these library types in the C:\ folder so mine was easy to find. The folder we want to select will be in the jdk/jre folder, and it is called lib. Make sure you select the whole folder and click ‘OK’

2017-07-11 19_32_06-Attach Files or Directories to Library 'resources'

Now Intellij knows where to look for dependancies and can index them, which allows for auto-completion.

Get Down and Code

Now you’re ready to code! Create a new module with the added Jython SDK. You can find this prompt in ‘File’ then ‘New’ and then ‘Module…’. Make sure to select the ‘Python’ selection on the left. Name the module whatever you need it to be.

2017-07-11 19_33_49-New Module

Next, create a new file called whatever you would like, just make sure to put the ‘.py’ on the end to signify a python (really jython in this case) file.

2017-07-11 19_34_20-OverThreaded99 Project - [C__Users_lucas.crandle_workspace_OverThreaded99 Projec

Now we get to experience the best of both worlds. A simple scripting language like Python, the flexibility of java, and the auto-completion features of Intellij!

2017-07-11 19_39_07-OverThreaded99 Project - [C__Users_lucas.crandle_workspace_OverThreaded99 Projec

This works for the basic java libraries, as well as any custom java jars you have created. So custom code can be utilized with auto-completion as well.

 

Leave a comment