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

How to write runtime.exec() in java

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to write runtime.exec () in java. It is very detailed and has a certain reference value. Friends who are interested must finish it!

First of all, let's talk about the Runtime class, which is a class related to the JVM runtime environment, which is Singleton. I'll talk about a few places that I think are important.

1. Runtime.getRuntime () can get the runtime environment of the current JVM, which is the only way to get the runtime environment in Java.

2. Most of the other methods on Runtime are instance methods, that is, the getRuntime method is used every time you make a run-time call.

3. The exit method in Runtime is probably the only way to exit the current JVM, because I see that exit in the System class actually exits JVM by calling Runtime.exit (). This explains the general rule of Java on the return value of Runtime (also mentioned later). 0 means normal exit, and non-0 represents abnormal abort. This is only the rule of Java, and there will always be some small confusion in various operating systems.

4. The Runtime.addShutdownHook () method can register a hook in the process of JVM executing shutdown, as long as the parameter of the method is an initialized but not executed Thread instance. (note that the Thread in Java is worthless after execution.)

5. When it comes to the addShutdownHook method, we should talk about the circumstances under which the JVM runtime environment is shutdown or abort. According to the document, when the last non-wizard process exits or receives a user interrupt signal, user logout, system shutdown, and the exit method of Runtime is called, JVM starts the shutdown process, and after the process starts, he starts all registered shutdown hook in parallel (note that it starts in parallel, which requires thread safety and deadlock prevention). When the shutdown procedure starts, you can abort the shutdown procedure and exit the JVM only by calling the halt method.

So when will JVM abort quit? First of all, when abort exits, JVM stops running but does not necessarily shutdown. This only occurs when JVM encounters a SIGKILL signal or a windows signal to abort a process, or an internal error such as accessing an illegal address occurs in a local method. In this case, there is no guarantee that the shutdown hook will be executed.

[@ more@] starts with all the overloads of the Runtime.exec () method. One thing to note here is public Process exec (String [] cmdArray, String [] envp); in this method, cmdArray is a string array of executed commands and parameters, the first element of the array is the command to be executed, and then the parameters of the command. Envp I personally feel that it should be the same as the environment variables in execve in C, and name=value is used in envp.

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