Is There Any Way To Use Linux Commands in Java Directly - java

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

Related

How to make a Python script an executable program

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.

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.

Automated distribution for Java project

I have to make a tool for automated distribution of the Java code. Basically, I have a repository with compiled files, and about 50 locations to distribute the same code.
Does anyone know some opensource tool which can help me in this process?
If you are speaking about easy deployment of java applications, use JNLP. The only thing user has to do in this case is to surf to URL.
If you wish to do it without any user participation I believe the solution depends on target platform:
Use SSH for Unix platforms
WNI or telnet for windows platforms.
To make the solution more portable you can run
wget THE-JNLP-URL
on target machine using SSH for unix like platforms.
I do not know built-in command like wget for windows. But you can implement this in VBS or JS and then invoke the script using cscript over WMI or telnet.
Good luck.
Either you can distribute it out with rsync, or you can use Java WebStart to let the user JVM download and invoke the software as needed. For Windows based clients this is usually the easiest, especially when you want people to update to a newer version.

Is there an API for calling system scripts/programs?

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.

Categories