How to embed JyNI in a jar - java

I'm developing an application in java, and in this one, I use jython-standalone (Jython for untold reasons, its goal being to ease some scripting within the application).
I would like to have access to NumPy, and it seems that JyNI provides such capabilities.
I found many posts explaining how to start a Jython project using JyNI, but none on how to include the .jar or something in the application so that it is available when needed.
As stated on JyNI, java -cp build/JyNI.jar -jar jython.jar does not work.
Is there a way for me to use JyNI when executing some Jython code through a PythonInterpreter ?

You should be able to start the live interpreter by executing the class org.python.util.jython.
On Linux, OSX:
java -cp jython.jar:build/JyNI.jar org.python.util.jython
On Windows:
java -cp jython.jar;build\JyNI.jar org.python.util.jython
Alternatively, one can use Jython's start script:
jython -J-cp build/JyNI.jar
This info is taken from https://github.com/Stewori/JyNI#running-jyni, but leaving out the script argument someFile.py.
If you want to explicitly leverage the interpreter through Java code, you will surely have some way to launch your overall application. Make sure to have JyNI.jar and the folder containing its binaries on classpath in that command. Jython should be running with JyNI support then.
A side-note: Be sure to use Jython 2.7.1 and NumPy >= 1.12. Version mismatch of these prerequisites is a common cause of failure.

Related

jep for using scikit model in java

I am using jep for running python script in java, I basically need to run the script that uses scikit package. But it shows me error when I try to run, which I couldn't understand.
This is the piece of code in my program,
Jep jep = new Jep();
jep.eval("import sklearn");
It shows the below error,but sklearn works perfectly well in python.
Jul 06, 2016 5:31:50 PM JepEx main
SEVERE: null
jep.JepException: jep.JepException: : /usr/local/lib/python2.7/dist-packages/sklearn/__check_build/_check_build.so: undefined symbol: _PyThreadState_Current
Contents of /usr/local/lib/python2.7/dist-packages/sklearn/check_build:
setup.py __init.pyc _check_build.so
build init.py setup.pyc
It seems that scikit-learn has not been built correctly.
If you have installed scikit-learn from source, please do not forget
to build the package before using it: run python setup.py install or
make in the source directory.
If you have used an installer, please check that it is suited for your
Python version, your operating system and your platform.
at jep.Jep.eval(Jep.java:485)
at JepEx.executeCommand(JepEx.java:26)
at JepEx.main(JepEx.java:38)
Caused by: jep.JepException: : /usr/local/lib/python2.7/dist-packages/sklearn/__check_build/_check_build.so: undefined symbol: _PyThreadState_Current
Contents of /usr/local/lib/python2.7/dist-packages/sklearn/check_build:
setup.py __init.pyc _check_build.so
build init.py setup.pyc
It seems that scikit-learn has not been built correctly.
If you have installed scikit-learn from source, please do not forget
to build the package before using it: run python setup.py install or
make in the source directory.
If you have used an installer, please check that it is suited for your
Python version, your operating system and your platform.
at /usr/local/lib/python2.7/dist-packages/sklearn/check_build/__init.raise_build_error(init.py:41)
at /usr/local/lib/python2.7/dist-packages/sklearn/check_build/__init.(init.py:46)
at /usr/local/lib/python2.7/dist-packages/sklearn/init.(init.py:56)
The _PyThreadState_Current error implies that it's using the wrong Python. You should be able to fix it by setting PATH and LD_LIBRARY_PATH to the python/bin and python/lib directories you want to use (and built Jep and sklearn against) before launching the process. That will ensure that Python, Jep, and sklearn are all using the same libraries.
If that doesn't work, it's possible that Jep or sklearn were built with different versions of Python than you're running.
We had the same problem.We believe it's a build problem and since rebuilding scikit-learn did not solved that we ended up using scikit-learn using the prebuilt Anacoda2.

Trying to install Java on a Rails Heroku app

My Rails app relies on legacy Java code for a piece of its logic. It runs the code via shell commands: javac whatever and then java whatever. However, after deploying to Heroku, the request fails because it can't locate javac or java.
What's the best way to install Java in a Rails app? I tried using a multi-buildpack solution, with this .buildpacks:
https://github.com/heroku/heroku-buildpack-ruby.git
https://github.com/heroku/heroku-buildpack-java.git
But I get this error:
=====> Downloading Buildpack: https://github.com/heroku/heroku-buildpack-java.git
! Push rejected, failed to compile Multipack app
I tried forking the Ruby buildpack and installing Java directly from there, but I don't really know what I'm doing. Any ideas?
Try using heroku-buildpack-jvm-common instead of the Java buildpack. So your .buildpacks file will look like this:
https://github.com/heroku/heroku-buildpack-jvm-common.git
https://github.com/heroku/heroku-buildpack-ruby.git
It's probably a good idea to put the JVM one before the Ruby one in case javac or java are needed at compile/build time.
The JVM buildpack is essentially the same as the Java buildpack, but doesn't install Maven and thus does not require a pom.xml file. The missing pom.xml is why you got that error I think.
EDIT
In the modern era, you can also run:
$ heroku buildpacks:add heroku/jvm
$ heroku buildpacks:add heroku/ruby

Jython Installation and Running (Beginner Issues)

