I need to execute a batch file which executes another Java application. I don't care whether it executes successfully or not and I don't have to capture any errors.
Is it possible to do this with ProcessBuilder? What are the consequences if I do not capture errors?
However, my requirement is just to execute another Java application.
The java and javaw tools start a Java application by starting a Java Runtime Environment and loading a specified class. On AIX, Linux, and Windows systems, the javaw command is identical to java, except that javaw has no associated console window. Use javaw when you do not want a command prompt window to be displayed.
Put you program in this directory: %appdata%\Microsoft\Windows\Start Menu\Programs\Startup Everything in there will be executed at startup.
To call a method in Java, write the method's name followed by two parentheses () and a semicolon; The process of method calling is simple.
Starting with Java SE 11 and for the first time in the programming language's history, you can execute a script containing Java code directly without compilation. The Java 11 source execution feature makes it possible to write scripts in Java and execute them directly from the *inx command line.
The Runtime.getRuntime().exec()
approach is quite troublesome, as you'll find out shortly.
Take a look at the Apache Commons Exec project. It abstracts you way of a lot of the common problems associated with using the Runtime.getRuntime().exec()
and ProcessBuilder
API.
It's as simple as:
String line = "myCommand.exe";
CommandLine commandLine = CommandLine.parse(line);
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
int exitValue = executor.execute(commandLine);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With