The backend of Windows starts the Jar project of SpringBoot
Use Spring Boot JAR files in Windows for background startup and output logs to specified locations.
Follow the steps below
- 1. Make sure that the Java Runtime Environment (JRE) or Java Development Kit (JDK) is installed.
- 2. Open a command prompt or PowerShell and enter the directory containing the Spring Boot JAR file.
- 3. Use the following command to start the application and output the log to the specified location:
java -jar > 2>&1 &
Replace with the actual Spring Boot JAR file name, the file path and name of the file you want to save the log.
> Redirecting standard output to document。 2>&1 Set standard errors(stderr)Redirect to the same location as standard output,It will also be written to document中。 After executing the command,The application will run in the background,并将日志输出保存到指定的document中。
Errors may occur when using the & symbol in PowerShell
You can try the following to start the application and output the logs to the specified location:
1. Open PowerShell and enter the directory containing the Spring Boot JAR file.
2. Use the following command to start the application and output the log to the specified location:
Start-Process java -ArgumentList '-jar', '' -RedirectStandardOutput -RedirectStandardError logfile_err.log
Replace with the actual Spring Boot JAR file name, logfile_err.log is the file path and name where you want to save the log.
3. After executing the command, the application will run in the background and save the log output to the specified file.
- This method uses PowerShell's Start-Process command to start the Java process and redirects standard output and standard errors to the same log file through the -RedirectStandardOutput and -RedirectStandardError parameters.
- Note that PowerShell sometimes raises errors for certain special characters (such as >), so it is recommended to enclose the file paths in the command in double quotes to avoid potential problems.
If you want to view a log file named , you can use the following command to execute it in PowerShell:
Get-Content -Path -Wait
This command will display the latest content of the file in real time and continuously monitor the changes in the file.
When a file is updated, PowerShell will automatically display the new content.
Please note:
- Real-time reading of log files may have a certain impact on system performance, especially when the log files are very large or are updated frequently.
- Therefore, it is recommended to use this method when necessary and stop reading the log file in time after use.
- You can stop real-time reading using the Ctrl+C key combination.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.