Is there an API for calling system scripts/programs? - java

is there something like API built atop the standard ProcessBuilder for calling system programs? I won't argue that scripts can be ported to Java but utilities like arping and netstat are good-to-go in Linux.

Have a look at Apache Commons Exec.

Why not just use the standard ProcessBuilder class... It handles calling scripts pretty well. Here is a post showing how this can be used to call a command via bash.

Using Runtime.getRuntime().exec(...) except you need to know the name and parameters to pass to system program.

Related

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

Calling C++ executable from JAVA

I have poker bot written in C++. I want to compete it with another Poker Academy agent. In order to do that there is an API in JAVA namely "meerkat api".
I have no idea how to call my c++ executable from java api, how does one go about communicating with the independent c++ executable from within a java package?
To get started see:
java.lang.Runtime.exec() family
java.lang.ProcessBuilder
Or if you need to integrate with the C++ app on lower lvl meaning not only "run a program and process the results". You can use the JNI or easier variant JNA, but I have to admit it is pain.
After reading/following #hmjd, Well you can use DefaultExecutor from Apache Commons Exec library to execute commands but it internally uses java.lang.Runtime and java.lang.Process.
I would suggest you to use this library over Runtime because Apache Command Execution APIs are more sophisticated, and provide all and more features than Java Runtime. It also handles exit Values.

Can we call a Windows cmd command in Java?

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.

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.

Java Wrapper to Perl/Python code

I have to deploy some Web Services on a server that only supports the Java ones, but some of them will be done using perl or python. I want to know if is possible to develop a Java wrapper to call a specific code written in perl or python. So, I want to have all the Web Services in Java, but some of them will call some code using other languages.
Thanks in advance.
Regards,
Ukrania
This depends heavily upon your needs. If Jython is an option for the Python code (it isn't always 100% compatible), then it is probably the best option there. Otherwise, you will need to use Java's Process Builder to call the interpretters directly and return the results on their output stream. This will not be fast (but then again, Jython isn't that fast either, relative to regular Java code), but it is an extremely flexible solution.
For the Python part of it you can use Jython to run Python code right from your Java virtual machine. It'll integrate fully with your Java code as a bonus.
For Perl, use Inline::Java. There are several options for integrating the code; you can call a separate process or you can use an embedded interpreter.
For Python you can use the Java Scripting API.
A Perl implementation is sadly still missing.
There's something I used a while back called Jython which allows you to execute Python code from Java. It was a little quirky, but I got it to do what I needed.
http://www.jython.org

Categories