Monday, July 30, 2018

It's All in the Packaging.

Hello all.

Recently, I posted that if you want to build a Jython project with JDK code, it is very simple to do. However, if you want to DEVELOP your OWN Java classes, it takes a little more work. If you use an IDE to develop your code, understanding how your IDE packages your .class files is a must. Because Jython DEPENDS on knowing where your .class files are.

Understanding Java package rules will help in this are. I have found that the easiest way is to develop your Java project FIRST, then create a separate Jython project. You need to point to the directory where your .class files live.  As an alternative, you could take your Java code, package it in a .JAR file, includ it in your JYTHONPATH environment variable and you're good to go.

Creating a .JAR file is simple. A short tutorial follows:

I use IntelliJ IDEA for my coding in both Java and Jython. In my project settings, under project, I defined a global /out folder for my compiled .class files. Doing this makes it easy to create .jar files depending on your project, as I will demonstrate soon. So, currently, my folder structure is

/home/patrick/out

When IntelliJ compiles the .class files, it adds a series of sub-folders, so that it looks like this\

/home/patrick/out/production/<projectname>

Under the project name is the hierarch you created with your Java project  For me, it is

org.ppalczewski.<project>.<source1>.<so on>

So, in the end, my /out folder looks like this:

/home/patrick/out/production/<projectname>/org.ppalczewski.<project>/<source1>/<source2>/ etc

Wow, that's a lot of typing. But I only want to create a .jar that shows the hierarchy from org.ppaczewski dowm. I can do this two ways. First, I can navigate to my <projectname> folder (i.e. /home/patrick/out/production/<projectname>).

Then, I type this command to build my .JAR file:

jar cvf <project>.jar .

This will recurse from the <projectname> folder down and include all .class files. The switches are c(create), v(verbose), f(jar file name)

I have a root jarfiles folder that holds all of my jar files. I can navigate to this folder and type the following:

jar cvf <project>.jar -C /home/patrick/out/production/<projectname>/ .

This will do the same thing, and it retains your fully qualified package name structure (for me: org.ppalczewski.<project>.etc,etc).

It is very important to include the ending slash (or backslash in Windows). If you don't, you may get unexpected results. you can verify your package qualified name by

jar tf <jarfile>.jar

and this will list the .class files you have in your project.

So, after I have built my .jar file, and included this in my JYTHONPATH (you MUST include the actual name of your .jar file also), I can now fire up my Jython interpreter and test to see that it works.

In my code, I type this:

import org.ppalczewski.<project> as project

Now I have access to any classes I may have created.

To me, this makes it easier than having worry about whether Jython can see the .class files or not. 1. Build Java code. 2. Package in .jar file. 3. Import into Jython 4. Enjoy!

Then, for distribution, have the .jar file available and that's it.

Say you alread have a .jar file and you updated the code in your project? Do you need to delete the jar file and create a new one? No. Use the jar command's updatte feature, which will recreate the .jar file for you, adding any new files or updated code.

jar uvf <jarfile>.jar [-C <path>/<to>/<production>/] .

If I am able, I will create a blog with a simple project demonstrating this.

That's it for today. Happy coding!

No comments:

Post a Comment