Saturday, July 21, 2018

Hybrid Jython/Java projects in IntelliJ?

The post title is a pretty good question. The answer is: It's extremely difficult.

I will explain:

If you are pulling from extablished Java packages (JAR files), it's very easy.

However, if you are developing YOUR OWN Java classes, this is where the roadblocks start popping up. What I have found is that it's easier (in the long run) to develop you own classes in a Java project, and package them into a JAR file. Then create a separate Jython package that points to the JAR file. You can actually add it as a library in your Project settings,
The reason for this is that IntelliJ actually stores all your .class files in an output directory you must specify. So,m when you open up a .py file, it can see your code internally, but the actual .class files are somewhere else. And if you use a package structure, Jython cannot see the .class files, so it breaks the code.

For the really adventurous, you need to navigate to your output folder, call the java class with the FULLY qualified name, and it will work.

Much better to simply start a Jython project abnd point it to your output folderwhere your .class files are stored. Even easier to do the Java code, bundle it in a JAR and use Jython to call it with the qualified package name.

My ealier post on setting up IntelliJ was to work on Jython ITSELF, not for general Jython development.

I have also discovered that if you use IntelliJ's GUI builder to do your forms, Jython has a lot of trouble with that also. Why> IntelliJ stores the attributes (preferredSize, windowClosing, etc.) in its own .class files, contained in a com.intellij package. To pull up the form via Jython, you need to reference theis package. If you know your Jaa forms, and can do it via code, this way with alleviate the stress of Jython trying to find the particulars of your form to display.

If you separate your projects as I have stated above, you should be okay, as you can import the com.intellij packages into jython with hno problem and the form will be called correctly by the JVM your are using.

If you want to test your code via the command-line outside of IntelliJ, you can. Naviate to the folder ABOVE source folder and call it with the qulalified path name. Here's an example:

Let's say you have the following directory structure:

/home/patrick/myproj/mycode

I use a global out folder in IntelliJ, so it looks like this:

/home/patrick/out/production/myproj mycode


I create a test.java, and compile it. It puts the class files in the /mycode folder. Then I navigate to /home/patrick/out/production and call Test like so:

java mycode.Test

It will run. You need to match the package structure in your source file to properlyt call the class.

Lets sayyou have no package name. Navigate to the folder containing the .class file. Then you call Java with the -classpath option (listing your folder) and the class.

java -classpath /home/patrick/myproj/mycode Test

That's is for now. Fell free to comment, or provide correction. I still have a lot to learn. if you have successfully developed a JythonJava project using your own classes, let me know how and I will update this post.

Thank you and happy coding!

No comments:

Post a Comment