Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Java Process.waitFor () method?

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Java Process.waitFor () method is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Java Process.waitFor () method

The Process.waitFor () method causes the current thread to wait until the object's process ends before returning the call.

Description

The java.lang.Process.waitFor () method will cause the current thread to wait, if necessary, until the process represented by the Process object has been terminated. This method will return immediately if the child process has been terminated. If the child process has not been terminated, the calling thread will be blocked until the child process exits.

Statement

The following is the declaration of the java.lang.Process.waitFor () method

Public abstract int waitFor () parameter

NA

Return value

The process of returning the exit value returned by this method. By convention, 0 indicates normal termination.

Abnormal

NA

Example

The following example shows how to use the lang.Process.waitFor () method.

Package com.yiibai; public class ProcessDemo {public static void main (String [] args) {try {/ / create a new process System.out.println ("Creating Process..."); Process p = Runtime.getRuntime () .exec ("notepad.exe"); / / cause this process to stop until process p is terminated p.waitFor () / / when you manually close notepad.exe program will continue here System.out.println ("Waiting over.");} catch (Exception ex) {ex.printStackTrace ();}

Compile and run the result:

Creating Process...

Waiting over.

Process.waitFor () has no effect

Recently, when the video needs to be transcoded using ffmpeg to run with cmd, it needs to be operated after the cmd runs successfully. Baidu always says to use the waitfor () method, but it always flashes by and does not play the role of waiting for completion.

Process process = Runtime.getRuntime () .exec ("cmd / c start notepad.exe"); process.waitFor ()

Java's Runtime.getRuntime (). Exec (commandStr) can be called to execute the cmd instruction.

Cmd / c dir closes the command window after executing the dir command.

Cmd / k dir does not close the command window after executing the dir command.

Cmd / c start dir will open a new window and execute the dir instruction, and the original window will close.

Cmd / k start dir will open a new window and execute the dir directive, and the original window will not close.

Process.waitFor (); in this code doesn't work.

The later code continues to run before the calling notepad.exe is finished.

It turns out that it is because of the use of start in exec

The consequence is that process.waitFor () is waiting for a fleeting command-line window to open exe.

Remove the word start and wait until the end of exe for the program to continue.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report