Execute a linux terminal command in java? - java

i'm trying to execute a SOX command from java, but unfortunately its returning an error everytime. Every other SOX commands are working perfectly though!!
Here is the code :
class Simple {
public static void main(String args[]) throws IOException, Exception {
Process p;
BufferedReader br;
String co = "sox speech_16.wav -p pad 0 2.5 | sox - -m speech_16.wav speech_output.wav";
p = Runtime.getRuntime().exec(co);
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
int returnCode = p.waitFor();
System.out.println("reurn code : "+returnCode);
}
}
When I'm executing the same sox command in terminal, its working fine. I really can't understand what the problem is!! Is it because of the '|' symbol??

The issue is that Runtime.exec() does not understand shell concepts such as "|". Instead try:
Runtime.getRuntime().exec("/bin/sh", "-c", co);
The problem is that exec runs a binary directly without invoking the shell. The "|" character is only recognized by the shell, not by sox. The "-c" tells the shell to run a single command, and passes the entire command as the single argument.

This is likely to be related to the environment in which the commands get executed, it could be any of the following:
The sox executable cannot be found (put the full path in the command)
The user does not have permission to run the sox command (check execute bit in file permissions)
Some environment variable needed by sox is not initialised when you run the command from Java (check sox documentation)
If speech_16.wav is an input file to sox then the file cannot be found (add full path of .wav file to command)
If sox needs to create an output file then it does not have permission to do so, either due to directory permissions, of because there is an existing file with that name which cannot be overwritten, or due to lack of space on the file-system.

Related

How to perform java command prompt action on unix

