I have a processbuilder that runs a .sh script. it opens a terminal. I want to destroy this terminal later. I tried process.destroy() but it did not do the job.
Code:
Process p = new ProcessBuilder("/usr/bin/gnome-terminal", "-e", "/home/omar/ros_ws/./baxter2.sh").start();
try {
Thread.sleep(10000); // wait for one second
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
p.destroy();
try
{
Process[] proc = new Process[2];
proc[0] = new ProcessBuilder("calc.exe").start();
proc[1] = new ProcessBuilder("notepad.exe").start();
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
}
proc[0].destroy();
proc[1].destroy();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
Related
I have following problem...I created a Process via ProcessBuilder in this way :
private ProcessBuilder processBuilder;
private Process process;
public void init() {
processBuilder = new ProcessBuilder(
"java", "-jar",
"bam.jar",
host,
);
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
try {
process = processBuilder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
and I have function who should a kill process :
public void stop() {
process.destroy();
try {
process.waitFor(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (process.isAlive()) {
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
process.destroyForcibly();
}
}
But killing process sometimes work, but sometimes doesn't work. Any idea?
had similar problem, resolved it by replacing processBuilder.start() with
java.lang.Process process = java.lang.Runtime.getRuntime().exec("command java -jar some.jar");
proccess.destroy();
I want to do following operation in ordered wise
1. Stop jetty server
2. Delete used resource from jetty
3. Restart jetty server.
I have done this above using shutdownhook in java as below :
<code>
Thread restartThread = new Thread(){
public void run(){
try {
sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String osName = System.getProperty("os.name");
logger.debug("OS name:" + osName);
if (osName != null
&& osName.toUpperCase().startsWith("WINDOWS")) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
List<String> cmdLine = new ArrayList<String>();
cmdLine.add("cmd.exe");
cmdLine.add("/C");
cmdLine.add("start");
cmdLine.add("\"\"");
cmdLine.add(getBaseDir() + File.separator + "restart.bat");
final ProcessBuilder pb = new ProcessBuilder(cmdLine);
Process process = pb.start();
if (process.exitValue() == 0) {
// after stopping server delete stores
deleteCertificates();
// restores files
restoreFiles(tmpdir, backupfilelist);
}
//p.waitFor();
} catch (IOException e) {
logger.error("Failed to restart:" + e.getMessage(), e);
}
}
});
System.exit(0);
} else {
try {
Runtime.getRuntime().exec("service appservice restart");
} catch (IOException e) {
logger.error("Failed to restart:" + e.getMessage(), e);
}
}
}
};
restartThread.start();
<code>
my concern is will it do it sequentially execution, otherwise application will fail to restore.
I tried to use this code to delete a file located into /data folder but it doesn't work, what's wrong in it? My device has root.
Runtime.getRuntime().exec(new String[]{"su","rm"+" "+"/data/logger"});
SOLVED USING THIS
Process p;
try {
p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("rm /data/logger"+"\n");
os.writeBytes("exit\n");
os.flush();
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Use the -f switch to force the delete.
File exists case is taken care by runtime class.
Try this:
Runtime.getRuntime().exec(new String[]{"su","rm","-f","/data/logger"});
or this
Runtime.getRuntime().exec("su")
Runtime.getRuntime().exec(new String[]{"rm","-f","/data/logger"});
I have two arrays that I want to print to separate files. Here's my code:
try {
PrintStream out = new PrintStream(new FileOutputStream(
"Edges.txt"));
for (i = 0; i < bcount; i++) {
out.println(b[i][0] + " " + b[i][1]);
}
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
try {
PrintStream out = new PrintStream(new FileOutputStream(
"Nodes.txt"));
for (i = 0; i < bigbIter; i++) {
out.println(bigb[i]);
}
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
If I only use the first set of try / catch / catch, it works perfectly. But when I use both it doesn't work, giving me the errors "illegal start of type ... } catch" and "error: class, interface, or enum expected". What am I doing wrong?
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
You have an extra }, which throws off the parser and gives you lots of errors.
You should write a method to write to the file. Just pass the file name and data. You should see that you have too many closing brackets, get your IDE to highlight brackets.
Lesson is just don't copy/paste and then edit the catch block when you want it again!
Edit: Also in java 7 you can have multiple catches in one block, it is better to do this:
catch (FileNotFoundException | IOException e)
{
}
What's the difference between running programs using java and run it using the command line? In the first case it does not work, but in the second case it works fine.
Java:
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("../../../my/prog \"//10.124.12.15/C:/output/*\" ../../../input/345 -N -A");
DataInputStream bis = new DataInputStream(proc.getInputStream());
int _byte;
while ((_byte = bis.read()) != -1)
System.out.print((char)_byte);
proc.waitFor();
} catch (IOException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
AND command:
../../../my/prog "//10.124.12.15/C:/output/*" ../../../input/345 -N -A
Try using absolute path. Maybe that's your problem.
Thanks all, I solved my problem:
try {
String cmd="/progs/my/prog //10.124.12.15/C:/output/* /temp/input/345 -N -A"
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(
new String[]{"/usr/bin/bash", "-c", cmd, "1>/dev/null 2>&1"});
proc.waitFor();
} catch (IOException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}