How to access linux command such as sed,awk from java - java

I have been studying JNI (Java Native Interface) since last month.It is really interesting .I mean call Java functions from C and vice versa.Now I have an intention call linux command which I mentioned above like sed,awk from Java side.also I have a little bit knowledge about Sell Script in linux. please give me some hint how to do this.

You should Runtime.exec() or ProcessBuilder depending on what you require to execute process into your local system.
This questsions could help:
difference between ProcessBuilder and Runtime.exec
Java Runtime.exec()

You could use this java snippet to run a shell command:
Process process = Runtime.getRuntime().exec("echo This is a test"); //Start the process
process.waitFor(); //Wait for the process to terminate
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); //Get the output

Related

Java calls wrong python version

I'm trying to run a Python script from a Java program using Process and ProcessBuilder, however Java keeps using the wrong version of Python. (The script needs 3.6.3 to run and Java runs Python 2.7)
However when I run the script from the terminal (outside of Java), it runs the correct Python (3.6.3). How does one change what version of Python gets run when called by Java?
The short version is it changes with your PATH environment variable.
Under Windows, Technet has the answer. Scroll down to the 'Command Search Sequence' section. This answer explains it nicely.
For UNIX-like OS's, this answer is nicely detailed.
There are two very useful commands for determining which executable is going to be called: which for UNIX-likes and where for newer Windows.
The most likely reason for the difference between Java and the terminal is a difference in your PATH. Perhaps your Java version is being run with a modified PATH? A launch script of some kind may be changing it.
Add /usr/bin/python3.4 to the start of your command to force the version of python you want. If you're not sure where python is installed, have a go at using whereis python and seeing what you get back.
private int executeScript(final List<String> command) {
try {
final ProcessBuilder processBuilder = new ProcessBuilder("/usr/bin/python3.4").command(command);
processBuilder.redirectErrorStream(true);
System.out.println("executing: " + processBuilder.command().toString());
final Process process = processBuilder.start();
final InputStream inputStream = process.getInputStream();
final InputStream errorStream = process.getErrorStream();
readStream(inputStream);
readStream(errorStream);
return process.waitFor();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return -1;
}
Then just pass in the List containing your python commands.

How to run Linux command with environment variable in JAVA [duplicate]

I am trying to make my Java program interact with Linux bash but something goes wrong. I have a simple executable prog that reads the one integer from stdin and outputs its square. Executing
echo 5 | ./prog
from bash itself prints correct answer 25 in stdout but running
import java.io.*;
public class Main {
public static void main(String[] args) throws InterruptedException, IOException {
Runtime run = Runtime.getRuntime();
Process proc = run.exec("echo 5 | ./prog");
proc.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while(br.ready())
System.out.println(br.readLine());
}
}
unexpectedly gives 5 | ./prog. What is the solution?
Java exec cannot run shell commands like that. If you want to run a shell command, you need to explicitly invoke the shell; e.g.
Process proc = run.exec(new String[]{"/bin/sh", "-c", "echo 5 | ./prog"});
For more details on what Java does with this, read the javadocs for exec(String) and exec(String[]). Note that these are "convenience methods", and you need to follow the chain of links to the underlying methods for a complete understanding of what the javadoc is saying.
If you want even more detail on how Java handles this, there is the source code ...
If you want to understand in depth why Java doesn't handle the shell syntax itself, you probably need to take a deep dive into the architecture and philosophy of UNIX / Linux systems, and the separation of concerns between application, operating system and command shell. Note that there are a myriad different shells, each with (potentially) different rules for quoting, argument splitting, redirection, pipes, etcetera. Most of the popular shells are similar, but that's just the way things panned out.
Explanation of the solution:
The reason for splitting the command by hand is that exec(String) won't split the command into a single command and arguments correctly. It can't. This is an example where there are two commands in a pipeline.
The reason for using "sh" is ... well ... you need a shell to parse and process a shell command line. Java's exec command does not support shell syntax ... as the javadoc explains.
The purpose of the "-c" option is explained by "man sh". Basically, sh -c "a b c" means "use 'sh' to run the command line 'a b c'".
FWIW, it is technically possible to construct and run a pipeline solely in Java (i.e. without relying on an external shell), but it is generally not worth the effort. You've already introduced a platform dependency by running external commands, so an extra dependency in the form of a specific shell command and syntax doesn't make things significantly worse.

Correct way to run another java program from java

I want to run the following shell command from a java application:
java -jar saxon9he.jar -warnings:fatal a.xml a.xsl param1=123 param2=abc
Currently, I am simply executing this as a shell command using
ProcessBuilder pb = new ProcessBuilder(commandLineParts);
[...]
Process process = pb.start();
What is the correct way to do this in java?
This is the correct way of executing a command in Java. Just to clear possible confusion: ProcessBuilder doesn't execute the program using a shell. That's the reason why you have to provide it with a list of arguments and not a single string (that would be parsed by a shell).
There are two possibilities:
either you want to run the Java program in a new JVM and then using
the ProcessBuilder is the way to go
or you don't mind if it is executed in the same JVM and then you can call the main method yourself as Sean suggests (possibly in a different thread)
Another option, depending on the type of the application, would be to perform some acrobatics with an application server to start the app in it.
If you use the ProcessBuilder just be careful about handling its input and output streams - if you don't handle them your application can hang: Java ProcessBuilder: Input/Output Stream This has been improved in Java 7.

Running program with subprocesses with Java

I need to run an external program written in C from my Java application.
I'm trying to use Runtime.getRuntime().exec() with partial success. I execute program using String with path to .exe file and its arguments:
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
log.info(line);
}
int exitVal = pr.waitFor();
This would work just fine - program executes and sends information about its behavior to console. Problem is that external program tries to run other subprograms during its execution. So basically what I need to do is: I run program.exe with my Java application and program.exe runs subprogram.exe. Unfortunately, that's not the case, because subprogram.exe never starts in current situation.
What should I do differently to make it work ? Thanks for any help.
It is not my decision to have it so complicated and I would be more than happy to have just one .exe file to execute, but I can't.
Try to run it from cmd.exe i.e.the command prompt and see if its working well.The console output should give you a trace of the program execution.

How to run .reg file in Java

I need to install a .reg file (INTRANET) by using Java. How do i get my goal ?
Cheers,
You could use System.exec to launch regedit yourfile.reg
Here is how to do it :
String[] cmd = {"regedit", "yourfile.reg"};
Process p = Runtime.exec(cmd);
p.waitFor();
Last line is optional, it only allows you to wait until the operation is over.
If you're already on Java 1.6, just grab java.awt.Desktop:
Desktop.getDesktop().open(new File("c:/yourfile.reg"));
It will launch the file using the default application associated with it, as if you're doubleclicking the particular file in Windows explorer.
This can achieved through Process Builder in JAVA. Please consider the following example for this:
ProcessBuilder processBuilder = new ProcessBuilder("regedit", "reg_file_to_run.reg");
Process processToExecute = processBuilder.start();
And then you can optionally wait for the completion of process execution with this line:
processToExecute.waitFor();
Note: If command in your registry file asks for confirmation prompts while making changes in registry entries, you can perform it silently as well with '/s' option. Like this:
ProcessBuilder processBuilder = new ProcessBuilder("regedit", "/s", "reg_file_to_run.reg");
With this command would be executed silently without any confirmation prompt.

Categories