Avoid `System.loadLibrary` for improved Java portability.

This product is not supported for your selected Datadog site. ().
이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.

Metadata

ID: java-code-style/avoid-using-native-code

Language: Java

Severity: Notice

Category: Code Style

Description

When Java applications use native code, typically through the Java Native Interface (JNI) and methods like System.loadLibrary() or System.load(), it introduces a strong dependency on platform-specific binaries. This practice significantly reduces the portability of the application, as the native libraries must be compiled and distributed for each target operating system and architecture. It also increases deployment complexity, makes debugging more challenging, and can lead to instability or crashes if native code interactions are not meticulously managed.

How to Remediate

To enhance portability and maintainability, favor pure Java solutions over native code whenever possible. If interacting with system-specific features or achieving critical performance gains necessitates native calls, consider abstracting them behind an interface to minimize their direct impact on the codebase. Prioritize using well-established, cross-platform libraries or external services that handle native interactions internally, rather than directly managing System.loadLibrary() calls within your application logic.

Non-Compliant Code Examples

public class Foo {
    public Foo() {
        System.loadLibrary("nativelib");
    }

    static {
        System.loadLibrary("nativelib");
    }

    public void foo() throws SecurityException, NoSuchMethodException {
        System.loadLibrary("nativelib");
    }
}

Compliant Code Examples

public class Bar {
    public void baz() {
        System.out.println("Executing pure Java code without native dependencies.");
        // No System.loadLibrary or System.load calls here
    }
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

원활한 통합. Datadog Code Security를 경험해 보세요