I am trying to write inside a JSP/Servlet Java web project, a python-3 Machine Learning code depends on torch and some advanced frameworks.
I tried:
to use Jython but it did not work because it only works for
python 2 programs.
to use ProcessBuilder and
Runtime.getRuntime().exec("pythonFile.py") to execute the python file but nothing
worked.
Is there any suggestions on how to write that Python code in the Java project or communicate them?
For option #2, you'd need to pass the python executable into the exec method. For example:
Runtime.getRuntime().exec("python pythonFile.py")
Source: https://www.edureka.co/community/358/how-to-execute-a-python-file-with-few-arguments-in-java
Related
My major program is written in Python 2.7 (on Mac) and need to leverage some function which is written in a Java 1.8, I think CPython cannot import Java library directly (different than Jython)?
If there is no solution to call Java from CPython, could I integrate in this way -- wrap the Java function into a Java command line application, Python 2.7 call this Java application (e.g. using os.system) by passing command line parameter as inputs, and retrieve its console output?
regards,
Lin
If you have lot of dependcieis on Java/JVM, you can consider using Jython.
If you would like to develop a scalable/maintainable application, consider using microservices and keep Java and Python components separate.
If your call to Java is simple and it is easy to capture the output and failure, you can go ahead with this running the system command to invoke Java parts.
A number of open source projects have been written to enable calling Java from CPython, depending on your needs.
Pyjnius
Py4J
JPype forked (original JPype hasn't been updated in years)
jpy
my project has a significant and isolated part that was written in ruby(jruby compatible). It is a commandline application that we run it in the terminal and provide it with various option flags.
My client wants to use this tool but only willing to use it if it is wrapped in a java class. I went through a lot of trouble to convert the ruby code to java by using jrubyc --javac A.rb. Inspecting the converted .java file, it is calling a Ruby Runtime to execute the ruby script. Like this:
org.jruby.Ruby.getGlobalRuntime().executeScript(stringBuiltFromARubyFile, 'path')
My question is performance wise, is this better than just wrap the create a runnable jar, wrap it with a java class that takes certain parameters, and execute the jar via Runtime.getRuntime().exec("java -jar A.jar args") ?
The application A.rb uses multi-threading. I bring in ruby dependencies (gems) using jruby-gradle plugin.
What are some other options I can explore?
Thanks in advance.
The approach you are using likely won't work. You are launching a JVM within a ruby context; and what you've been asked for is launching some of your ruby code within a JVM context.
I would look at C to Ruby bindings, and then use a Java to C (JNI) interface to launch the required Ruby code from the C layer. If such a thing is not practical, as your Ruby is more of a standing service than a CLI process, I would then consider making a set of Java libraries to call the process through a network call.
I have seen in answered here that if you want to call a python script from java, you can use jython as a option.
But have seen other answers as well saying, you can use Process or ProcessBuilder and call their exec or start methods to run the python script.
As I understand jython allows you to program python in you java code, but it allows you to call python scripts as well via, PythonInterpreter.execfile.
So I'm wondering what are my options if I want to call a python script (e.g. text processing script which uses nltk) from my java code and along with some arguments and return the results back to my java programme? which option should I use?
Jython is an implementation of the Python language for the Java platform
(source)
You can simply use ProcessBuilder if there is a regular python script on a machine that has regular python installed. This saves you from having to include a whole python runtime in your java application. You can pass parameters quite easily, the returned text is a little trickier: See http://examples.javacodegeeks.com/core-java/lang/processbuilder/java-lang-processbuilder-example/ or other examples. Basically anything that the python program prints can be captured that way.
However if you don't like external dependencies and prefer shipping everything in a single application you can try jython. It has some limitations though since it doesn't come with all those modules and it seems it may have difficulties with C modules. Getting Python's nltk.wordnet module working for Jython looks like an explanation on how to get nltk to run with it. Arguments and return values should be simpler with jython since the integration is better. And if you want more than just printed text, Jython can even call into java code directly from python. That would give you interesting options for hybrid code.
I'm working on a big Java project right now, and I decided that the easiest way to "fix" an issue I have with the data in a File object would be to parse and edit it with a Python script I wrote. I tested the Python script on its own on this particular file (outside of Java) and it works fine. Now I'm just wondering how I integrate my Python script with the rest of my Java code.
I basically want to somehow pass this File object to my Python script, which has to be called from Java. Then I want to take the file that the Python script outputs, read it back in as a File object, and return that from this particular Java method.
From researching a bit, I've heard Jython come up a bit, as well as the built-in Python interpreter in Java 6 and later. I'm just not sure where to start. Or alternatively, I guess I could port this Python script over to Java, but that's kind of a last resort. Any help is appreciated!
Look at these classes
http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html
http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html
You can use them in Java to call your Python process.
can a java application call a unix executable written in c++? basically i have written code in unix in c++ and shared the executable with a couple of companies. All is well save for this on e company that is using java under a linx platform. would it not be possible for them to just call my executable from their java app? of course i make sure my unix os matches their etc etc. but i don't want to redevelop my code using java for this. any solution to this problem?
Yes, it's possible. To execute a command:
String command = "./myscript";
Process process = Runtime.getRuntime().exec(command);
You can change the command to execute your C++ program.
See also here and here (the latter of which discusses Windows as well as *nix.)
Have a look at ProcessBuilder. I'm assuming your executable is a compiled command line application (and you're not out to call / integrate your C++ code from Java directly).
Java would still be using the operating system to run the native code. To run your program on Linux you need to compile a Linux binary.
Yes, you can. Take a look at the Runtime class in the Java api, you will found your answer there. But also, be aware of what J2EE Specifications are about doing these kinds of things.
Cheers.
As long as you can run the command without Java, you can use Runtime.getRuntime().exec(command); to launch it from a Java process.