1. Windows environment
In the cmd command window, execute java -jar ***.jar to start the java program. Press ctrl+c or directly close the cmd command window, and the current program will exit.
In the Windows environment, the javaw startup command represents the background running. Close the cmd command window and still run. You can see related services in the task manager, such as
javaw -jar ***.jar
2. Linux environment
The java program start command, execute java -jar ***.jar to start the java program. Press ctrl+c or close the terminal directly, and the current program will exit.
Add "&" at the end of the command, "&" means that the program can be executed in the background, such as
java -jar ***.jar &
However, when the window is closed, the program will also abort the run, and add nohup to the front of the command, so that when the terminal is closed, the program will not hang up and run, such as
nohup java -jar ***.jar &
The print log of the current program will be written to the file in the current directory, and you can modify the write to the specified file, such as
nohup java -jar ***.jar > &
The current program's print log will be written to the file in the current directory. If you do not want to write the log, you can redirect the log to /dev/null. /dev/null represents the empty device file of linux, and all the content written into this file will be lost, such as
nohup java -jar ***.jar > /dev/null &
When only error messages are output to the log, standard output is not written to the log and will be discarded directly, such as
nohup java -jar ***.jar > /dev/null 2> &
When the standard output is redirected to /dev/null, and then the error message output is redirected to the standard output, the error message is also directed to /dev/null, and the error output is also discarded, such as
nohup java -jar ***.jar > /dev/null 2>&1 &
When the standard output is redirected to the standard output, the error message is also directed to the standard output, such as
nohup java -jar ***.jar > 2>&1 &
When you need to stop the java program, find the PID through the following command and terminate the process according to the PID, as shown below
ps -ef | grep ***.jar kill -15 PID
3. Redirection
Redirection is quite convenient in linux. You only need to use > or >> to match. The meaning is as follows
> It is overwrite write,Clear all the original content。 >> It is an additional write,Add at the end of the original content。
- Standard input file (stdin): The file descriptor of stdin is 0, and Unix programs read data from stdin by default.
- Standard output file (stdout): The file descriptor of stdout is 1, and Unix programs output data to stdout by default.
- Standard error file (stderr): The file descriptor of stderr is 2, and Unix programs will write error information to the stderr stream.
In this way, we know that the output/error output information is represented by the numbers 1 and 2 respectively. On the other hand, the above writing method specifies the principle of redirection.
java -jar >log 2>&1 &
Here, 2>&1 means merging error output 2 into 1 and redirecting to a file named log. Symbol >& is a whole and cannot be separated. After separation, it does not mean the above.
2>&1 symbols must be placed behind >log, why? We might as well understand that 1 and 2 are a pointer, and then look at the above statement as follows: Originally, 1----->screen (1 points to the screen), after executing >log, 1----->log (1 points to log), after executing 2>&1, 2----->1 (2 points to 1, and 1 points to log, so 2 also points to log).
Summarize
This is the end of this article about starting jar package command in Java. For more related content on starting jar package commands, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!