본문 바로가기
Programming/Java_Spring

Jar실행 시, java.lang.ClassNotFoundException발생.

by 곰네Zip 2022. 8. 1.

스프링부트로 프로젝트 만들고.. 이제 테스트해보았다. IntelliJ 내장으로는 잘 돌았다.

근데.. jar로 실행하니 오류난다.

뭐야 이거.. -_-... (뭐긴.. 내가 모르니까 삽질한거지)

어.. 우선 output을 ${projectRootBaseDir}/target 으로 뽑아내고, 거기서 실행해본거다.

근데 jar파일이 내꺼 하나만 딱 있더라.

Main에서 뭘 못찾은거다. 불러와야할 클래스들이 없는경우라고 한다. (이유는 못불러와서).

다른 패키지가 포함되어야한다면 같은 경로에 있어야하는것 같다. (C++, C#도 그랬으니까.. 원칙적으로는)

우선 패키지를 어떻게 같이 포함시켜줄까?

스택오버플로우에는 다 있더라.. (역시 스형..)

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.0.2</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.basedir}/../target</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.2</version>
            <configuration>
                <outputDirectory>../target</outputDirectory>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>../lib/</Class-Path>
                        <Main-Class>myPackage.MyClass.MyApplication</Main-Class>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

 위와 같이 빌드 시, plugin에서 설정하면 되더라. 잊지말아야지

반응형

댓글