So, I'm writing a program(mostly for practice, kind of for future convenience) that will show a java front-end when run, and depending on the input, will use a python script to crawl and interpret a webpage, then send the interpreted data back to java to format for the front end. I'm sure I can get this running on my own computer, but when I want to distribute my program for my friends to use, how can I ensure my java program, mainProgram.jar can find my python code, script_x.py. Is there a way I can assure my python script will be supported. I've read that there are ways to include the necessary runtimes and such with your program when you build it, but would it still interact the same way with my java program if it was bundled with it's dependencies? I can provide some sample code soon, but it's still a work in progress, as I don't have the python script finished yet.
here is a link https://bytes.com/topic/python/insights/949995-three-ways-run-python-programs-java
use Jython (compiles to java byte code) https://en.wikipedia.org/wiki/Jython
distributing Jython applications: https://wiki.python.org/jython/JythonFaq/DistributingJythonScripts
Related
I am writing the python module where I need to interact with java module for some work.
I have already jar for java layer.
I can I invoke the jar files and call the class/Methods which are there in Jar files.
I don't want to use the Jython since major of my code is pure python
I tried subprocess.call()
but it's not serving my purpose
subprocess.call(['java','-jar', 'my.jar'])
EDIT:
I need to call the java layer because I need some input to my python module from there.
I tried py4j but no successes
JPype is an alternative to Jython that I made some good experience with. If it is not enough to call the java program and work with the output (it's hard to tell from your question), then JPype can be used to (more or less) transparently work with Java object in Python code.
I works by starting a JVM and handing requests to said JVM.
I'm using Python to launch a java program and to extract its results using os.system
os.system("java myprogram")
The problem is this launch the program on console and I have to type command inside the program to extract the result.
Do somebody know how to give command to the java program from Python ?
Thank you,
Easy way to invoke java program or any other script is using subprocess in python.
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions
Read more about subprocess
Example:
subprocess.Popen - Following will invoke your script in background.
subprocess.Popen(["java", "ProgramPath/filename.java", "arg1", "arg2", "arg3"])
subprocess.call - And this will wait for the command to complete.
subprocess.call(["java", "ProgramPath/filename.java", "arg1", "arg2", "arg3"])
subprocess.check_output - will return you the output
output = subprocess.check_output(["java", "ProgramPath/filename.java", "arg1"])
http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html
This chapter will focus on integrating Java and Python, but it will explore several different angles on the topic. You will learn several techniques to make use Jython code within your Java applications. Perhaps you’d like to simplify your code a bit; this chapter will show you how to write certain portions of your code in Jython and others in Java so that you can make code as simple as possible.
I want to make a web application for books using java servlets with Tomcat. I want to import some code (that reads *.epub, *.pdf, *.doc, *.mobi, and *.txt files) from the project Calibre into my project, if it's possible.
Calibre source: http://bazaar.launchpad.net/~kovid/calibre/trunk/files Some source from this link I want to work into my project.
Application Calibre is an open source written in Python and C. What I want is to import sources written in python (.py), which allows me to read standard epub, pdf, txt ... in my project developed in java servlet to view and to convert the books. I using Eclipse(Java EE) and I do not know what steps to import these sources and how to make. It must to export these source to jar?
Python isn't compatible with Java, in that Java can't run python code.
But, there are a couple of options.
Run the python code from your java app directly with a system call.
I use ProcessBuilder for these situations, and you can look at
http://javaevangelist.blogspot.com/2011/12/java-tip-of-day-using-processbuilder-to.html.
Set up Calibre as a web application and call the conversion
functions from your web application to the calibre webapplication
you start up. It appears ready to run as a web application
according to http://calibre-ebook.com/about and
http://www.techrepublic.com/blog/opensource/how-to-use-calibre-to-access-your-ebook-collection-online/2275.
I see two other ways:
You could work with Jython (but I have no experience with that and don't know if it solves your problem)
You could write a JNI module for Java which in turn starts and uses the Python interpreter.
Python and java are different languages. You cannot have java code call python code or the other way round without some sort of conversion. I think your best option is to either
Look for a Java library that does what you want, or
Write your website in python; there are many excellent libraries for these
Sure there are ways to call python from java, but it'll be rather hard to apply them to something as big Calibre, which also uses some C.
I am in the process of designing a MATLAB-based algorithm. Is it possible to call that MATLAB source code from a Java application?
Has anyone come across such a issue?
have a look at MATLAB Builder JA. This program allows you to convert your matlab code into java classes.
It is possible to call MATLAB from Java using matlabcontrol. It's a Java API designed specifically for that. However, it requires MATLAB to be running on the same machine that the Java code is executing on. As you put the Android tag I assume you want to run this on an Android device (although not mentioned in your description), and there is no known way to directly run MATLAB code on Android. matlabcontrol definitely will not do that, but you could use it to write a Java application which acts as a server for your Android application. Similarly, you could do the same with MATLAB Builder JA which generates code that needs either MATLAB or the MATLAB Compiler Runtime (which is available for free - so users would not need to buy MATLAB).
I have a Java app that takes a long time to be initialized (so I can't use a command-line like interface) and I need to pass text and receive the output of a Java method from Python. Is it possible to load the Java application, have it open all the time the Python script runs and use a method from that app?
I don't think the use of Python helps all that much over a command line (at least not a *nix command line), but the basic idea is to communicate over a socket or some similar mechanism. That means that the Java application will have to be wrapped in some code which opens a socket and waits for the python script to contact it. If you are most comfortable with python, you could look to implement that wrapper in Jython.
I've used JPype for something similar and it worked great.
JPype is an effort to allow python programs full access to java class libraries. This is achieved not through re-implementing Python, as Jython/JPython has done, but rather through interfacing at the native level in both Virtual Machines.
If the java app is running you should also consider xml-rpc as it also works well.
Py4J is a way to call Java from a python script. Here is the project website: http://py4j.sourceforge.net/