Jni调用不同时使用swing API [英] Jni call not simultaneously working with swing API
本文介绍了Jni调用不同时使用swing API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我想从Netbeans的java类中调用Jni funktion,但只有当我关闭Swing Gui时才会执行Jni funktion。
运行应用程序时,
Hello World from C
如何显示?
任何解决方案?
我尝试过: < br $>
* @author peter
* /
公共类Linux_Kamera_AWT扩展java.awt.Frame {
/ **
*创建新表单Linux_Kamera_AWT
* /
public Linux_Kamera_AWT(){
initComponents();
}
/ **
*从构造函数中调用此方法以初始化表单。
*警告:请勿修改此代码。此方法的内容始终由表单编辑器重新生成
*。
* /
//< editor-fold defaultstate =collapseddesc =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>
/ **
*退出应用程序
* /
private void exitForm(java.awt.event.WindowEvent evt){
System.exit (0);
}
private void Start_JNIActionPerformed(java.awt.event.ActionEvent evt){
// TODO在这里添加你的处理代码:
callNativeFunction( );
JNIServer.nativePrint();
}
/ **
* @param args命令行参数
* /
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 - 在调用hello之前。);
new JNIServer()。nativePrint(); //其中hello是本机方法
System.out.println(Test.run - 在调用hello之后。);
}
});
nativeFunctionThread.start();
}
//变量声明 - 不要修改
private javax.swing.JButton Start_JNI;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;
//变量结束声明
}
C源文件:
// *要更改此许可证标题,请在项目属性中选择许可证标题。
#include< jni.h>
#include< stdio.h>
#includeLinux_Kamera.h
JNIEXPORT void JNICALL Java_humer_peter_JNIServer_nativePrint
(JNIEnv * env,jobject obj)
{
printf( \ nHello World from C\ n);
}
公共类JNIServer {
public native void hello ();
static {
System.out.println(Inload library);
System.loadLibrary(JNI_Lib);
}
}
C-Headerfile:
/ *不要编辑这个文件 - 它是机器生成的* /
#include< jni.h>
/ *班级humer_peter_JNIServer * /
#ifndef _Included_humer_peter_JNIServer
#define _Included_humer_peter_JNIServer
#ifdef __cplusplus
externC{
#endif
/ *
*类:humer_peter_JNIServer
*方法:nativePrint
*签名:()V
* /
JNIEXPORT void JNICALL Java_humer_peter_JNIServer_nativePrint
(JNIEnv *,jobject);
#ifdef __cplusplus
}
#endif
#endif
解决方案
< blockquote>您正在从基于窗口的应用程序调用printf。但printf使用AWT中不存在的控制台。
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
解决方案
You are calling printf from a window based application. But printf uses the console which does not exist in AWT.
这篇关于Jni调用不同时使用swing API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文