Plivo sending a single message through windows command prompt and java - java

Hi i am trying to run this command that is passed on to the runExternalProgram. When i ran the command through cmd, it was successful and i was able to receive a message but when i run the java program, there was no error issue but no message was received. How should I go about solving this error?
String Command ="cmd /c start java -classpath C:/Users/admin/Desktop/Smsworkspace/sendmessage/src;C:/Users/admin/Desktop/Smsworkspace/sendmessage/src/plivo-java-3.0.9-jar-with-dependencies.jar sendmessage.SendSMS2 +xxxxx +xxxxx CS";
runExternalProgram
public HashMap runExternalProgram_Windows(String Command) {
String _LOC = "[SB_Utilities: runExternalProgram_Windows]";
System.out.println(_LOC + "1.0");
System.out.println("Command is: "+ Command);
String line;
InputStream stderr = null;
InputStream stdout = null;
HashMap _m = new HashMap();
try {
Process process = Runtime.getRuntime ().exec (Command);
System.out.println("Process is: " + process);
Worker worker = new Worker(process);
System.out.println("Worker is: " + worker);
worker.start();
try {
worker.join(180000);
if (worker.exit == null)
{
throw new Exception();
}
}
catch(Exception ex) {
ex.printStackTrace();
_m.put("LOG_ERROR_EXTERNAL", "180 second time out...check connection");
return _m;
}
finally {
process.destroy();
}
stderr = process.getErrorStream ();
stdout = process.getInputStream ();
String _log_output = null;
// clean up if any output in stdout
BufferedReader brCleanUp = new BufferedReader (new InputStreamReader (stdout));
System.out.println("Outcome is: "+brCleanUp.readLine());
while ((line = brCleanUp.readLine ()) != null) {
if (_log_output==null) {
_log_output = line + "\n";
} else {
_log_output = _log_output + line + "\n";
}
}
brCleanUp.close();
_m.put("LOG_OUTPUT", _log_output);
String _log_error = null;
// clean up if any output in stderr
brCleanUp=new BufferedReader (new InputStreamReader (stderr));
System.out.println("Outcome error is "+brCleanUp.readLine());
while ((line = brCleanUp.readLine ()) != null) {
if (_log_error==null) {
_log_error = line + "\n";
} else {
_log_error = _log_error + line + "\n";
}
}
brCleanUp.close();
_m.put("LOG_ERROR_EXTERNAL", _log_error);
} catch (Exception e) {
e.printStackTrace();
}
return _m;
}

Related

Java program hangs itself after executing shell script with ProcessBuilder [duplicate]

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.

How to understand why java process stuck

I have to launch console commands from java to publish to verdaccio.
So it works(pretty bad), but for few packages processes stucks. When I destroy them it returns code 137, witch means not enough memory. I watched in profiler, I have much more free heap, then used.
Maybe it's not because not enough memory.
How to understand why it stucks and how to fix it?
protected String execCommand(List<String> command) throws IOException, NpmAlreadyExistException{
ProcessBuilder pb = new ProcessBuilder(command);
File workingFolder = new File(System.getProperty("user.dir"));
pb.directory(workingFolder);
Process process = pb.start();
try {
boolean finished = process.waitFor(60, TimeUnit.SECONDS);
logger.info("PROC FINISHED: " + finished);
if (!finished) {
process.destroyForcibly();
int exitCode = process.waitFor();
logger.info("PROC EXIT CODE: " + exitCode);
return null;
}
} catch (InterruptedException e) {
logger.info("PROCESS WAS INTERRUPTED!!!");
logger.info(e.getMessage());
return null;
}
logger.info("PROC EXIT CODE: " + process.exitValue());
String s;
BufferedReader stdErr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
StringBuilder err = new StringBuilder();
while ((s = stdErr.readLine()) != null) {
err.append("\n").append(s);
}
stdErr.close();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder result = new StringBuilder();
while ((s = stdInput.readLine()) != null) {
result.append("\n").append(s);
}
stdInput.close();
process.destroy();
logger.info(String.format("execCommand response [stdin]: %s", result));
logger.info(String.format("execCommand response [stdErr]: %s", err));
if (err.length() != 0) {
if (err.toString().contains("Update the 'version' field in package.json and try again.")) {
throw new NpmAlreadyExistException("Пакет с таким именем и версией уже существует в репозитории.");
}
}
return result.toString();
}
Thank you!

SQLLoader 704 Internal error while loading through JAVA

