简体   繁体   English

从Java Swing GUI调用本机C代码

[英]Native C code calling from Java Swing GUI

I am trying to read and write to the parallel port, I implemented the writing and reading in C, now I want to import that code into a java GUI application. 我试图读写并行端口,我用C语言实现了读写,现在我想将该代码导入到Java GUI应用程序中。 I managed to include the C .so file into the java project and when calling the functions directly in the Java solutions main() method they work just fine. 我设法将C .so文件包含到java项目中,当直接在Java解决方案main()方法中调用函数时,它们可以正常工作。

I tried to call the native functions when a button is pressed, but it won't work, the application crashes. 我试图在按下按钮时调用本机函数,但是它不起作用,应用程序崩溃。 I am running the application as root, root priviledge is needed to change and read the parallel ports values. 我以root用户身份运行应用程序,需要root特权来更改和读取并行端口值。

How I am trying to call the native function: 我如何尝试调用本机函数:

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
       try
       {
           int portNR=Integer.parseInt(jTextField1.getText());
           int value=Integer.parseInt(jTextField2.getText());
        ParalellComanderApp.setPort(portNR,value );
       }
       catch (Exception e)
       {
           System.err.println(e.getMessage());
       }
    }

The Native function in C: C语言中的本机函数:

JNIEXPORT void JNICALL Java_paralellcomander_ParalellComanderApp_setPort
  (JNIEnv *env, jobject obj, jint port, jint value)
{
     outb(value,MAIN_PORT+port); 
     printf("Setting port %d to value %d\n",port,value);
}

Crash message: 崩溃消息:

    A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f00adaf9833, pid=6516, tid=139640785835776
#
# JRE version: 6.0_23-b23
# Java VM: OpenJDK 64-Bit Server VM (20.0-b11 mixed mode linux-amd64 compressed oops)
# Derivative: IcedTea6 1.11pre
# Distribution: Ubuntu oneiric (development branch), package 6b23~pre10-0ubuntu5
# Problematic frame:
# C  [libAccessParalel.so+0x833]  inb+0x17
#
# An error report file with more information is saved as:
# /home/bari/NetBeansProjects/ParalellComander/dist/hs_err_pid6516.log
#
# If you would like to submit a bug report, please include
# instructions how to reproduce the bug and visit:
#   https://bugs.launchpad.net/ubuntu/+source/openjdk-6/
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.

Sorry for the long post. 抱歉,很长的帖子。 Can anybody help me? 有谁能够帮助我?

Rather than using JNI and running your GUI as root, I would make the C part into a stand alone program (that runs with the bare minimum privileges - ie it binds to the parallel port and then revokes all other privileges) which talks to the Java app over a network socket. 而不是使用JNI并以root用户身份运行GUI,我将使C部分成为一个独立的程序(以最低的最低特权运行-即,它绑定到并行端口,然后撤销所有其他特权),该程序与Java对话通过网络套接字的应用程序。 That way you have more control over what passes between the part that faces the end user and the part that has elevated privileges, and are less vulnerable to attack. 这样,您可以更好地控制在面对最终用户的部分与特权较高且不易受到攻击的部分之间传递的内容。 It would also be easier to debug and test, because you can test the network communication to the C program using telnet or netcat without even involving the GUI part. 调试和测试也将更加容易,因为您可以使用telnet或netcat测试与C程序的网络通信,而无需使用GUI部分。

I don't think it's necessary to create a totally stand-alone program, but you should create an interface class, that makes sure that the port is setup properly, and also serialize the jni-calls, rather than banging on the hardware directly from the gui. 我认为没有必要创建一个完全独立的程序,但是您应该创建一个接口类,以确保正确设置端口,并序列化jni调用,而不是直接从硬件上敲打硬件。桂。

Other than that, write a good JUnit test, and research some more about parallel port programming. 除此之外,编写一个良好的JUnit测试,并进一步研究并行端口编程。 (http://as6edriver.sourceforge.net/Parallel-Port-Programming-HOWTO/accessing.html) (http://as6edriver.sourceforge.net/Parallel-Port-Programming-HOWTO/accessing.html)

I don't suppose you have a core dump from the jvm? 我不认为您有来自jvm的核心转储? Have you read the logs that were generated? 您已阅读生成的日志吗?

Btw, I think JNA is slightly less of a pain to work with than JNI, although your problem won't be solved by changing to JNA... 顺便说一句,我认为与JNI相比,JNA的工作痛苦要小一些,尽管改用JNA并不能解决您的问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM