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 use of Java Runtime?

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

Share

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

This article will explain in detail what is the use of Java Runtime, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Preface

Recently, to do the project framework, you need to close the server connection and clear part of the framework to run lock files at the end of the framework. Here you think of shutdownhook and learn how to use Runtime by the way.

1. Shutdownhook

Demo example, which proves that it will be called when the program ends normally. If kill-9, it will definitely not be called.

Public class ShutdownHookTest {public static void main (String [] args) {System.out.println ("= application start="); Runtime.getRuntime () .addShutdownHook (new Thread (()-> {System.out.println ("- hook 1 -");})) Runtime.getRuntime () .addShutdownHook (new Thread (()-> {System.out.println ("- hook 2 -");})); System.out.println ("= application end=");}}

The normal operation ends, and the result is as follows

= application start=

= application end=

-hook 1-

-hook 2-

Process finished with exit code 0

If paused, click the square red icon in the lower left corner of the image below to stop the running application

The result is as follows, shutdownhook has been executed.

Shutdownhook can delete files, close connections and so on when the program ends normally.

2. Exec execution 2.1 regular command execution

Examples of demo are as follows, such as ls

Public class ShutdownHookTest {public static void main (String [] args) throws InterruptedException, IOException {Process process = Runtime.getRuntime (). Exec ("ls"); try (InputStream fis = process.getInputStream (); InputStreamReader isr = new InputStreamReader (fis); BufferedReader br = new BufferedReader (isr)) {String line While ((line = br.readLine ())! = null) {System.out.println (line);}

The results are as follows

And the result of normal execution

But this method has the risk of remote execution, that is, executing specific instructions, such as rm-rf *, on the browser side, and the result is very...

2.2 Pipe symbol

But it will fail after meeting the pipe character, what is the solution, sh-c, but you can't use it directly, otherwise you will get the TTY window information.

Public static void main (String [] args) throws IOException {Process process = Runtime.getRuntime (). Exec ("sh-c ps aux | grep java"); try (InputStream fis = process.getInputStream (); InputStreamReader isr = new InputStreamReader (fis); BufferedReader br = new BufferedReader (isr)) {String line While ((line = br.readLine ())! = null) {System.out.println (line);}

As a result?

The parameter of sh-c should be separated, otherwise runtime will think it is a parameter.

2.3 Source code analysis

Track the code and use ProcessImpl to execute instructions

Public Process exec (String [] cmdarray, String [] envp, File dir) throws IOException {return new ProcessBuilder (cmdarray) .environment (envp) .directory (dir) .start ();}

ProcessBuilder

/ / Only for use by ProcessBuilder.start () static Process start (String [] cmdarray, java.util.Map environment, String dir, ProcessBuilder.Redirect [] redirects, boolean redirectErrorStream) throws IOException {assert cmdarray! = null & & cmdarray.length > 0; / / Convert arguments to a contiguous block It's easier to do / / memory management in Java than in C. Byte [] [] args = new byte [cmdarray.length-1] []; int size = args.length; / / For added NUL bytes for (int I = 0; I < args.length; iota +) {args [I] = cmdarray [I + 1] .getBytes (); size + = args.length } byte [] argBlock = new byte [size]; int I = 0; for (byte [] arg: args) {System.arraycopy (arg, 0, argBlock, I, arg.length); I + = arg.length + 1; / / No need to write NUL bytes explicitly} int [] envc = new int [1] Byte [] envBlock = ProcessEnvironment.toEnvironmentBlock (environment, envc); int [] std_fds; FileInputStream f0 = null; FileOutputStream f1 = null; FileOutputStream f2 = null; try {if (redirects = = null) {std_fds = new int [] {- 1,-1,-1} } else {std_fds = new int [3]; if (redirects [0] = = Redirect.PIPE) std_fds [0] =-1; else if (redirects [0] = = Redirect.INHERIT) std_fds [0] = 0 Else {f0 = new FileInputStream (redirects [0] .file ()); std_fds [0] = fdAccess.get (f0.getFD ());} if (redirects [1] = = Redirect.PIPE) std_fds [1] =-1 Else if (redirects [1] = = Redirect.INHERIT) std_fds [1] = 1; else {F1 = new FileOutputStream (redirects [1] .file (), redirects [1] .append ()) Std_fds [1] = fdAccess.get (f1.getFD ());} if (redirects [2] = = Redirect.PIPE) std_fds [2] =-1; else if (redirects [2] = = Redirect.INHERIT) std_fds [2] = 2 Else {f2 = new FileOutputStream (redirects [2] .file (), redirects [2] .append ()); std_fds [2] = fdAccess.get (f2.getFD ()) }} return new UNIXProcess (toCString (cmdarray [0]), argBlock, args.length, envBlock, envc [0], toCString (dir), std_fds, redirectErrorStream) } finally {/ / In theory, close () can throw IOException / / (although it is rather unlikely to happen here) try {if (f0! = null) f0.close ();} finally {try {if (F1! = null) f1.close ();} finally {if (f2! = null) f2.close () }}

New UNIXProcess environment

/ * java.lang.Process subclass in the UNIX environment. * * @ author Mario Wolczko and Ross Knippel. * @ author Konstantin Kladko (ported to Linux and Bsd) * @ author Martin Buchholz * @ author Volker Simonis (ported to AIX) * / final class UNIXProcess extends Process {3. Summary

Runtime is of many uses, at the bottom.

For example, call gc

Load jar Fil

Runtime is powerful, but it needs to be exploited reasonably. Many attacks are executed through Runtime.

However, it is convenient to use shutdownhook for subsequent processing of stopping tasks.

On the use of Java Runtime what is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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