Getting following error on executing sqlldr command.
SQL*Loader-704: Internal error: ulconnect: OCIServerAttach [0]
ORA-12154: TNS:could not resolve the connect identifier specified
Following is the sqlldr cmd :
sqlldr BILLING/'"Bill!ng#123#"'#10.113.242.162:1521/bssstc control=/log/bssuser/CDR/Postpaid_CDR_Log/CTRL_File.ctrl log=/log/bssuser/CDR/Postpaid_CDR_Log/LOG_File.log direct=false silent=header skip_unusable_indexes=true rows=200000 bindsize=20000000 readsize=20000000 ERRORS=25000
Note :- When executing the same through command prompt its getting succes.
Following is the code snipt i tried.
Runtime rt = Runtime.getRuntime();
Process proc = null;
try {
proc = rt.exec(sqlLoaderCommand);
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null){
logger.info(line);
}
int exitVal = proc.waitFor();
logger.info("Process exitValue: " + exitVal);
int returnValue = proc.exitValue();
String str = null;
if (returnValue != 0) {
InputStream in = proc.getInputStream();
InputStreamReader preader = new InputStreamReader(in);
BufferedReader breader = new BufferedReader(preader);
String msg = null;
while ((msg = breader.readLine()) != null) {
logger.info(msg);
str = str + msg;
}
System.out.flush();
preader.close();
breader.close();
in.close();
InputStream inError = proc.getErrorStream();
InputStreamReader preaderError = new InputStreamReader(inError);
BufferedReader breaderError = new BufferedReader(preaderError);
String errorMsg = null;
while ((errorMsg = breaderError.readLine()) != null) {
logger.info("Copy Error: " + errorMsg);
str = str + errorMsg;
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
I think error could be in your username - "Bill!ng#123#". Check out the quotes escaping rules in Java. Like:
String str = "BILLING/'\"Bill!ng#123#\"'#10.113.242.162:1521";
1) From command line try to execute tnsping 10.113.242.162
if the tool will return full description you have to set "oracle.net.tns_admin" in java.
System.setProperty("oracle.net.tns_admin", "ORACLE_DIRECTORY/network/admin"); -- put correct path here and execute before your code
2) For test you can try to use full connection description .
instead of 10.113.242.162:1521/bssstc ->
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.113.242.162)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=bssstc))

Running a shell script using ProcessBuilder

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};

wait for method of process class doesn't return

I have Process Class in my code and that couldn't read the inputbufferreader data when i try to reading the larger number of data from inputbuffer and waitfor method of process class never return anything
this issue is happened in live server
but below code running fine and read the all the data from inputbuffer in local server
private static JSONObject ExecJniApp(String inputJsonString) throws JniException
{
int exitStatus = 0;
String workingDirectory = JniSettings.getJniAppDirectory();
String command = workingDirectory + binaryName;
System.out.println("ExecJniApp: inputJsonString: " + inputJsonString);
String outputString = "";
try
{
Process p = Runtime.getRuntime().exec(command, null, new File(workingDirectory));
// send input vi stdin
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writer.write("\n>>>>>>>>>>");
writer.write(inputJsonString);
writer.write("<<<<<<<<<<\n");
writer.flush();
if (JniSettings.isLinux())
{
p.waitFor();
}
else
{
// System.out.println("ExecJniApp: windows wait for");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
for (;;)
{
String line = reader.readLine();
if (line == null)
{
break;
}
// System.out.println(line);
outputString += line + "\n";
}
// exit code of command and log error detail
// exit status = 0 -> Success
exitStatus = p.exitValue();
if (exitStatus != 0)
{
System.out.println("ExecJniApp: inputJsonString: " + inputJsonString);
System.out.println("ExecJniApp: outputString:\n" + outputString);
throw new Exception("Exit status other than zero :- " + exitStatus + "\noutput: ");
}
}
catch (Exception e)
{
// System.out.println("ExecJniApp failed");
e.printStackTrace();
throw new JniException("JniInterface failed");
}
System.out.println("ExecJniApp: outputString:\n" + outputString);
int index1 = outputString.indexOf(">>>>>>>>>>");
if (index1 == -1)
{
// System.out.println("ExecJniApp failed, Invalid output format");
throw new JniException("ExecJniApp failed, Invalid output format");
}
index1 += 10;
int index2 = outputString.indexOf("<<<<<<<<<<");
if (index2 == -1 || index2 <= index1)
{
// System.out.println("ExecJniApp failed, Invalid output format");
throw new JniException("ExecJniApp failed, Invalid output format");
}
String outputJsonString = outputString.substring(index1, index2);
// System.out.println("ExecJniApp: outputJsonString: " + outputJsonString);
JSONParser parser = new JSONParser();
JSONObject obj = null;
try
{
obj = (JSONObject) parser.parse(outputJsonString);
}
catch (ParseException e)
{
// System.out.println("ExecJniApp failed, Json parse failed");
e.printStackTrace();
throw new JniException("JniInterface failed, Json parse failed");
}
Object errorObject = obj.get("error");
if (errorObject != null)
{
String errorString = errorObject.toString();
// System.out.println("ExecJniApp failed, " + errorString);
throw new JniException("ExecJniApp failed, " + errorString);
}
return obj;
}
Just replace your code with below code.
Must close your InputStream connection.
Process p = Runtime.getRuntime().exec(command, null, new File(workingDirectory));
// send input vi stdin
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writer.write("\n>>>>>>>>>>");
writer.write(inputJsonString);
writer.write("<<<<<<<<<<\n");
writer.flush();
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
for (;;)
{
String line = reader.readLine();
if (line == null)
{
break;
}
// System.out.println(line);
outputString += line + "\n";
}
p.getOutputStream().close();
p.getInputStream().close();
}
catch(Exception e)
{
e.printStackTrace();
}
if (JniSettings.isLinux())
{
p.waitFor();
}
else
{
// System.out.println("ExecJniApp: windows wait for");
}
// exit code of command and log error detail
// exit status = 0 -> Success
exitStatus = p.exitValue();
if (exitStatus != 0)
{
System.out.println("ExecJniApp: inputJsonString: " + inputJsonString);
System.out.println("ExecJniApp: outputString:\n" + outputString);
throw new Exception("Exit status other than zero :- " + exitStatus + "\noutput: ");
}

Categories