I have two jar files named test1.jar and test2.jar.
In order to execute a function in in this jar file, I run below command in cmd.
“java getOutput argument1 argument2 argument3 argument4”
As a pre-requisite, I have set the following entries in my system environment variables to execute the java function using the above command.
1.CLASSPATH=C:\Folder\test1.jar;C:\Folder\test2.jar;C:\Program Files\Java\jre1.8.0_181\lib\plugin.jar
2.JAVA_BIN=C:\Program Files\Java\jre1.8.0_181\bin
3.JAVA_HOME=C:\Program Files\Java\jre1.8.0_181
4.PATH = C:\Program Files\Java\jre1.8.0_181\bin
The output of the above command is a string.
I have another java code. I have to capture the above string output in a variable in my java program and I am able to do this by using below java code in eclipse.
String strArgs= strArg1+" "+strArg2+" "+strArg3+" "+strArg4;
Process p=Runtime.getRuntime().exec("cmd /c java getOutput "+strArgs+"");
BufferedReader input =new BufferedReader(new
InputStreamReader(p.getInputStream()));
String strOutput = input.readLine();
if (strOutput != null) {
System.out.println(strOutput);
String strVariable = strOutput;
input.close();
}else {
System.out.println("FAIL- Output not generated");
}
In order to execute the above code on unix, I replace Line 2 with "Process p=Runtime.getRuntime().exec("usr/bin/java getOutput "+strArgs+"");" , convert the eclipse java project into a jar file named automation.jar. And I try to execute this jar file on unix using the below command .
`java -cp automation.jar Package.MainClass.'
When I run the code I get the output as null. But when I run the command on windows command prompt I get the output string.
Could you please help me on how to set the system variables in unix and perform the above operation to generate the string output.
Please let me know if any additional information required.
cmd is windows only. Unix uses a shell. But you shouldn't need either here. Just remove the cmd call.
Process p = Runtime.getRuntime().exec("java getOutput " + strArgs);
If it can't find Java, you can read the JAVA_BIN (or JAVA_HOME) from the environment.
String java_bin = System.getenv("JAVA_BIN");
Process p = Runtime.getRuntime().exec(java_bin + "/java getOutput " + strArgs);
Finally, your environment variables look like Windows path variables. Make sure they're correct for your Unix environment (Unix doesn't have a "C" drive).

complete CMD command not running from java

I am running the below query through Java on a Postgres DB using psql:
psql.exe -U <user> -w -h <host> -d <db_name> -a -f <file> 2> "<path_to_file>\psql.log"
Initially, for quite some time the java program did create the file. Then I ran into another problem, that it was not overwriting the log file. So i used file.delete() function after every time this log file got created via java.
Now, Java is not even creating the log file for some reason. If I run the above manually in command prompt, it runs absolutely fine, but not via java code. I can see this command getting run in the java log, but it does not create the log file even when i have removed the file.delete() function
I researched a lot on it but could not find any solution. Any help would be highly appreciated.
its a long code..so i will tell you the relevant part.
I am calling a function from a thread. Code is below for that function:
public static void SaveACopyfileToServer(int auditid,String filepath,String fname,String tb_name,String plpgsql_path) throws Exception
{
Map<String, String> env = System.getenv();
String plpgsql = "\""+plpgsql_path+"\" -U "+env.get("PG_USER")+" -w -h "+env.get("PG_HOST")+" -d "+env.get("PG_DB")+" -a -f "+"\""+filepath+"copy_"+tb_name+auditid+".sql\" 2> \"C:\\ER\\ETL\\logs\\psql.log\"";
System.out.println(plpgsql);
Process p = Runtime.getRuntime().exec(plpgsql);
p.getOutputStream().close();
p.waitFor();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
Calendar cal10 = Calendar.getInstance();
System.out.println("Data loaded for "+tb_name+auditid+" at "+sdf.format(cal10.getTime()));
}
After this i am calling another function which is:
public static void extracterrorreason(String fname,int auditid,String sessionid,Connection con_pg) throws FileNotFoundException, IOException, InterruptedException{
File file = new File("C:\\ER\\ETL\\logs\\psql.log");
if(file.exists())
{
System.out.println("File present");
}
else
{
System.out.println(file+" not found");
}
if (file.length()!=0){
System.out.println("Log file being read is "+file);
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine();
String out_err = line.substring(line.indexOf("ERROR"));
System.out.println(out_err);
System.out.println("Error while loading the file into Database for file "+fname);
String comment = "CopyToStage','"+out_err;
Utils.updateAuditDetailTable(auditid, sessionid, -1, comment, true, con_pg,"");
br.close();
//file.delete();
}
}
The first function used to create the psql.log file, but now it does not even create it. Not sure where is the problem. Every time i run the code and from the second function,i get the printline that log file not found. The part before the redirection of the output of the cmd command works fine.
I tried process builder also..
I even tried it with Process builder
String plpgsql = "\""+plpgsql_path+"\" -U "+env.get("PG_USER")+" -w -h "+env.get("PG_HOST")+" -d "+env.get("PG_DB")+" -a -f "+"\""+filepath+"copy_"+tb_name+auditid+".sql\" 2> \"C:\\ER\\ETL\\psql_" +auditid +".log\"";
ProcessBuilder pb = new ProcessBuilder("cmd.exe",plpgsql);
Process p =pb.start();
p.getOutputStream().close();
p.waitFor();
I expect that the problem is that Runtime.getRuntime().exec(plpgsql) is splitting the command line into arguments incorrectly. Basically, exec does not understand quoting. Instead, it splits wherever it sees one or more spaces ... even if those spaces are in quotes.
The solution is to use the exec(String[]) overload, and pass each individual argument as a separate string; e.g.
.exec(new String[]{plpgsql_path,
"-U",
env.get("PG_USER"),
"-w,
"-h",
// etcetera
});
UPDATE
I didn't notice that you were using > output redirection as well1.
That doesn't work with exec either. (And the same applies to all shell syntax.) To get redirection, you need to use ProcessBuilder and one of the redirect methods.
The other alternative is to run the command in a shell. Pass the command as a string, and let the shell take care of the quote handling, substitution of environment variables, globbing, redirection ... and so on.
For example (if you were running on UNIX, Linux or MacOSX):
.exec(new String[]{"/bin/sh", "-c", plpgsql});
For Windows
.exec(new String[]{"cmd.exe", "/C", plpgsql});
Note the "/C" option in the Windows case!
1 - It serves you right for not line-breaking that ~200 character line in your source code! Check out what Java coding standards say about source line lengths ...

CreateProcess error=2 when the path really exists

I'm working with the java ProcessBuilder class to start an instance of nmap on my windows workstation.
The following code produces an exception:
java.io.IOException: Cannot run program "C:\Program Files (x86)\Nmap\nmap.exe -T4 -A -v --max-scan-delay 0ms --min-rate 1000000 --max-retries 0 -oX - 192.168.1.1 ": CreateProcess error=2, The system cannot find the file specified
The value of command is:
C:\Program Files (x86)\Nmap\nmap.exe -T4 -A -v --max-scan-delay 0ms --min-rate 1000000 --max-retries 0 -oX - 192.168.1.1
Running the command is generated right in a command window executes properly.
Any ideas?
String command = this.getCommand().toString();
ExecutionResults results = new ExecutionResults();
ProcessBuilder procBuilder = new ProcessBuilder(new String[]{command.toString()});
try {
Process e = procBuilder.start();
results.setErrors(this.convertStream(e.getErrorStream()));
results.setOutput(this.convertStream(e.getInputStream()));
You're passing the whole command (including parameters) as a single parameter of the ProcessBuilder constructor. It takes a String[], where the first item should be the path to the executable, and the other items should be the parameters. Try
command.split(" ")
instead of
new String[]{command.toString()}
Edit: I see that you have spaces in your path, that will break it :( you could try splitting the executable path and the arguments into two strings. And the constructor argument will be a String[] containing the path itself as the first item and then an array of the parameters split by space.
OR: if you don't mind not using the ProcessBuilder.. this is much simpler:
Process e = Runtime.getRuntime().exec(command);
It is simple from the exception itself,
java.io.IOException: Cannot run program "C:\Program Files (x86)\Nmap\nmap.exe -T4 -A -v --max-scan-delay 0ms --min-rate 1000000 --max-retries 0 -oX - 192.168.1.1 ": CreateProcess error=2, The system
Process builder taking the whole line thinking it is as an executable and trying to run that. Just use the below example,
ProcessBuilder procBuilder = new ProcessBuilder(new String[]{"C:\Program Files (x86)\Nmap\nmap.exe"});
procBuilder.start();
This will work fine. So this is not an issue what you think that java is unable to find the executable. It is taking whole line as an executable. Better you do like below,
ProcessBuilder procBuilder = new ProcessBuilder(new String[]{"C:\Program Files (x86)\Nmap\nmap.exe", "-T4", "-A"}); //add all params
procBuilder.start();
Try this, it should work fine
As the other answers have indicated, your immediate problem is that you're passing an entire command line as if it's the name of a program to run. CreateProcess is looking for a program named "C:\Program Files (x86)\Nmap\nmap.exe -T4 -A etc" and failing to find it.
If you have a string containing a command to run (a program name with arguments, redirection, and so on), then the simplest way to run it is to launch it via the command line processor. Here's a simple example of doing that:
public static void main(String[] args) throws IOException, InterruptedException {
String command = "dir /w";
ProcessBuilder procBuilder = new ProcessBuilder(new String[]{"cmd", "/c", command});
procBuilder.redirectInput(Redirect.INHERIT);
procBuilder.redirectOutput(Redirect.INHERIT);
procBuilder.redirectError(Redirect.INHERIT);
Process p = procBuilder.start();
int ecode = p.waitFor();
System.err.println("Exit code " + ecode);
}
It might be because of a couple of reasons from my experience
Firewall would be removed certain files necessary for the installer. So would suggest to uninstall and reinstall
Can also be because of spaces in the folder path, would suggest to install in new folder which does not have spaces in the path.

bat file does not execute within Java

I have written some code for executing .bat file. which contains some
commands like setting java classpath,etc..And finally there is one command
which runs a Java class file.The HelloWorld class converts some xml file and generating a new xml file in some folder. When I double click .bat file, it executes fine,
but when I try to run I am not getting any output as I was getting through
double click the .bat file. How to make a batch execute and probably it would be nice
if I could see the results through Java console.
Following is MyJava code to execute the .bat file
public void run2() {
try {
String []commands = {"cmd.exe","/C","C:/MyWork/Java/classes/run.bat"} ;
Process p = Runtime.getRuntime().exec(commands);
BufferedReader in = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
And below the some commands which has been set to .bat file
set CLASSPATH=%CLASSPATH%;C:/MyWork/Java
set CLASSPATH=%CLASSPATH%;C:/MyWork/Java/classes
java -cp test.jar;test2.jar test.HelloWorld
Tried with "/C" commad as well. It does not execute. Actually it does not give effect of double click the .bat file. Is there any other way that I can try with?
I can see the contents inside the .bat file through Eclipse console. But it does not give the desired output. Desired output means when I double click .bat file, it executes well. But through java call, I can see the contents only .
When using cmd.exe use /C-Parameter to pass command:
String []commands = {"cmd.exe","/C","C:/MyWork/Java/classes/run.bat"} ;
according to this, the Windows CMD needs the /c argument, to execute commands like this. try this:
String []commands = {"cmd.exe","/c","C:/MyWork/Java/classes/run.bat"} ;
Windows uses \ backslash for Windows and MS-DOS path delimiter. Forward slash / is accepted by Java in the java.io package and translated to be a path delimiter, but will not be directly acceptable to Windows or accepted by the cmd.exe shell.
You may also need to specify either the working directory for the batch file to be executed in, or possibly a full path to the cmd.exe command interpreter.
See: Runtime.exec (String[] cmdarray, String[] envp, File dir)
String[] commands = {"C:\\Windows\\System32\\cmd.exe", "/c",
"C:\\MyWork\\Java\\classes\\run.bat"};
File workDir = new File( "C:/MyWork");
Process process = Runtime.getRuntime().exec( commands, null, workDir);
To verify if the batch file is run at all, add a pause command to the batch file. That will keep the window open so you can verify if the batch file is launched at all, and debug this stage-by-stage.
You do not read the error output of your batch file, therefore, you'll never see any error messages printed from there or from CMD.EXE itself. In addition, the sub-program may stall and just wait for you to read the error stream.
Please see related discussions here: How to make a java program to print both out.println() and err.println() statements?

Getting error in calling shell script in windows environment using java code and cygwin...!

I want to call shell script on windows environment using java code.
I am trying to execute code below to run my test script(not actual script):
Java Code:
public static void main (String args[]) {
Runtime r = Runtime.getRuntime();
try {
Process p = r.exec("C:\\cygwin\\bin\\bash -c '/cygdrive/d/scripts/test.sh'");
InputStream in = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
System.out.println("OUT:");
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
in = p.getErrorStream();
br = new BufferedReader(new InputStreamReader(in));
System.out.println("ERR:");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
test.sh
#!/bin/bash
rm -rf ./test
But I am getting these error and script in not able to remove the directory.
ERR:
/cygdrive/d/ereader/scripts/test.sh: line 2: $'\r': command not found
/cygdrive/d/ereader/scripts/test.sh: line 3: rm: command not found
Another thing, when I run the script from the cygwin terminal it works fine. I checked the path variable they all are fine. But I try to execute same script through java code it gives error..
Now how to tell java program where to refer for rm commands?
The giveaway is the '\r' error.
Windows and Unix (which includes Mac and Linux) use different representations of a new line. Windows uses '\r\n' while Unix simply uses '\n'. Most programming editors account for this and only insert a '\n' so they work with Unix tools.
I would suggest retyping your shell script in another editor like Notepad++ (which only inserts '\n'), or make sure your current editor is set to use Unix newlines. You have to retype it or the bad '\r\n' sequences will get copied over. You might have some luck in doing a replace-all but that always acts flaky for me.
probably you will need to set the PATH variable first thing in the test.sh script. Make sure rm is in the PATH you set
Not really answer. You could try following to narrow down the problem
Use ProcessBuilder instead of Process. It is much more friendly to handle arguments.
Set absolute path to rm (/bin/rm ) in the script
remove using absolute path to directory or remove after verifying you are in the correct directory..
The bash prompt you have is result of cygwin.bat calling bash with --login. It will have path variables and other usesul stuff sources. bash -c does not do it.
Try launching bash.exe with bash -l -c <command> : This sources the bash profile.

Categories

Resources