Recently, I encountered a scenario in my graduation project: I need to dynamically package an existing project when running a web project.
Because we have encountered similar situations in some projects before, I have positioned the solution to the runtime class.
Speaking of the runtime class, there are not many actually used. Previously, this class was used in the project to obtain the JVM runtime environment through runtime, execute the window command to obtain the MAC address of the machine. The following is a simple example. If you use runtime to obtain and execute the window command "ipconfig-all", if you obtain the MAC address, I don't need to talk about it. Just create a regular expression and match it.
Stringbuilder sb = new stringbuilder ();
SB. append ("ipconfig-all ");
Try {
Process = runtime.getruntime(cmd.exe C (sb. tostring ());
Bufferedreader BR = new bufferedreader (New inputstreamreader (process. getinputstream ()));
String line = NULL;
While (line = Br. Readline ())! = NULL ){
System. Out. println (line );
}
} Catch (ioexception e ){
E. printstacktrace ();
}
If you want to execute other commands, the method is similar and I will not give an example one by one. Next, I will summarize some of my points below so that you can avoid detours.
1. If you want to execute some cmd commands, in many cases, if you directly plug in Exec like ipconfig above, it is likely that an error is reported, saying that the command is not supported. The solution is to open the CMD console. The command is as follows: "CMD/c Cd D:/foldera/floderb". First, you must describe the console opened in this way, the default path is the path of the current file. If you want to go to directory A/directory B and make some mistakes, you can use the following command. However, the above command is meaningless. It just changes the default directory of CMD and does nothing else. What if I want to execute multiple commands? We can use "&" and computation. If you do not understand this operator, check it yourself. Now, let's change the command, "CMD/c Cd D:/foldera/floderb & mkdir ABC. In this way, we can create a directory named ABC under the Directory D:/foldera/floderb.
Execute commands in this way. But if you want to print something in the console during the execution of the command, you have to write more code. Otherwise, your program will not be automatically executed, it seems that the last time it got stuck. In fact, this is not the case. When the command is executed, JDK sends the command output to the cache area, and I find that if you do not clear the cache area, the program will not end. That is to say, the command you run is not completed, even though it seems to be finished in your own debugging output. At this time, process = runtime.getruntime(cmd.exe C (command) must be used. Process is a sub-process, which has two methods to obtain the content of the cache, one is process. geterrorstream () is used to obtain information about command execution errors, and the other is process. getinputstream () is used to obtain the normal output of command execution. Well, if you encounter a program that cannot automatically end normally, use these two methods to return an inputstream object and then clean the object read, your program will automatically end normally.
3. Sometimes, the console outputs Chinese characters. You must select the read operation class. Otherwise, Chinese characters are garbled.