Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing another application from Java

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.

like image 376
user234194 Avatar asked Aug 12 '10 14:08

user234194


People also ask

What can be used to execute an application in Java?

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.

How do I make a Java program run automatically?

Put you program in this directory: %appdata%\Microsoft\Windows\Start Menu\Programs\Startup Everything in there will be executed at startup.

How do you call a process in Java?

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.

Can you run Java without compiling?

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.


1 Answers

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);
like image 146
Jose Diaz Avatar answered Oct 20 '22 05:10

Jose Diaz