Question

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. 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.

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.

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:

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?

Was it helpful?

Solution

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 - i.e. it binds to the parallel port and then revokes all other privileges) which talks to the Java app over a network socket. 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.

OTHER TIPS

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.

Other than that, write a good JUnit test, and research some more about parallel port programming. (http://as6edriver.sourceforge.net/Parallel-Port-Programming-HOWTO/accessing.html)

I don't suppose you have a core dump from the 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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top