How to make a Python script an executable program - java

To be clear, not just "run python scripts"
But to make my python script a "executable" or "callable" program
that can be used in other programming languages or platforms.
More like a API maybe.
The thing is I implemented several algorithms in java and
they're supported by numpy and spipy, but others want to
call my python program in their java program.
Then the numpy and spipy are problems. They can't be in java and
jython...
Is there a solution that I can make this an executable program that others don't need the environment but just to run the program with several parameters accepted?

If you just need to make the python programs executable, then you can add a line to the top like this:
#!/usr/bin/python
Replace /usr/bin/python with the filepath to python, which can be obtained by typing
which python
into the terminal, assuming you're using a unix-based system.
You can then tell the operating system that the program is executable with
chmod +x nameofprogram
If the java programs require something more complicated than just being able to run the python parts as executables, then you'll probably need to provide more information for anyone to be able to help you.

Related

How to use NLTK in Jython, or PY4J cross platforn?

I have two questions, this could go into different directions based on opinion, but as of right now I am writing a Java/Kotlin API. So far it is compatible across all platforms, excluding IOS(have not tested).
A few of the tasks it runs is python using PY4J but the way the API calls the python script to start the PY4J connection is
Runtime.getRuntime().exec("python script.py") # Is there a better way to do this?
Which is fine until it comes to Android. Will Android be able to use this method? If so, the question is answered and finished there.
If not, is there a way to embed a python interpreter into a JAR file? Java project?
I like Jython, but I could not get NLTK to work with it.
1) Is there a way to make Jython work with NLTK : If not,
2) Is there a way to use PY4J in Python?
3) Is there a way to embed a custom Python interpreter in Java/JAR?
Feel free to edit my question, code and title. I don't do this much, so it is probably sloppy.
This was silly, but I will keep it up since this was a real question. You can install python3 directly into the JAR. When the python installation screen shows up you can just install it into a folder inside your Java project.
For the Android part, there is a python package called p4a, python-for-android. You can create a python interpreter with dependencies. This should work.

do you need jython for calling a python script from java?

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.

Is There Any Way To Use Linux Commands in Java Directly

I want to use Linux grep in my java program for searching, because grep's performance is satisfying. But I don't want to call it with Runtime.getRuntime().exec() command from operating system. Therefore I downloaded grep-2.9 source code but now I don't know how I can integrate that code to my java app. Is there any way to use grep or other linux commands directly, not from OS?
I think you should better try this:Grep4J
Another possible alternative is to use groovy they have grep supported by the language library
Both of approaches are better then running linux specific utilities from Java.
Hope this helps

Package them all! Perl, python, java for naive users (in windows)

I have several scripts written in perl, python, and java (wrapped under java GUI with system calls to perl & python). And I have many not-tech-savy users that need to use this in their windows machines (xp & 7).
To avoid users from installing perl,python,and java and to avoid potential incompatibility between various versions of these interpreters, I'd like to make a local copy of these interpreters in a folder and then calling them. I'd zip the whole folder (which would also contain my code) and send it away.
I'd have to worry about environment variables and make calls to the correct interpreter (especially when other versions of python,java,perl may exists in their current system), but not sure what other problems I may face. Any better ideas?
I never used jython and do not know the overhead of moving to it. I also suspect a complex python system, with many files and 3rd party modules will have problems. Same with perl scripts and I don't know a robust perl interpreter callable from java.
Thank you, in advance.
Try Portable Python and Portable Perl. You can unzip them into your application tree and they should work.
Why don't you try migrating your perl/python code into java and then packagin everything into a nice webstart application? What do perl/python offer that java doesn't support?
For perl you can use something like perl2exe and for python py2exe so you can have 2 exes (which would include all the necessary interpreter bits) and invoke them as resources from within java? Or unzip them inside user's home directory and call them again as normal external programs (ProcessBuilder ?) ?

calling a unix exectuable via a java enterprise application

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.

Categories