I am having some serious trouble installing and running Jython. I am completely new to how jython runs, so I am pretty lost. I have downloaded Jython 2.5.3 and java jre 1.8.0_25. I believe I have succesfully run the jython.jar file using this command in cmd:
C:\java\jre1.8.0_25\bin\java -jar C:\jython2.5.3\jython.jar
I have beginner-intermediate (closer to beginner) knowledge of programming in jython, but that is on a computer with jython already installed. My main problem is being able to run jython so I can see the actual function/program area where you would type out your functions then run them. I appreciate any help someone can give.
That command is exactly how you run Jython, and without providing any additional arguments, the Jython interpreter will start (where you can type out functions and run them).
If you want to run Jython with a single command, there should be a jython.bat file located somewhere under the Jython installation directory. Jython does not have a true executable because it is run through Java. The jython.bat file is the closest you'll get to an executable because it is a batch script which runs the jython.jar file using a command nearly identical to yours with Java. jython.bat should be located at one of the following locations:
C:\jython2.5.3\jython.bat
C:\jython2.5.3\bin\jython.bat
NOTE: The jython.bat file requires a standard Java installation.

creating 100% standalone executable jar that doesn't require the java command

so apparently if you create an executable jar, in order to run it you still need the java command:
java -jar something.jar
but what if I just want it to run without the java command, so just directly from the command line
something.jar
is there a way to export my java app in eclipse in order to accomplish such
On Unix systems you can append the jar file at the end of an executable script.
On Windows you have to create a batch file.
For instance in Unix:
$cat HelloWorld.java
public class HelloWorld {
public static void main( String ... args ) {
System.out.println("Hola mundo!");
}
}
$cat M.mf
Main-Class: HelloWorld
$cat hello
#!/bin/sh
exec java -jar $0 "$#"
$javac HelloWorld.java
$jar -cmf M.mf hello.jar HelloWorld.class
$cat hello.jar >> hello
$chmod +x hello
$./hello
Hola mundo!
In windows you have to create a batch file like:
::hello.cmd
javaw -jar hello.jar
Which has the same effect.
On Windows and OSX you can double click on the jar to run it, I'm pretty sure you may add a trigger on Linux too.
I hope this help
Excelsior JET - http://www.excelsior-usa.com/jet.html - claims to compile to native code and bring its own runtime support, so it does not require an existing JVM. Commercial product.
I have not tried it myself, but they have spent quite a bit of effort over the years to market JET as a great deployment method for precompiled binaries.
Also note that if you have an executable/runnable jar which works fine with "java -jar someting.jar" and you just want to be able to invoke it in a more convenient way, this is the job of the program accepting your command and launching the java command.
For Linux you can frequently add an alias saying that "something" expands to "java -jar something.jar", and some command interpreters allow for saying that all commands ending with jars should be executed specially. The exact details depend on which shell (command line interpreter) you are using.
What you need is a tool called 'Java Executable Wrapper'.You can use it to Pack all your class files to a Single Executable Package.
The One i recomment is launch4j,you can download it from sourceforge launch4j.sourceforge.net
Launch4J can be used to create standalone Executables (.exe) from a jar file for windows Environment.
The thing is, that Java gets interpreted by the JVM, so you'll at least need to ship it with your app.
To be a little more specific about this, Java gets kind of compiled to byte-code so it can be interpreted faster. But the Byte-Code can't run without the JVM. This is the nice side of Java: You don't need to recompile your Apps to run on other platforms like Linux or OS X, the JVM takes care of that (as it is written in native code and is recompiled for those platforms).
There are some compilers out there which can convert your Java code to something native like C which can then be executed without the JVM. But this isn't the idea behind Java and most of those tools suck at what they do.
If you want your App to run without any interpreter, you'll need to use a compiled language like C or C++
Java program runs on a JVM, for the first question I don't think there's a compiler that can do the job well. For the second question since a jar file is not an executable per se, there must be some sort of settings in the target machine, "executing" a jar file without providing the java command is a matter of convenience for the user. On Windows every file extension has a program associated with it, so .doc documents have (usually) Word as the program associated -that setting is set by the office installer, the java runtime also sets the setting for .jar files when you install it, but behind the scenes, java command will be used by the system. So the short answer to the second question is: depends on the target machine.

Jar file of java

I have created a java application and packed it into a jar file on a Windows platform. Now I wants to deploy that jar file on Debian Linux.
Will the same jar file work for Debian Linux?
Is the command, used in windows for executing a jar file from the command prompt, same for Debain Linux?
i.e.
java -jvm "MyJar.jar"
Will the above command work for Debian Linux?
Generally, it should. However this depends on a few conditions:
If you use native code (JNI) you must make sure that the native library is available for the target platform
You must make sure you have no paths hardcoded which are Windows specific (in fact you should even watch out for special characters like the Path seperator : vs. ;)
You cannot use Runtime specific code
Yes.
Jar files are portable across platforms, and the syntax of the jar command is the same on both Linux and Windows.
EDIT: You should use the latest version of Sun Java unless there is a very good reason not to. Installation instructions: http://wiki.debian.org/Java/Sun
1. Will the same jar file work for Debian Linux?
Yes. Hence the nature of Java (portable code)
2. Is the command, used in windows for executing a jar file from the command prompt, same for Debain Linux?
java -jar "MyJar.jar"
yes, the main idea of java is that it (should) run on different operating systems, as long as a java runtime is installed.
though i have never heard of the -jvm flag.
if you want to start a jar file you should use the -jar flag.
java -jar "MyJar.jar"
you can also read up on the Write once run anywhere principle.
I do my development on a mac but run in linux and windows environments without any problem. Key is not to use JNI, As everyone else have mentioned I would use java -jar "MyJar.jar"
Almost. Use:
java -jar "MyJar.jar"
And of course you shouldn't have used anything such JNI or runtime stuff
Yes, although you might want to do, in Linux:
java -jar YourJar.jar
Instead of:
java -jvm YourJar.jar

Categories

Resources