cannot be accessed from outside package 에러 해결 방법

etc|2020. 1. 14. 06:00

cannot be accessed from outside package 에러 해결 방법

프로그래밍 언어 중 우리나라에서 가장 많이 사용을 하는 언어는 자바입니다. 자바 커피가 있을 만큼 인기가 많은 언어로 스프링, 스프링부트로 웹개발을 할 수도 있습니다. 스프링부트로 웹개발을 할 때에 발생을 할 수 있는 에러가 있습니다. cannot be accessed from outside package 에러 해결 방법에 대해 알아보도록 해요.


cannot be accessed from outside package 에러는 다른 클래스를 사용을 할 때에 발생을 하는 에러입니다. 이러한 에러가 났을 때 다시 프로그래밍을 하는 것 보다는 직접 원인을 알아보고 수정을 하는 것이 실력을 쌓을 수 있는 방법입니다.


cannot be accessed from outside package 에러 해결 방법


cannot be accessed from outside package 에러 원인과 해결방법

Testing started at 오전 1:15 ...

> Task :cleanTest UP-TO-DATE

> Task :compileJava

> Task :processResources UP-TO-DATE

> Task :classes

> Task :compileTestJava FAILED

E:\study\src\test\java\com\example\study\repository\UserRepositoryTest.java:3: error: StudyApplicationTests is not public in com.example.study; cannot be accessed from outside package

import com.example.study.StudyApplicationTests;

                        ^

E:\study\src\test\java\com\example\study\repository\UserRepositoryTest.java:10: error: StudyApplicationTests is not public in com.example.study; cannot be accessed from outside package

public class UserRepositoryTest extends StudyApplicationTests {

                                        ^

2 errors

FAILURE: Build failed with an exception.

* What went wrong:

Execution failed for task ':compileTestJava'.

> Compilation failed; see the compiler error output for details.

* Try:

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.

Use '--warning-mode all' to show the individual deprecation warnings.

See https://docs.gradle.org/6.0.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 6s

4 actionable tasks: 2 executed, 2 up-to-date


콘솔에 해당 에러와 같이 cannot be accessed from outside package 에러는 다른 패키지에 있는 class를 참조를 하는 경우에 발생을 하는 에러입니다. 해당 class에 public로 정의가 되어 있으면 해당 에러는 나타나지는 않는데 만일 정의가 되어 있지 않다면 에러가 발생을 합니다. 해결을 할 수 있는 방법은 간단합니다. 사용을 하고자 하는 class에 public으로 지정을 하면 됩니다.


package com.example.study;


import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)

@SpringBootTest

public class StudyApplicationTests {


@Test

void contextLoads() {

}


}


StudyApplicationTests class를 사용을 하고자 한다면 앞에 public를 붙여야 cannot be accessed from outside package에러를 해결을 할 수 있습니다. cannot be accessed from outside package 에러 해결 방법에 대해 알아보았습니다. 스트링부트 에러를 알아보신다면 유용한 정보가 되시길 바래요~!

댓글()