<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Number 42 &#187; Java</title>
	<atom:link href="http://www.thenumber42.net/category/computer-stuff/programming/java-programming-computer-stuff/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thenumber42.net</link>
	<description>Mostly about software testing, but a little bit about life, the universe, and everything</description>
	<lastBuildDate>Wed, 31 Mar 2010 01:52:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to Use JPype</title>
		<link>http://www.thenumber42.net/2008/03/31/how-to-use-jpype/</link>
		<comments>http://www.thenumber42.net/2008/03/31/how-to-use-jpype/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 18:41:11 +0000</pubDate>
		<dc:creator>Isaac</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[jpype]]></category>

		<guid isPermaLink="false">http://www.thenumber42.net/2008/03/31/how-to-use-jpype/</guid>
		<description><![CDATA[Background
I love Python and my personal experience has shown that testers are about 4x more productive when using Python than when using Java.  However, at work I am a tester on a Java application.  I've been looking for a way to bridge that gap and allow me to write my tests in Python [...]]]></description>
			<content:encoded><![CDATA[<h2>Background</h2>
<p>I love Python and my personal experience has shown that testers are about 4x more productive when using Python than when using Java.  However, at work I am a tester on a Java application.  I've been looking for a way to bridge that gap and allow me to write my tests in Python but test a Java application.</p>
<p>I've looked into <a href="http://www.jython.org/Project/index.html">Jython</a> and while it had many advantages it has one major drawback that I can't live with: Jython only implements Python 2.2.  Most of the time this is fine, but there are times where I use Python 2.4 or 2.5 features and don't want to create a workaround just so I can use an older Python standard.  If I had the money I would pay someone to bring Jython up to date.  But, that's not a possibility.</p>
<p>I did find another alternative that so far seems to work great.  It is still in development, but <a href="http://jpype.sourceforge.net/">JPype</a> seems to be working fairly well.  JPype differs in philosophy from Jython in a fundamental way: Jython is a complete reimplementation of Python in Java, whereas JPype is a bridge between CPython and native Java.</p>
<h2>Kicking The Tires</h2>
<p>This article is a "how to," so let's get down to business.  I'm going to create a Java class that I would like to access from Python.  Here is Java class:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> org.<span style="color: #006600;">wg3i</span>.<span style="color: #006600;">test</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> msg;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> speak<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> msg<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>msg<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setString<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> s<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	msg = s;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> getString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">return</span> msg;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>The class is simple enough, but it will illustrate the basic JPype usage.  For my testing I compiled this and placed it into a jar file.  Feel free to leave it as a class file if you wish.  I'll show you how to deal with both situations below.</p>
<p>For reference, here is my ant build file:<br />
<font face="courier"><br />
&lt;?xml&nbsp;version=&quot;1.0&quot;?&gt;<br />
&lt;project&nbsp;name=&quot;TestProject&quot;<br />
	&nbsp;basedir=&quot;.&quot;<br />
	&nbsp;default=&quot;jar&quot;&gt;</p>
<p>&nbsp;&nbsp;&lt;property&nbsp;name=&quot;src.dir&quot;&nbsp;value=&quot;src&quot;/&gt;<br />
&nbsp;&nbsp;&lt;property&nbsp;name=&quot;build.dir&quot;&nbsp;value=&quot;build&quot;/&gt;<br />
&nbsp;&nbsp;&lt;property&nbsp;name=&quot;classes.dir&quot;&nbsp;value=&quot;${build.dir}/classes&quot;/&gt;<br />
&nbsp;&nbsp;&lt;property&nbsp;name=&quot;jar.dir&quot;&nbsp;value=&quot;${build.dir}/jar&quot;/&gt;<br />
&nbsp;<br />
&nbsp;&nbsp;&lt;target&nbsp;name=&quot;clean&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;delete&nbsp;dir=&quot;build&quot;/&gt;<br />
&nbsp;&nbsp;&lt;/target&gt;<br />
&nbsp;<br />
&nbsp;&nbsp;&lt;target&nbsp;name=&quot;compile&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;mkdir&nbsp;dir=&quot;${classes.dir}&quot;/&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;javac&nbsp;srcdir=&quot;${src.dir}&quot;&nbsp;destdir=&quot;${classes.dir}&quot;/&gt;<br />
&nbsp;&nbsp;&lt;/target&gt;<br />
&nbsp;<br />
&nbsp;&nbsp;&lt;target&nbsp;name=&quot;jar&quot;&nbsp;depends=&quot;compile&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;mkdir&nbsp;dir=&quot;${jar.dir}&quot;/&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;jar&nbsp;destfile=&quot;${jar.dir}/${ant.project.name}.jar&quot;<br />
	&nbsp;basedir=&quot;${classes.dir}&quot;/&gt;<br />
&nbsp;&nbsp;&lt;/target&gt;<br />
&nbsp;<br />
&nbsp;&nbsp;&lt;target&nbsp;name=&quot;clean-build&quot;&nbsp;depends=&quot;clean,jar&quot;&gt;<br />
&nbsp;&nbsp;&lt;/target&gt;<br />
&lt;/project&gt;</font><br />
After creating a jar file you are ready to go.  Here is the Python code to utilize a jar file and create an instance of the Java class.</p>
<pre class="python">&nbsp;
<span style="color: #808080; font-style: italic;"># Simple little JPype test.  Note: This test assumes</span>
<span style="color: #808080; font-style: italic;"># that the jar file is in the build/jar directory</span>
<span style="color: #808080; font-style: italic;"># relative to the current directory</span>
<span style="color: #ff7700;font-weight:bold;">import</span> jpype
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>
&nbsp;
jarpath = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">abspath</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'build/jar'</span><span style="color: black;">&#41;</span>
jpype.<span style="color: black;">startJVM</span><span style="color: black;">&#40;</span>jpype.<span style="color: black;">getDefaultJVMPath</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot;-Djava.ext.dirs=%s&quot;</span> % jarpath<span style="color: black;">&#41;</span>
Test = jpype.<span style="color: black;">JClass</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'org.wg3i.test.Test'</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># get the class</span>
t = Test<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># create an instance of the class</span>
t.<span style="color: black;">speak</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;This is a test message&quot;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># try to call one of the class methods</span>
t.<span style="color: black;">setString</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello, World&quot;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># set a string</span>
s = t.<span style="color: black;">getString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># get the string back</span>
<span style="color: #ff7700;font-weight:bold;">print</span> s
jpype.<span style="color: black;">shutdownJVM</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>In a nutshell that's it.  I promised to show you how to initialize JPype when you have class files and not jar files.  Here's a Python example that shows that  This example also shows the use of the JPackage method, which gets a <em>package</em>, not a class.</p>
<pre class="python">&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> jpype
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>
&nbsp;
classpath = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">abspath</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'build/classes'</span><span style="color: black;">&#41;</span>
jpype.<span style="color: black;">startJVM</span><span style="color: black;">&#40;</span>jpype.<span style="color: black;">getDefaultJVMPath</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot;-Djava.class.path=%s&quot;</span> % classpath<span style="color: black;">&#41;</span>
testPkg = jpype.<span style="color: black;">JPackage</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'org'</span><span style="color: black;">&#41;</span>.<span style="color: black;">wg3i</span>.<span style="color: #dc143c;">test</span>  <span style="color: #808080; font-style: italic;"># get the package</span>
Test = testPkg.<span style="color: black;">Test</span> <span style="color: #808080; font-style: italic;"># get the class</span>
t = Test<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># create an instance of the class</span>
t.<span style="color: black;">speak</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;This is a test message&quot;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># try to call one of the class methods</span>
t.<span style="color: black;">setString</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello, World&quot;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># set a string</span>
s = t.<span style="color: black;">getString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># get the string back</span>
<span style="color: #ff7700;font-weight:bold;">print</span> s
jpype.<span style="color: black;">shutdownJVM</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<h2>Conclusion</h2>
<p>There are many things I have not covered, such as passing arrays back and forth, types other than strings, exceptions, etc.  However, this should be enough to get you started using JPype.  I'd be interested in hearing your own experiences or suggestions.</p>
<div class="link-summarizer">Link Summary
<ul>
<li><a href='http://www.jython.org/Project/index.html'>http://www.jython.org/Project/index.html</a></li><li><a href='http://jpype.sourceforge.net/'>http://jpype.sourceforge.net/</a></li><li><a href='http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky'>http://www.google.com...27m%20Feeling%20Lucky</a></li><li><a href='http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky'>http://www.google.com...27m%20Feeling%20Lucky</a></li>
</ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.thenumber42.net/2008/03/31/how-to-use-jpype/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
