Development Tip

import sun.misc.BASE64Encoder 결과 Eclipse에서 컴파일 오류 발생

yourdevel 2020. 11. 25. 21:15
반응형

import sun.misc.BASE64Encoder 결과 Eclipse에서 컴파일 오류 발생


이 두 수입품;

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

이 오류가 발생했습니다.

Access restriction: The type BASE64Decoder is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar

이 오류를 어떻게 해결할 수 있습니까?


이 오류는 Eclipse 구성으로 인해 발생합니다. 경고로 줄일 수 있습니다. 더 좋은 방법은 비공개 API의 일부가 아닌 Base64 인코더를 사용하는 것입니다. Apache Commons에는 하나 가 있거나 이미 Java 1.8을 사용 중이면 java.util.Base64.


Window-> Preferences-> Java-> Compiler-> Error / Warnings로 이동합니다. Deprecated and Restricted API를
선택 합니다 . 경고로 변경하십시오 .
변경 금지 하고 낙담 참조 및 경고로 변경합니다. (또는 필요에 따라.)


물론입니다. Sun base64 인코더 / 디코더를 사용하지 마십시오. Apache Codec 또는이 공용 도메인 구현을 포함하여 사용 가능한 다른 옵션이 많이 있습니다 .

그런 다음 sun. * 패키지를 사용하지 말아야하는 이유를 읽으십시오 .


Java 6은 javax.xml.bind.DatatypeConverter. 이 클래스는 동일한 디코딩 및 인코딩을 지원하는 두 가지 정적 메서드를 제공합니다.

parseBase64Binary() / printBase64Binary()

업데이트 : Java 8부터 이제 훨씬 더 나은 Base64 지원이 제공됩니다.

이것을 사용하면 같은 추가 라이브러리가 필요하지 않습니다 Apache Commons Codec.


그렇습니다. sun.misc.BASE64Decoder는 훨씬 느립니다. java.xml.bind.DatatypeConverter.parseBase64Binary ()보다 9 배 느리고 org.apache.commons.codec.binary.Base64.decodeBase64 ()보다 4 배 느립니다. Java 6 OSX의 작은 문자열.

아래는 제가 사용한 테스트 프로그램입니다. OSX에서 Java 1.6.0_43 사용 :

john:password = am9objpwYXNzd29yZA==
javax.xml took 373: john:password
apache took    612: john:password
sun took       2215: john:password

commons-codec 1.4를 사용하는 Btw. 1.7에서는 느려진 것 같습니다.

javax.xml took 377: john:password
apache took    1681: john:password
sun took       2197: john:password

Java 7 또는 기타 OS를 테스트하지 않았습니다.

import javax.xml.bind.DatatypeConverter;
import org.apache.commons.codec.binary.Base64;
import java.io.IOException;

public class TestBase64 {
    private static volatile String save = null;
    public static void main(String argv[]) {
        String teststr = "john:password";
        String b64 = DatatypeConverter.printBase64Binary(teststr.getBytes());
        System.out.println(teststr + " = " + b64);
        try {
            final int COUNT = 1000000;
            long start;
            start = System.currentTimeMillis();
            for (int i=0; i<COUNT; ++i) {
                save = new String(DatatypeConverter.parseBase64Binary(b64));
            }
            System.out.println("javax.xml took "+(System.currentTimeMillis()-start)+": "+save);
            start = System.currentTimeMillis();
            for (int i=0; i<COUNT; ++i) {
                save = new String(Base64.decodeBase64(b64));
            }
            System.out.println("apache took    "+(System.currentTimeMillis()-start)+": "+save);
            sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();
            start = System.currentTimeMillis();
            for (int i=0; i<COUNT; ++i) {
                save = new String(dec.decodeBuffer(b64));
            }
            System.out.println("sun took       "+(System.currentTimeMillis()-start)+": "+save);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

jdk1.6.0_37 에서이 문제가 발생했습니다. 이것은 내 시스템에서 유일한 JDE / JRE입니다. 이유는 모르겠지만 다음으로 문제가 해결되었습니다.

프로젝트-> 속성-> Java 빌드 경로-> 라이브러리

실행 환경 에서 Alernate JRE로 라디오 버튼을 전환합니다 . 이것은 동일한 jdk1.6.0_37을 선택하지만 정리 / 빌드 후 컴파일 오류가 사라졌습니다.

아마도 ram (3 월 16 일 9:00)의 답변에 대한 설명 이 그것과 관련이 있습니다.


  1. 프로젝트 속성에서 빌드 경로 설정으로 이동합니다.
  2. JRE 시스템 라이브러리 제거
  3. Add it back; Select "Add Library" and select the JRE System Library. The default worked for me.

This works because you have multiple classes in different jar files. Removing and re-adding the jre lib will make the right classes be first. If you want a fundamental solution make sure you exclude the jar files with the same classes.


This error (or warning in later versions) occurs because you are compiling against a Java Execution Environment. This shows up as JRE System library [CDC-1.0/Foundation-1.0] in the Build path of your Eclipse Java project. Such environments only expose the Java standard API instead of all the classes within the runtime. This means that the classes used to implement the Java standard API are not exposed.

You can allow access to these particular classes using access rules, you could configure Eclipse to use the JDK directly or you could disable the error. You would however be hiding a serious error as Sun internal classes shouldn't be used (see below for a short explanation).


Java contains a Base64 class in the standard API since Java 1.8. See below for an example how to use it:

Java 8 import statement:

import java.util.Base64;

Java 8 example code:

// create a byte array containing data (test)
byte[] binaryData = new byte[] { 0x64, 0x61, 0x74, 0x61 };
// create and configure encoder (using method chaining) 
Base64.Encoder base64Encoder = Base64.getEncoder().withoutPadding();
// encode to string (instead of a byte array containing ASCII)
String base64EncodedData = base64Encoder.encodeToString(binaryData);

// decode using a single statement (no reuse of decoder)
// NOTE the decoder won't fail because the padding is missing
byte[] base64DecodedData = Base64.getDecoder().decode(base64EncodedData);

If Java 8 is not available a library such as Apache Commons Codec or Guava should be used.


Sun internal classes shouldn't be used. Those classes are used to implement Java. They have got public methods to allow instantiation from other packages. A good build environment however should protect you from using them.

Using internal classes may break compatibility with future Java SE runtimes; the implementation and location of these classes can change at any time. It should be strongly discouraged to disable the error or warning.


I am using unix system.

In eclipse project-> Properties -> Java Compiler -> Errors/Warning -> Forbidden Access(access rule) -> Turn it to warning/Ignore(Previously it was set to Error).


I know this is very Old post. Since we don't have any thing sun.misc in maven we can easily use

StringUtils.newStringUtf8(Base64.encodeBase64(encVal)); From org.apache.commons.codec.binary.Base64


This error is because of you are importing below two classes import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder;. Maybe you are using encode and decode of that library like below.

new BASE64Encoder().encode(encVal);
newBASE64Decoder().decodeBuffer(encryptedData);

Yeah instead of sun.misc.BASE64Encoder you can import java.util.Base64 class.Now change the previous encode method as below:

encryptedData=Base64.getEncoder().encodeToString(encryptedByteArray);

Now change the previous decode method as below

byte[] base64DecodedData = Base64.getDecoder().decode(base64EncodedData);

Now everything is done , you can save your program and run. It will run without showing any error.


Add base64decoder jar and try these imports:

import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;

참고URL : https://stackoverflow.com/questions/5549464/import-sun-misc-base64encoder-results-in-error-compiled-in-eclipse

반응형