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.
Related
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
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 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
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 ?) ?
Can we call a Windows cmd command in Java? For example, calling the "unzip" command of Windows in a Java program. Would that be difficult?
Yes, that's possible. The most basic API which Java SE provides for this is the Runtime#exec(). It has some known traps though, this article is an excellent read: When Runtime.exec() won't.
Note that Java SE provides the java.util.zip package as well for zipping/unzipping files programmatically. See also this article for a guide.
yes you can do it,USE
**Runtime.getRuntime().exec("your command");**
I would suggest using the newer class ProcessBuilder: http://download.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html
It holds your hand a little bit more and it allows you to merge the error and stdout streams so that you don't have to have two streamgobbler threads running.