Java and Linux kernel programming

JianJunShen
JianJunShen used Ask the Experts™
on
Hi: I have a Linux(red hat)  kernel module, now I want to use Java to call this module. do you know how to do it? Shanll I use JNI?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
You would need to write another shared library to call it. Alternatively, a better way is to use JNA

Author

Commented:
what is JNA? Any more explanation?
Top Expert 2016
Commented:
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Author

Commented:
Thanks first, how to write Shared library then? Are there any example?
Top Expert 2016
Commented:
You don't need to write one with JNA. Just load and call the one you're interested in
Commented:
IMHO ... you cant make a call to an exported symbol (symbol exported in the module or kernel)  from the user space program (using Java).

However, if you are looking at loading / unloading a kernel module using java - yes then this is possible. You could try something like this, but make sure your java program runs as root.

String loader (String s, int i) throws BadLoaderIntException{
        if(i == 0){
                s = "insmod " + s;
        }else if(i == 1){
                s = "rmmod " + s;
        }else{
                throw new BadLoaderIntException();
        }

        ProcessBuilder pb = new ProcessBuilder("bash", "-c", s);
        pb.redirectErrorStream(true); //Outputs to stderr in-case of Error
        Process shell = null;
        try {
                shell = pb.start();
        } catch (IOException e) {
                e.printStackTrace();
        }
        InputStream shellIn = shell.getInputStream();
        ...
Mick BarryJava Developer
Top Expert 2010
Commented:
yes JNI would be the go. Basically what you need is to just write some native code to interface between java and module you want to call

have a read through here
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html

and give me a yell if you have questions
Top Expert 2016

Commented:
:-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial