Alright, I have a shutdown hook that makes sure I close my connection nicely whenever I close. What I need to do now is open another program (terminal emulator) then close mine while leaving the terminal emulator open. I am able to open the emulator but the java program doesn't close until the emulator closes. How can I run something and close my program out?
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
#Override
public void run() {
if (connection != null) {
try {
connection.close();
System.out.println("Connection Closed");
try {
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "C:\\MVbase\\mvterm.lnk");
Process p = pb.start();
System.exit(0);
} catch (IOException e) {
System.out.println(e);
}
} catch (MVException e) {
System.out.println(e.getMessage());
}
}
}
}, "Shutdown-thread"));
The easiest way to do it is to make a shell script or batch file that runs the program then shuts it down and starts the terminal emulator.
#!/bin/sh
cd C:/location/of/program
javac program.java
java program
kill program
cd C:/location/of/terminalemulator
run terminalemulator
Save this as a batch file and it should do it all from the command line.
Related
I have a code piece about killing the chrome drivers on Selenium/Java:
Runtime.getRuntime().exec("taskkill /f /im chromedriver.exe");
When I run this code piece in Eclipse, it works and kills all drivers but when I create jar file from same project and run this jar, exec command does not working and does not kill drivers.
public class CommonKillAllDrivers {
public void killAllDrivers() throws InterruptedException {
try{
Runtime.getRuntime().exec("taskkill /f /im chromedriver.exe");
}
catch (Exception e) {
System.out.println("error in catch");
}
}
}
This code is called from afterTest method:
#AfterTest(alwaysRun = true)
public void killDrivers() throws InterruptedException {
CommonKillAllDrivers killDrivers = new CommonKillAllDrivers();
killDrivers.killAllDrivers();
}
So I want this: all opened drivers while created with tests are killed at the end. ( I am observing these chromedriver.exe in Task Manager)
But when I run jar file from Powershell:
java -jar "C:\Users\mervey\eclipse-workspace\tvb2b\target\b2b-selenium-jar-with-dependencies.jar" "C:\Users\mervey\eclipse-workspace\tvb2b\testng-suite.xml"
...this line of code is not working and not kill drivers.
I am currently working on a programm where I try to open another application (such as eg Notepad) from a link.
As sometimes it will take some time to open that programm, I wanted to implement either a loading screen, or simply indicate the program being loaded by changing the cursorstyle, but only for the period, until the application has been launched and is running.
Whenever I try to implement this feature the cursor is only changed after the application is already running.
The application is launched via the following method:
public void run(String path) {
try {
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec(path);
try {
Thread.sleep(5000);
} catch (Exception e) {
new Warnings().AppOpenError(dataBean.getLinkPath());
}
process.destroy();
dataBean.getPrimaryStage().getScene().setCursor(Cursor.DEFAULT);
} catch (IOException e) {
new Warnings().ioException(e);
}
}
How can I achieve the loading bar only being shown during loadingtime of the application?
I want to run a jar script from within a Java program. The command is tested to run without problems if pasted sloley to the Windows cmd.
However, my Java script stucks at p.waitFor().
public class MyClass{
public static void main(String[] args) {
try {
// create a new process
System.out.println("Creating Process...");
Process p = Runtime.getRuntime().exec("U:\\locallib\\jdk-8u65-windows-x64\\tools\\bin\\java.exe -Xmx4g -jar U:\\path\\to\\my.jar arg1 arg2");
System.out.println("Process is running...");
p.waitFor();
System.out.println("Process is terminated...");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
Ouput:
Creating Process...
Process is running...
...and nothing else.
i want to run a specified file (.exes) in background...
File file = new File ("C:\\Documents and Settings\\INTEL\\My Documents\\NetBeansProjects\\demo\\calc.exe");
Desktop.getDesktop().open(file);
what if i use Runtime stuff
Executes the specified string command in a separate process.
Process process = Runtime.getRuntime().exec("C:\\Documents and Settings\\INTEL\\My Documents\\NetBeansProjects\\demo\\calc.exe");
You could execute your process in another thread
Thread thread = new Thread(){
public void run(){
try {
Runtime.getRuntime().exec("...");
} catch (Exception e) { ... }
}
}
thread.start();
I have a UI wrapper for a jar. When the event 'submitButton' is triggered from the UI, the following method is called to execute it. The jar executes in a separate process, but the mail is sent only after the calling process (i.e. UI server) is stopped. Any idea why ? (The jar works fine when executed from cmd line).
public static String doSendMail(){
// Run a java app in a separate system process
Process process;
try {
ProcessBuilder pb = new ProcessBuilder("java", "-jar", "SendMail.jar", ">>", "test.log");
process = pb.start();
InputStream in = process.getInputStream();
InputStream err = process.getErrorStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Success";
}
A couple of problems: >> is shell redirection. You are running the subprocess directly, so it probably got ignored. Also, you never handled the output written from the process, so it likely got buffered and blocked the process until the UI ended.