Lang/Java

Java jar 윈도우 서비스 등록하기

hamaganatanadda 2024. 12. 22. 15:23

2개 버전 테스트

Eclipse Adoptium jdk-8.0.412.8-hotspot, 윈도우 11

JDK 1.8 202, 윈도우 서버 2019

WinSW(WinSW.NET4.exe) v.2.12.0 

Spring Tool Suite 4 Version: 4.13.0.RELEASE

 

javaSource.zip
0.01MB
serviceTest.zip
1.96MB

1. apache common daemon

자료가 많이 없다.

 

2. nssm

블로그에 참고 자료가 매우 많고 무난하게 사용하기 좋아보였다.

Bugs in nssm can cause severe issues such as excessive CPU usage, memory exhaustion, data loss and even bluescreens.

CPU, 메모리, 블루스크린 등의 버그가 생길 수도 있다고 하는데 중요한 서버라서 문제가 생길까봐 사용하지 않았다.

검증은 충분이 되었을거 같긴 하다.

https://nssm.cc/builds

 

NSSM - the Non-Sucking Service Manager

NSSM - the Non-Sucking Service Manager All builds nssm is built using the Jenkins continuous integration server. And yes, the build slave is controlled by nssm itself, like this: nssm install Jenkins %PROGRAMFILES%\Java\jre7\bin\java.exe nssm set Jenkins A

nssm.cc

https://nssm.cc/

 

 

3. winsw

아래 블로그가 설명이 잘 되어 있다.  샘플 자료도 나중에 볼 수 있게 업로드해두었다.

exe와 xml 파일 이름은 동일해야한다.

 

파일다운 

여러 개가 있는데 .net 버전 기준으로 보면 되는 거 같다.

일반적으로 WinSW.NET4로 하는거 같고 WinSW-x64.exe를 써도 동작을 하는 것으로 확인했다. 용량이 클뿐.

WinSW-x64.exe
WinSW-x86.exe
WinSW.NET2.exe
WinSW.NET4.exe

https://github.com/winsw/winsw/releases/tag/v2.12.0

 

Release WinSW v2.12.0 · winsw/winsw

🐛 Bug fixes Fix download exceptions when ignoring errors (#854 <- #853) 📦 Packaging changes No self extraction (#807 <- #806)

github.com

 

 

필요한 파일

 


등록, 삭제

 

serviceTest.xml

<?xml version="1.0" encoding="UTF-8"?>
<service>
    <id>serviceTestID</id>
    <name>serviceTestName</name>
    <description>serviceTest description</description>
    <env name="JAVA_HOME" value="C:\Program Files\Eclipse Adoptium\jdk-8.0.412.8-hotspot"/>
    <executable>C:\Program Files\Eclipse Adoptium\jdk-8.0.412.8-hotspot\bin\java</executable>
    <arguments>-jar test.jar paramtest1 paramtest2</arguments>
    <logpath>C:\serviceTest\logs</logpath>
    <logmode>rotate</logmode>
</service>

 

등록 된 서비스

 

로그 파일

C:\serviceTest\logs

serviceTest.out.log 내용이고 파라미터도 찍힌다.

error가 나면 err.log가 찍힌다.

args: [paramtest1, paramtest2]
System.out.println: 2024-12-22 15:40:43
System.out.println: 2024-12-22 15:40:44
System.out.println: 2024-12-22 15:40:45

 

자바 소스 log4j2 도 사용하여 따로 찍히게 하였다.

package com.maven;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

class Test {
    private static final Logger log = LogManager.getLogger(Test.class);
    private static final DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
    public Test() {
        start();
    }
    
    private void start() {
        while (true) {
            try {
                log.info("info: " + LocalDateTime.now().format(f));
                System.out.println("System.out.println: " + LocalDateTime.now().format(f));
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                
            }
        }
    }
}

public class App {
    public static void main(String[] args) {
        if (args.length > 0) {
            System.out.println("args: " + Arrays.toString(args));
        }
        new Test();
    }
}

 

logj4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout disableAnsi="false"  pattern="%-5p: %c - %m%n"/>
        </Console>

        <RollingFile name="RollingFile"
            fileName="./logFiles/info.log"
                     filePattern="./logFiles/info.log.%d{yyyy-MM-dd}">
            <PatternLayout>
                <pattern>%d %p %C{1.} [%t] %m%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
        </RollingFile>
    </Appenders>

    
    <Loggers>
        <Root level="info">
            <AppenderRef ref="RollingFile" level="info"/>
        </Root>
    </Loggers>

</Configuration>

참고 블로그

https://m.blog.naver.com/jang_delay/222131244354

 

Window에 SpringBoot Service 등록

스프링부트로 개발을 완료 후, 외장 톰캣에 배포하려 했더니 스프링부트를 스프링 구조로 바꿔줘야 하는 단...

blog.naver.com

 

 

https://github.com/winsw/winsw

 

GitHub - winsw/winsw: A wrapper executable that can run any executable as a Windows service, in a permissive license.

A wrapper executable that can run any executable as a Windows service, in a permissive license. - winsw/winsw

github.com