Click here to Skip to main content
15,914,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to call a Jni funktion from a java class in Netbeans, but the Jni funktion is only performed, when I close the Swing Gui.

How can the
Hello World from C
displayed at ones when running the application?


Any solutions?

What I have tried:

* @author peter
 */
public class Linux_Kamera_AWT extends java.awt.Frame {

    /**
     * Creates new form Linux_Kamera_AWT
     */
    public Linux_Kamera_AWT() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        Start_JNI = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        jButton1.setText("jButton1");

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        Start_JNI.setText("jButton2");
        Start_JNI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Start_JNIActionPerformed(evt);
            }
        });
        add(Start_JNI, java.awt.BorderLayout.NORTH);

        jButton3.setText("jButton3");
        add(jButton3, java.awt.BorderLayout.WEST);

        pack();
    }// </editor-fold>                        

    /**
     * Exit the Application
     */
    private void exitForm(java.awt.event.WindowEvent evt) {                          
        System.exit(0);
    }                         

    private void Start_JNIActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        
        callNativeFunction();
        JNIServer.nativePrint();
    }                                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        //JNIServer.nativePrint();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Linux_Kamera_AWT().setVisible(true);
            }
        });
    }
    
    
    private static void callNativeFunction() {
        Thread nativeFunctionThread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Test.run - before calling hello.");
                new JNIServer().nativePrint();  // where hello is native method
                System.out.println("Test.run - after calling hello.");
            }
        });

        nativeFunctionThread.start();
    }


    // Variables declaration - do not modify                     
    private javax.swing.JButton Start_JNI;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    // End of variables declaration                   
}


C-Source File:

// * To change this license header, choose License Headers in Project Properties.
#include <jni.h>
#include <stdio.h>
#include "Linux_Kamera.h"

JNIEXPORT void JNICALL Java_humer_peter_JNIServer_nativePrint
        (JNIEnv *env, jobject obj)
{

    printf("\nHello World from C\n");

}


public class JNIServer {
    public native void hello();

    static {  
        System.out.println("Inload library");
        System.loadLibrary("JNI_Lib");
    }  
}



C-Headerfile:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class humer_peter_JNIServer */

#ifndef _Included_humer_peter_JNIServer
#define _Included_humer_peter_JNIServer
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     humer_peter_JNIServer
 * Method:    nativePrint
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_humer_peter_JNIServer_nativePrint
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif
Posted
Updated 27-Oct-18 6:05am
v2

1 solution

You are calling printf from a window based application. But printf uses the console which does not exist in AWT.
 
Share this answer
 
Comments
Peter____ 27-Oct-18 15:25pm    
Hello Richard,

your answer solved my problem.
The native method printf could also be displayed by calling the flush method in C.


https://stackoverflow.com/questions/23085044/jni-system-out-and-printf-behaviour

Thanks,

Peter

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900