I have a Java project that is being built remotely through Jenkins on a Windows server and running some Cucumber tests. Once the tests are finished running, I am creating a process to execute a Putty pscp command to transfer a file over to a linux server. The code works fine when I run it on my local..and it works fine when I manually run the pscp transfer command on the remote windows server where Jenkins is building the project. But for whatever reason when the project is built through Jenkins ...the command is not getting issued. Any ideas? The try Catch is printing out that it successfully has issued the command, however the transferred file is not showing up on the linux server as being updated...so I don't have any failure messages to help me debug this in the output logs.
Some assumptions which I've verified:
The path I'm using for the file that is going to be transferred is correct.
The file is not being transferred.
Some of the code:
String updateJiraLocalPath = System.getProperty("user.dir")+"\\src\\test\\resources\\scripts\\updatejira.sh"; //Eclipse Project path
ProcessBuilder process= new ProcessBuilder("C:\\Putty\\0.6.3\\PSCP.EXE", "-r", "-l", "username#servername", "-pw", "serverPassword", updateJiraLocalPath, "username#servername:/tmp/username/dirToTransferFile");
p = process.start();
try {
final int exitValue = p.waitFor();
if (exitValue == 0){
String line;
System.out.println("Successfully executed the command);
BufferedReader br = new BufferedReader (new InputStreamReader(p.getInputStream()));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
input.close();
}
else {
System.out.println("Failed to execute the command: due to the following error(s):");
try (final BufferedReader b = new BufferedReader(new InputStreamReader(p.getErrorStream()))) {
String line;
if ((line = b.readLine()) != null)
System.out.println(line);
} catch (final IOException e) {
e.printStackTrace();
System.out.println(e);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(e);
}
Related
Basically, I have a problem which is, I am using ProcessBuilder () to run Noxim simulator from java IDE, but neither the shell opened nor the results returned. It just displayed this error :
Exit with error code: 127
I tried the same code to execute the ping command, and it worked and returned the output shown in the shell. I also used the code run Kdeveloper and it worked well.
Note: the path is correct as It worked well in the shell
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("/home/sa/Bureau/NOXIM/noxim/bin/noxim");
try {
Process process = processBuilder.start();
int exitCode = process.waitFor();
System.out.println("\nExited with error code : " + exitCode);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
It's important to read the error stream too. I guess you see some more messages. Have a look here: https://gist.github.com/th-schwarz/041e13ede396a869c7681b5ad637460c
The easiest way is to read the error stream too is:
processBuilder.redirectErrorStream(true);
I have batch file 'turnOff.bat' to turn off sound in system
cd C:\Users\xxx\nircmd\
nircmd.exe mutesysvolume 1
Here is the java spring code that uses that file:
try {
Process process = Runtime.getRuntime().exec(
"cmd /c turnOff.bat", null, new File("/test"));
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
when I run it on intellIj it works normally but when I export the war file to run on tomcat it can't work.
I have button in client, when I click on the button will call this api /manager/sound, Here is the error message after I click:
GET http://localhost:4000/manager/sound 404.
Please help me if you know anything about it!
I use a standard Java routine to execute a terminal command, however I haven't been able to get it to work to run a python file including arguments.
The terminal command (which works on the terminal) is:
python3 umlsConverter.py colon cancer
Where colon cancer is one of N possible string arguments
The Java routine I usually run (from Eclipse) to execute terminal commands is:
public static String execCmdV2(String cmd,String workingDirectoryPath) {
Runtime rt = Runtime.getRuntime();
//String[] commands = {"system.exe","-get t"};
String[] env= {};
Process proc;
File runDir = new File(workingDirectoryPath);
try {
proc = rt.exec(cmd,env,runDir);
} catch (IOException e1) {
System.out.println("Error executing command:" + e1.getLocalizedMessage());
return null;
}
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String fullOutputstring = null;
String s = null;
try {
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
fullOutputstring = fullOutputstring + s;
}
} catch (IOException e) {
System.out.println("Unable to output the results due to error:" + e.getLocalizedMessage());
return null;
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
try {
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
System.out.println("Unable to output the errors due to error:" + e.getLocalizedMessage());
return null;
}
return fullOutputstring;
}
And the error I get, when I run the routine for:
cmd = "python3 umlsConverter.py Breast cancer"
and
`workingDirectoryPath="/Users/n9569065/QuickUMLS"`
is
Error executing command:Cannot run program "python3" (in directory "/Users/n9569065/QuickUMLS"): error=2, No such file or directory
I think the problem has something to do with accessing python3?
use the full path of the python executable. for example: /usr/bin/python3
I'm trying to execute a network testing application via my Java application. Below is the code:
try {
String file = new File("iperf3.exe").getCanonicalPath();
String cmd[] = {file,"-c ping.online.net -P 20 -w 710000 -t"};
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
lblConsole.setText(line);
//System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I got an error saying that the application could not connect to the server. However, when I execute the command in my command prompt, there is no error.
May I know what could I have missed out here?
Managed to solve this issue. It is mainly on how I executed the code in the cmd. Just updated to as below:
String file = new File("iperf3.exe").getCanonicalPath(); String cmd1[] = {file,"-c","ping.online.net","-P","10","-w","710000"};
I want the Apache Cordova CLI being called by a Java Process but unfortunatly the Java Process doesn't wait until it is finished.
This is, how i call it:
StringBuffer sb = null;
String cmd = "cd /location/generated && cordova create MyNewApp"
try {
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
sb = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I've seen many solutions, that say "waitFor()" will do the trick but unfortunatly not for me. I've already tried to always read and compare the last line of code generated by the cordova cmd and finish afterwards, but this is not a good approach. Do you have any suggestions?
Resolved it:
Cmd looks like this "cordova create /path/to/generated/app/ Hello World"
Be aware, the path has to exist before the cmd is beeing called