I am working on an application an have an issue about running shell command from java application. here is the code:
public String execRuntime(String cmd) {
Process proc = null;
int inBuffer, errBuffer;
int result = 0;
StringBuffer outputReport = new StringBuffer();
StringBuffer errorBuffer = new StringBuffer();
try {
proc = Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
return "";
}
try {
response.status = 1;
result = proc.waitFor();
} catch (InterruptedException e) {
return "";
}
if (proc != null && null != proc.getInputStream()) {
InputStream is = proc.getInputStream();
InputStream es = proc.getErrorStream();
OutputStream os = proc.getOutputStream();
try {
while ((inBuffer = is.read()) != -1) {
outputReport.append((char) inBuffer);
}
while ((errBuffer = es.read()) != -1) {
errorBuffer.append((char) errBuffer);
}
} catch (IOException e) {
return "";
}
try {
is.close();
is = null;
es.close();
es = null;
os.close();
os = null;
} catch (IOException e) {
return "";
}
proc.destroy();
proc = null;
}
if (errorBuffer.length() > 0) {
logger
.error("could not finish execution because of error(s).");
logger.error("*** Error : " + errorBuffer.toString());
return "";
}
return outputReport.toString();
}
but when i try to exec command like :
/export/home/test/myapp -T "some argument"
myapp reads "some argument" as two seperated arguments.but I want to read "some argument" as only a argument. when i directly run this command from terminal, it executed successfully. I tried '"some argument"' ,""some argument"" , "some\ argument" but did not work for me. how can i read this argument as one argument.
I recall that the an overload of exec method provides a parameter for the arguments seperately. You need to use that
Yup. Here is it
public Process exec(String[] cmdarray)
throws IOException
Just make the command line and all arguments Seperate elements of the String array
first make a string
String cmd="/export/home/test/myapp -T \"some argument\"";
then run cmd in proc
Related
I am executing a command which returns me the Revision number of a file; 'fileName'. But if there is some problem executing the command, then the application hangs up. What can I do to avoid that condition? Please find below my code.
String cmd= "cmd /C si viewhistory --fields=revision --project="+fileName;
Process p = Runtime.getRuntime().exec(cmd) ;
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
I guess the issue is that you are only reading InputStream and not reading ErrorStream. You also have to take care that both the streams are read in parallel. It may so happen that currently the data piped from the output stream fills up the OS buffer, your exec command will be automatically be suspended to give your reader a chance to empty the buffer. But the program will still be waiting for the output to process. Hence, the hang occurs.
You can create a separate class to handle both the Input and Error Stream as follows,
public class ReadStream implements Runnable {
String name;
InputStream is;
Thread thread;
public ReadStream(String name, InputStream is) {
this.name = name;
this.is = is;
}
public void start () {
thread = new Thread (this);
thread.start ();
}
public void run () {
try {
InputStreamReader isr = new InputStreamReader (is);
BufferedReader br = new BufferedReader (isr);
while (true) {
String s = br.readLine ();
if (s == null) break;
System.out.println ("[" + name + "] " + s);
}
is.close ();
} catch (Exception ex) {
System.out.println ("Problem reading stream " + name + "... :" + ex);
ex.printStackTrace ();
}
}
}
The way you use it is as follows,
String cmd= "cmd /C si viewhistory --fields=revision --project="+fileName;
Process p = Runtime.getRuntime().exec(cmd) ;
s1 = new ReadStream("stdin", p.getInputStream ());
s2 = new ReadStream("stderr", p.getErrorStream ());
s1.start ();
s2.start ();
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(p != null)
p.destroy();
}
This code is based on the same idea Arham's answer, but is implemented using a java 8 parallel stream, which makes it a little more concise.
public static String getOutputFromProgram(String program) throws IOException {
Process proc = Runtime.getRuntime().exec(program);
return Stream.of(proc.getErrorStream(), proc.getInputStream()).parallel().map((InputStream isForOutput) -> {
StringBuilder output = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(isForOutput))) {
String line;
while ((line = br.readLine()) != null) {
output.append(line);
output.append("\n");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return output;
}).collect(Collectors.joining());
}
You can call the method like this
getOutputFromProgram("cmd /C si viewhistory --fields=revision --project="+fileName);
Note that this method will hang if the program you are calling hangs, which will happen if it requires input.
I am trying to run a script using Java and ProcessBuilder. When I try to run, I receive the following message: error=2, No such file or directory.
I dont know what I am doing wrong but here is my code (ps: I tried to execute just the script without arguments and the error is the same:
String[] command = {"/teste/teste_back/script.sh, "+argument1+", "+argument+""};
ProcessBuilder p = new ProcessBuilder(command);
try {
// create a process builder to send a command and a argument
Process p2 = p.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p2.getInputStream()));
String line;
log.info("Output of running " + command + " is: ");
System.out.println("Output of running " + command + " is: ");
while ((line = br.readLine()) != null) {
log.info(line);
}
}
Try replacing
String[] command = {"/teste/teste_back/script.sh, "+argument1+", "+argument+""};
with
String[] command = {"/teste/teste_back/script.sh", argument1, argument};
Refer ProcessBuilder for more information.
ProcessBuilder(String... command)
Constructs a process builder with the specified operating system
program and arguments.
You can define a method with ProcessBuilder.
public static Map execCommand(String... str) {
Map<Integer, String> map = new HashMap<>();
ProcessBuilder pb = new ProcessBuilder(str);
pb.redirectErrorStream(true);
Process process = null;
try {
process = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = null;
if (process != null) {
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
}
String line;
StringBuilder stringBuilder = new StringBuilder();
try {
if (reader != null) {
while ((line = reader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (process != null) {
process.waitFor();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
if (process != null) {
map.put(0, String.valueOf(process.exitValue()));
}
try {
map.put(1, stringBuilder.toString());
} catch (StringIndexOutOfBoundsException e) {
if (stringBuilder.toString().length() == 0) {
return map;
}
}
return map;
}
You can call the function to execute shell command or script
String cmds = "ifconfig";
String[] callCmd = {"/bin/bash", "-c", cmds};
System.out.println("exit code:\n" + execCommand(callCmd).get(0).toString());
System.out.println();
System.out.println("command result:\n" + execCommand(callCmd).get(1).toString());
Unless your script.sh has a comma in its name, that is the mistake:
String[] command = {"/teste/teste_back/script.sh" , argument1, argument};
I'm trying to count the number of lines of a text file using a unix command from java code.
My code looks like:
String filePath = "/dir1/testFile.txt";
Runtime rt = Runtime.getRuntime();
Process p;
try {
System.out.println("No: of lines : ");
findLineCount = "cat " + filePath + " | wc -l";
p = rt.exec(findLineCount);
p.waitFor();
} catch (Exception e) {
//code
}
But, nothing is displayed in the console. When I execute the command directly, it works. What could be the issue in the above code?
I suggest you use a ProcessBuilder instead of Runtime.exec. You can also simplify your command by passing the filePath to wc. Please don't swallow Exception(s). Finally, you can use ProcessBuilder.inheritIO() (Sets the source and destination for subprocess standard I/O to be the same as those of the current Java process) like
String filePath = "/dir1/testFile.txt";
try {
System.out.println("No: of lines : ");
ProcessBuilder pb = new ProcessBuilder("wc", "-l", filePath);
pb.inheritIO();
Process p = pb.start();
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
Of course, it's more efficient to count the lines in Java without spawning a new process. Perhaps like,
int count = 0;
String filePath = "/dir1/testFile.txt";
try (Scanner sc = new Scanner(new File(filePath));) {
while (sc.hasNextLine()) {
String line = sc.nextLine();
count++;
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.printf("No: of lines : %d%n", count);
When I execute the command directly
I doubt you're execute it "directly". You're probably running it in a shell.
Your code should run that script in a shell too.
rt.exec(new String[]("bash", "-c", findLineCount});
This is how i printed number of lines
public static void main(String[] args) {
try {
Runtime run = Runtime.getRuntime();
String[] env = new String[] { "path=%PATH%;" + "your shell path " }; //path of cigwin bin or any similar application. this is needed only for windows
Process proc = run.exec(new String[] { "bash.exe", "-c", "wc -l < yourfile" }, env);
BufferedReader reader = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
String s;
while ((s = reader.readLine()) != null) {
System.out.println("Number of lines " + s);
}
proc.waitFor();
int exitValue = proc.exitValue();
System.out.println("Status {}" + exitValue);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
A simple example of using phantomJs in Java will block undefinitely:
public void runPhantomJs(String path, String command) {
Process process;
String outFile = "a11.txt";
try {
process = Runtime.getRuntime().exec(path+ " " + command + " > " +outFile);
int exitStatus = process.waitFor();
//String status = (exitStatus == 0 ? "SUCCESS:" : "ERROR:");
File f = new File(outFile);
if (f.exists()) {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(f),"UTF-8"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
The script execute is very simple, but it returns a whole page on the console:
var webPage = require('webpage');
var page = webPage.create();
page.open('http://www.google.com/', function(status) {
if (status !== 'success') {
console.log('1');
phantom.exit();
} else {
console.log(page.content);
phantom.exit();
}
});
Note that on the pasted code I've added a "> a11.txt" to see if it worked better to read a file instead of reading the output directly. It should be faster, but for some reason it doesn't work. I suppose the redirection > doesn't work.
So I got my code to work. Apparently the output of phantomjs has to be read or the buffer will fill up completely, blocking further execution.
So I think your code will work if you modify it like so:
process = Runtime.getRuntime().exec(path+ " " + command + " > " +outFile);
BufferedInputStream bis = new BufferedInputStream(process.getInputStream());
bis.close();
process.waitFor();
...
If it doesn't work, try using ProcessBuilder. This is my working code:
try {
String phantomJsExe = configuration.getPhantomJsExe().toString();
String phantomJsScript = configuration.getPhantomJsScript().toString();
String urlsTextFile = configuration.getPhantomJsUrlsTextFile().toString();
Process process = new ProcessBuilder(phantomJsExe, phantomJsScript, urlsTextFile).start();
BufferedInputStream bis = new BufferedInputStream(process.getInputStream());
bis.close();
process.waitFor();
} catch (Exception ex) {
ex.printStackTrace();
}
i wanted to perform rename and delete function and the environment is LINUX.
This is the code which I'm using,
String[] command_ary = { "/usr/bin/sh", "-c", command };
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec( command_ary );
But Im getting the following error message,
java.io.IOException: Cannot run program "/usr/bin/sh": java.io.IOException: error=2, No such file or directory
Can anybody help me.
Thank-You
As the exception says, there's no file /usr/bin/sh on your system. If you're looking for the Bourne shell, that's /bin/sh.
public void run() {
//log.info("taskDao:{},concurrentHashSet:{},id:{},shellName:{},shellDate:{}", taskDao, concurrentHashSet, id, shellName, shellDate);
log.info("concurrentHashSet:{},param:{}", concurrentHashSet, param);
int exeState = 99999;
// ssh跳转到其他机器去执行脚本,you can add "ssh rd#g1-jg-hadoop-01 \"source ~/.bash_profile ; bash -x %s %s\"" instead of command
String command = "source ~/.bash_profile ; bash -x %s %s";
String commandF = String.format(command, param.getShellName(), param.getRecallDate());
String[] cmdArr = {"/bin/sh", "-c", commandF};
long taskId = param.getTaskId();
Runtime runtime = Runtime.getRuntime();
Process process = null;
try {
process = runtime.exec(cmdArr);
InputStream stderr = process.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line).append(System.lineSeparator());
}
log.info("exe task thread param:{},commandF:{},execute shell info:{}", param, commandF, sb.toString());
exeState = process.waitFor();
} catch (InterruptedException | IOException e) {
log.error("execute shell error,exeState:{},command:{}", exeState, commandF,e);
} finally {
log.info("execute shell state:{}", exeState);
// 从set中删除 更新表状态 多张表一个脚本,会有问题,其中状态覆盖
if (exeState == 0) {
// 执行成功
taskDao.updateStateByPrimaryKey(taskId, (short) 30, new Date());
// 邮件参数
param.setState(30);
String mailBody = SendMailUtil.beautyMailHtmlLayout(param);
String mailToAddress = param.getMailToUser();
String mailTitle = param.getMailContentTitle();
try {
SendMailUtil.sendMail(mailToAddress, mailTitle, mailBody);
} catch (Exception e) {
e.printStackTrace();
}
}
if (exeState != 0) {
// 执行失败
taskDao.updateStateByPrimaryKey(taskId, (short) 40, new Date());
// 邮件参数
param.setState(40);
String mailBody = SendMailUtil.beautyMailHtmlLayout(param);
String mailToAddress = param.getMailToUser();
try {
SendMailUtil.sendMail(mailToAddress, "回溯任务", mailBody);
} catch (Exception e) {
e.printStackTrace();
}
}
ExecuteTaskModel taskModel = new ExecuteTaskModel();
taskModel.setId(taskId);
log.info("remove task in thread,id:{}", taskId);
boolean remove;
if (concurrentHashSet.contains(taskModel)) {
remove = concurrentHashSet.remove(taskModel);
}
log.info("remove from set success set:{}", concurrentHashSet);
log.info("execute task thread exit!");
System.out.println("Process exitValue: " + exeState);
assert process != null;
process.destroy();
}
}