Having problems running system commands from a java program: - java

This is giving just the output of ls:
String[] cmd={"bash","-c","ls","-l"}:
ProcessBuilder pb=new ProcessBuilder(cmd);
Whereas this is giving long listing output properly:
String[] cmd={"bash","-c","ls -l"};

In the first code snippet, the -l option is being passed as an argument to bash, and not to ls. Bash interprets the -l option as specifying that it should behave as a 'login' shell.
The argument after -c should include the whole bash script (spaces included) that you want to be executed, so the second code snippet is correct.

The former passes two option flags to bash: -c with argument ls, and -l which according to the manpage causes bash to act as a login shell.
The second passes one option flag, -c, which the argument ls -l as a single string.

String[] cmd={"bash","-c","ls -l"}:
ProcessBuilder pb=new ProcessBuilder(cmd);
The arguements are to bash, so if you want bash to interpert your "command" via "bash", "-c", ...
then the next item needs to be your entire command, aka "ls -l".
Bash will then parse up the "command" and the -l will be sent as a parameter to "ls". Currently it is a parameter to "bash", which is why you're not getting the results you desire.

Related

Running pmcmd from java

I am trying to run pmcmd and pass arguments from java. This is my code :
String cmd="C:\\Informatica\\9.6.1\\clients\\PowerCenterClient\\CommandLineUtilities\\PC\\server\\bin\\pmcmd.exe";
final Process cmdProcess;
cmdProcess = Runtime.getRuntime().exec(new String[]{cmd,"connect -sv IS_NAME -d DOMAIN_NAME -u USER -p PWD"});
cmdProcess.getOutputStream().close();
The problem is I am not able to get the desired output. I get the following error:
ERROR: Unknown command [connect]
When I try the same command on the command line, it works.
pmcmd>connect -sv IS_NAME -d DOMAIN_NAME -u USER -p PWD
The output:
Connected to Integration Service:[IS_NAME].
Can anyone tell what mistake I am doing?
(adding my comment as an answer, after it worked according to the OP)
Your command line example suggests that the connect -sv ... is issued within the pmcmd process, and not provided as an argument.
So you should probably send that to the process' STDIN (accessed by cmdProcess.getOutputStream()) instead of passing as argument to the call.
pmcmd works in two modes, command line and interactive. connect command works in interactive mode only.
When invoking from java, you are using command line mode, and do not need to connect first. You can directly invoke the command you intend to run (ex. startWorkflow) and provide the connection parameters with that command like below:
pmcmd startworkflow -sv MyIntService -d MyDomain -u seller3 -p jackson ‑f SalesEast wf_SalesAvg
More details here.
I had to issue a command within the pmcmd process. So I modified my code and it works :
String cmd="C:\\Informatica\\9.6.1\\clients\\PowerCenterClient\\CommandLineUtilities\\PC\\server\\bin\\pmcmd.exe";
final Process cmdProcess;
cmdProcess = Runtime.getRuntime().exec(new String[]{cmd,""});
OutputStream out = cmdProcess.getOutputStream();
out.write("connect -sv IS_NAME -d DOMAIN_NAME -u USER -p PWD".getBytes());
out.close;

Java runtime exec zip command with "*" character

I want to zip contents of folder without including folder it self using Java Runtime
For example: zip folder /home/duyvt/example
In terminal, I exec the following commands and it works well. Archive.zip will include only contents of example folder.
$ cd /home/duyvt/example.
$ zip -r /home/duyvt/archive.zip *
But in Java Runtime, it does not work.
Runtime.getRuntime().exec("zip -r /home/duyvt/archive.zip *", null, new File("/home/duyvt/example"));
It seems Java Runtime does not understand "*" character.
I have also tried to use "ProcessBuilder" but not work too.
Is there anyone can help me ?.
Thanks for any help.
On Linux, it is the shell that understands and expands *.
On Windows, the command program has to do it itself.
Java is generic, and does not implement shell functionality.
If you want shell functionality, run the command through the shell, e.g.
sh -c 'zip -r /home/duyvt/archive.zip *'
UPDATE
From javadoc of exec(String command, String[] envp, File dir):
This is a convenience method. An invocation of the form exec(command, envp, dir) behaves in exactly the same way as the invocation exec(cmdarray, envp, dir), where cmdarray is an array of all the tokens in command.
More precisely, the command string is broken into tokens using a StringTokenizer created by the call new StringTokenizer(command) with no further modification of the character categories. The tokens produced by the tokenizer are then placed in the new string array cmdarray, in the same order.
Since StringTokenizer doesn't understand the shell quoting using '', the command is split incorrectly, and you'll have to do it yourself:
Runtime.getRuntime().exec(new String[] { "sh",
"-c",
"zip -r /home/duyvt/archive.zip *" },
null, new File("/home/duyvt/example"));
Start shell and pass it a command, like so:
"sh -c 'zip -r /home/duyvt/archive.zip *'"

Shell (bash) brace expansion with Java's runtime.exec

I'm trying to get an expansion command to work with runtime.exec, but the braces are being interpreted as literals rather than being expanded. Here's what I'm trying to do:
String command = "mkdir -p Foldername{1,2,3}/InnerFolder";
Runtime.getRuntime().exec( new String[] { "sh", "-c", command } );
Unfortunately, that gives me a single folder in my current directory named "Foldername{1,2,3}" instead of "Foldername1", "Foldername2", and "Foldername3". Does anyone know of a way to prevent the braces from being interpreted as literals?
You're trying to use Bash wildcards. They are interpreted within the Bash shell. You are running mkdir directly, so there is no shell to interpret {}. You need to specify path to the shell
String command = "mkdir -p Foldername{1,2,3}/InnerFolder";
Runtime.getRuntime().exec( new String[] { "/bin/bash", "-c", command } );
Source.

How to use linux commands involving ">" through runTime.exec()

I'm new to Java. I want to use a command
"ps -e > /home/root/workspace/MyProject/ProcessList.txt"
with runTime.exec();
On searching through the web, I came to know that runTime.exec() doesn't support pipes or redirecting. Please let me know how can I execute this command with my Java code. Please give exact answers.
Pipes and redirection are features provided by the shell. The easy (and dirty) solution is to spawn the command inside a shell: "/bin/sh -c 'ps -e > /home/root/workspace/MyProject/ProcessList.txt'".
Edit: I had forgotten that the default StringTokenizer does not work with quoted strings. Provide arguments as an array of strings.
String[] args = {
"/bin/sh",
"-c",
"ps -e > /home/root/workspace/MyProject/ProcessList.txt"
};
java.lang.Runtime.getRuntime(args);
You could take a look at this question: Input and Output Stream Pipe in Java
Otherwise, if you know you are on a platform that supports the bourne shell (sh), you could add that to the command to run the original command in that shell:
"sh -c 'ps -e > /home/root/workspace/MyProject/ProcessList.txt'"

Executing a terminal command in a java progam

javap -classpath /Users/amol/Documents/Java/ -l -c a
When I execute the above command in my terminal the output shows the function names in my class along with the local variables
Process process3 = Runtime.getRuntime().exec(new String[]{
"javap","-classpath","/Users/amol/Documents/Java/","-l","-c","a"});
However when I type the above line in netbeans and run it, it only show the function names and line numbers where the local variables have been declared.
Why is there a change in the output for the same command?

Categories