國(guó)內(nèi)做日化官方網(wǎng)站怎么推廣軟件
-
java代碼生成好的.exe啟動(dòng)nginx服務(wù)程序
-
根據(jù)nginx占用端口來解決nginx服務(wù)重復(fù)啟動(dòng)問題(下面代碼了解代碼邏輯后根據(jù)自己的業(yè)務(wù)需求修改即可)
代碼:
package org.example;import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.List;/*** @author ${USER}* @author ${DATE}*/
public class Main {public static boolean isPortUse(int port) {try (Socket socket = new Socket()) {socket.connect(new InetSocketAddress("localhost", port));return true;} catch (Exception e) {return false;}}public static int portNginxConf() {Path nginxConfPath = Paths.get(".\\conf\\nginx.conf"); // 修改為nginx.conf文件的實(shí)際路徑int port = 0;try {List<String> lines = Files.readAllLines(nginxConfPath);for (String line : lines) {if (line.contains("listen")) {if (line.indexOf("#") != -1) {line = line.split("#")[0];}if (line.indexOf(";") != -1) {line = line.split(";")[0];}line = line.split("listen")[1];port = Integer.parseInt(line.trim());System.out.println("Port: " + port);break; // 如果只需要找到一個(gè)端口號(hào),就退出循環(huán)}}} catch (IOException e) {e.printStackTrace();}return port;}public static void main(String[] args) {try {int port = portNginxConf();if (port != 0) {if (isPortUse(port) == false) {ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", "start nginx"); // /c參數(shù)表示執(zhí)行后關(guān)閉CMD窗口Process process = processBuilder.start();TrayIcon trayIcon = new TrayIcon(new ImageIcon("nginx.png").getImage());SystemTray.getSystemTray().add(trayIcon);// 等待命令執(zhí)行完成process.waitFor();}}} catch (AWTException e) {throw new RuntimeException(e);} catch (InterruptedException | IOException e) {throw new RuntimeException(e);}}
}
- 啟動(dòng)nginx用到的nginx.exe 和nginx圖標(biāo)我已經(jīng)進(jìn)行隱藏顯示