Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用JNI启动Jar #1

Open
ElisaMin opened this issue Apr 28, 2023 · 0 comments
Open

用JNI启动Jar #1

ElisaMin opened this issue Apr 28, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@ElisaMin
Copy link
Owner

use std::ffi::CString;
use std::os::raw::{c_char, c_int};

fn main() {
    // Get the number of command line arguments.
    let argc = unsafe {
        let mut argc = 0;
        std::getenv("argc")
            .map(|s| s.parse().unwrap())
            .unwrap_or(argc);
        argc
    };

    // Get the command line arguments.
    let argv = unsafe {
        let mut argv = Vec::new();
        for i in 0..argc {
            let arg = std::getenv(format!("argv{}", i)).unwrap_or("");
            argv.push(CString::new(arg).unwrap());
        }
        argv
    };

    // Initialize the JVM.
    let status = unsafe {
        let status = jli_init(argc, argv.as_ptr());
        if status != 0 {
            println!("Error initializing the JVM: {}", status);
            std::process::exit(1);
        }
        status
    };

    // Launch the Java program.
    let status = unsafe {
        let status = jli_launch(argc, argv.as_ptr());
        if status != 0 {
            println!("Error launching the Java program: {}", status);
            std::process::exit(1);
        }
        status
    };

    // Terminate the JVM.
    unsafe {
        jli_term();
    }
}

extern "C" {
    fn jli_init(argc: c_int, argv: *const *const c_char) -> c_int;
    fn jli_launch(argc: c_int, argv: *const *const c_char) -> c_int;
    fn jli_term();
}
@ElisaMin ElisaMin added the enhancement New feature or request label Apr 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant