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 をお試しください