[JAVA] I want to use jar from python

I want to use jar from python

JNI seems to have to use CPython, so py4j is a good idea. A guy who starts java in another process and bridges with python by socket communication.

I tried it with kuromoji and my own jar. I mean, I was happy that I could use kuromoji from python, so I tried various things.

Preparation

code

First of all, the one I made

org/kuryu/Sample.java


package org.kuryu;

public class Sample {
  public String foo() {
    return "FOO!!!!!!!!";
  }

  public static String bar() {
    return "BAR!!!!!!!!";
  }
}

Make it a jar


$ javac org/kuryu/Sample.java
$ jar cvf kuryu.jar org/kuryu/*.class

Code on the java side of py4j

J4Py.java


import py4j.GatewayServer;

public class J4Py {
  public static void main(String[] args) {
    J4Py app = new J4Py();
    GatewayServer server = new GatewayServer(app);
    server.start();
  }
}

Add your own jar and kuromoji to the manifest.

manifest.mf


Main-Class: J4Py
Class-Path: py4j0.8.2.1.jar kuromoji-0.7.7.jar kuryu.jar

Make it a jar


$ javac -cp py4j0.8.2.1.jar J4Py.java
$ jar cvfm J4Py.jar manifest.mf J4Py.class

Start Gateway Server

If you start it through the classpath, it seems that you can also use it from the py4j side.

When starting with jar


$ java -jar J4Py.jar

When starting with class


$ java -cp py4j0.8.2.1.jar:kuromoji-0.7.7.jar:kuryu.jar:. J4Py

Code on the python side of py4j

With the Java Gateway Server running, do this.

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from py4j.java_gateway import JavaGateway

gateway = JavaGateway()

# java.util.Random
random = gateway.jvm.java.util.Random()
n1 = random.nextInt(10)
n2 = random.nextInt(10)
print "%d + %d = %d" % (n1, n2, n1+n2)

#Self-made jar
kuryu = gateway.jvm.org.kuryu.Sample
print kuryu.bar()
ins = kuryu()
print ins
print ins.foo()

#kuromoji Tokenizer
KuroTokenizer = gateway.jvm.org.atilika.kuromoji.Tokenizer
tokenizer = KuroTokenizer.builder().build();
a = tokenizer.tokenize(u"I want to eat sushi.")
for token in a:
    print token.getSurfaceForm() + "\t" + token.getAllFeatures()

I used it as a reference

Mainly this strange.

http://www.atilika.com/ja/products/kuromoji.html http://py4j.sourceforge.net/py4j_java_gateway.html http://qiita.com/mojaie/items/f16b97b1388f73e56b86

Recommended Posts

I want to use jar from python
I want to use ceres solver from python
I want to use MATLAB feval with python
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to use Temporary Directory with Python2
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to make C ++ code from Python code!
I wanted to use the Python library from MATLAB
[Python3] I want to generate harassment names from Japanese!
I want to use the R dataset in python
I want to debug with Python
I want to start a lot of processes from python
[Python] I want to use the -h option with argparse
I want to send a message from Python to LINE Bot
I want to build a Python environment
I want to use Linux on mac
I want to analyze logs with Python
I want to play with aws with python
I want to use IPython Qt Console
I want to use a wildcard that I want to shell with Python remove
I want to use both key and value of Python iterator
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Use MySQL from Python
Use MySQL from Python
Use BigQuery from python.
Use mecab-ipadic-neologd from python
I want to connect to PostgreSQL from various languages
I want to memoize including Python keyword arguments
I want to create a window in Python
I want to perform SageMaker inference from PHP
I want to make a game with Python
[Bash] Use here-documents to get python power from bash
I want to merge nested dicts in Python
I want to make fits from my head
I don't want to use -inf with np.log
#Unresolved I want to compile gobject-introspection with Python3
I want to use ip vrf with SONiC
I want to solve APG4b with Python (Chapter 2)
What I did when updating from Python 2.6 to 2.7
I want to sell Mercari by scraping python
I want to write to a file with Python
I want to use the activation function Mish
I want to display the progress in Python!
Use Tor to connect from urllib2 [Python] [Mac]
Python: Use zipfile to unzip from standard input
Tips for manipulating numpy.ndarray from c ++ -I want to use an iterator-
Use Python from Java with Jython. I was also addicted to it.
I want to write in Python! (1) Code format check
I want to see the file name from DataLoader
Even beginners want to say "I fully understand Python"
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to iterate a Python generator many times
Post from Python to Slack
python3: How to use bottle (2)
I tried to summarize how to use matplotlib of python
I want to generate a UUID quickly (memorandum) ~ Python ~
Cheating from PHP to Python
I want to use self in Backpropagation (tf.custom_gradient) (tensorflow)