Re: Problem using JNI with swing

Discussion in 'Java' started by arundeep, Apr 11, 2004.

  1. arundeep

    arundeep Guest

    i am getting this error when i press a key in java code :
    # The exception above was detected in native code outside the VM
    #
    # Java VM: Java HotSpot(TM) Client VM (1.4.2-b28 mixed mode)
    #
    # An error report file has been saved as hs_err_pid22771.log.
    # Please refer to the file for further information.
    #
    Aborted



    this is the code that i had written to test the native code:

    public class ParallelTest1 extends JFrame implements KeyListener{

    private static final int[] stepData={1,2,4,8};
    private int position=0;
    private ParallelTest test=null;

    // main program
    public static void main(String[] args) {
    //int x=5;
    // int temp;
    ParallelTest1 test1=new ParallelTest1();
    test1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    test1.show();
    }//main ends

    public ParallelTest1(){
    addKeyListener(this);
    int temp;
    //System.loadLibrary("CJParallelInterface");
    test=new ParallelTest();
    temp=test.setPermissions(ParallelPortUtil.LPTDataPort,3,1);
    test.setModeParallel(ParallelPortUtil.WRITE_MODE,ParallelPortUtil.LPTControlPort);
    System.out.println("value returned by ioperm function = " +
    Integer.toString(temp));

    if(temp!=0){
    System.out.println("Error : No permision granted for ports");
    System.exit(0);
    }
    try{
    test.sendParallel(0,ParallelPortUtil.LPTDataPort);
    Thread.sleep(300);
    test.sendParallel(1,ParallelPortUtil.LPTDataPort);
    Thread.sleep(300);
    test.sendParallel(2,ParallelPortUtil.LPTDataPort);
    }
    catch(InterruptedException e){
    System.out.println("Error");
    }
    //super.show();
    }
    public void keyPressed(KeyEvent event){
    int keyCode=event.getKeyCode();
    if(keyCode==KeyEvent.VK_LEFT){
    test.sendParallel(stepData[position--],ParallelPortUtil.LPTDataPort);
    position--;
    position=(position+4)%4;
    System.out.println(" Key Pressed is left and outValue = "
    +stepData[position] );
    }
    if(keyCode==KeyEvent.VK_RIGHT){
    test.sendParallel(stepData[position++],ParallelPortUtil.LPTDataPort);
    position++;
    position=position%4;
    System.out.println("Key Pressed is right and outValue = "+
    stepData[position]);
    }
    if(keyCode==KeyEvent.VK_END){
    System.exit(0);
    }
    }



    this is the code for ParallelTestImp.c :


    JNIEXPORT jint JNICALL Java_ParallelTest_cSetPermissions
    (JNIEnv *env, jobject obj, jint from, jint num, jint turnon){
    int y;
    y=ioperm(from,num,turnon);
    return (jint)y;
    }


    JNIEXPORT void JNICALL Java_ParallelTest_cSendParallel
    (JNIEnv *env, jobject obj, jint outValue, jint port){

    outb(outValue,port);
    return;
    }




    JNIEXPORT void JNICALL Java_ParallelTest_cSetModeParallel
    (JNIEnv *env, jobject obj, jint mode, jint port){

    outb(mode,port);
    return;
    }


    JNIEXPORT jint JNICALL Java_ParallelTest_cReadParallel
    (JNIEnv *env, jobject obj, jint port){
    int y;
    y=inb(port);
    return (jint)y;
    }
     
    arundeep, Apr 11, 2004
    #1
    1. Advertising

  2. arundeep

    Alex Hunsley Guest

    arundeep wrote:
    > i am getting this error when i press a key in java code :
    > # The exception above was detected in native code outside the VM
    > #
    > # Java VM: Java HotSpot(TM) Client VM (1.4.2-b28 mixed mode)
    > #
    > # An error report file has been saved as hs_err_pid22771.log.
    > # Please refer to the file for further information.
    > #
    > Aborted
    >
    >
    >
    > this is the code that i had written to test the native code:

    [snip]

    As the error suggests, the problem, or 'symptom' if you like, is
    occurring in your native code, and it happens when you're calling from
    AWT. This sounds like an AWT threading problem. Are you familiar with
    the concept of the event despatch thread (AKA gui thread)? listener
    methods that hear about AWT/swing events tend to get called on this gui
    thread. The may want to try having your native calling code spawn
    another thread that calls the native code.


    A general idea for when debugging JNI: A helpful step would be to work
    out what values java in passing into the native code, and whereabout in
    the native exactly things are going wrong.
    So, for example, as a quick debug hack (if you don't have a debugger,
    e.g. eclipse!), whereever you call native code from java, before the
    native code call, print out the parameters you're passing:

    System.out.println("debugprint#1: About to call sendToParallel "
    + "with params: "
    + stepData[position++]
    + ", "
    + ParallelPortUtil.LPTDataPort);

    [[digression:
    Depending on how badly the VM is freaking out because of the badness
    happening in the native code, you may not actually see this println
    outputting anything unless you temporarily comment out the actual call
    the native code!
    Btw, try to avoid doing method calls that also increment/decrement
    variables in the same line, i.e. instead of:

    test.sendParallel(stepData[position--],ParallelPortUtil.LPTDataPort);

    write:

    test.sendParallel(stepData[position],ParallelPortUtil.LPTDataPort);
    position--;

    This way, things are much clearer, and it is more obvious where you have
    changed that variable. And you won't temporarily comment out the
    test.sendParallelLine (for reasons I've suggested above) and maybe
    forget that then position varaible won't be then decremented, and your
    printlns from java will contains misleading values that don't change!
    ]]


    HTH,
    alex
     
    Alex Hunsley, Apr 11, 2004
    #2
    1. Advertising

  3. arundeep

    arundeep Guest

    i am known to the event dispatch thread but not to very
    detail. if u could just provide the the coe to achieve this thing or just
    modified the code appropriately, that would be of gr8 help to solve it and
    also to understand the problem.
    regarding the variable like position and stepData[], i have checked and
    found no problem with it.

    thanks.
     
    arundeep, Apr 12, 2004
    #3
  4. arundeep

    arundeep Guest

    i am known to the event dispatch thread but not to very
    detail. if u could just provide the the coe to achieve this thing or just
    modified the code appropriately, that would be of gr8 help to solve it and
    also to understand the problem.
    regarding the variable like position and stepData[], i have checked and
    found no problem with it.

    thanks.
     
    arundeep, Apr 12, 2004
    #4
  5. arundeep

    Alex Hunsley Guest

    arundeep wrote:
    > i am known to the event dispatch thread but not to very
    > detail. if u could just provide the the coe to achieve this thing or just
    > modified the code appropriately, that would be of gr8 help to solve it and
    > also to understand the problem.
    > regarding the variable like position and stepData[], i have checked and
    > found no problem with it.
    >
    > thanks.
    >


    You should have a look at java's thread tutorial, it's very easy:

    http://java.sun.com/docs/books/tutorial/essential/threads/simple.html

    i.e. just make a class that subclasses Thread as in the example above, with the
    run method calling your native parallel port methods. (It would get the
    parameters for the call when it was constructed, in the simlpest example.)
    Then, whenever your code (the code you posted) wants to do the parallel port
    sending, it just makes a new instance of the class we created above (with the
    right parameters) - suppose we called the class ParallelPortDataSender - and
    then calls start():

    ParallelPortDataSender dataSender = new ParallelPortDataSender(5, 10);
    dataSender.start(); // starts the thread off which makes the pport send


    alex
     
    Alex Hunsley, Apr 13, 2004
    #5
  6. arundeep

    arundeep Guest

    well i modified the code as u said and is given below but i get the similar
    error

    public void keyPressed(KeyEvent event){
    int keyCode=event.getKeyCode();
    if(keyCode==KeyEvent.VK_LEFT){
    //test.sendParallel(stepData[position--],ParallelPortUtil.LPTDataPort);
    Thread t= new
    ParallelRunThread(test,stepData[position--],ParallelPortUtil.LPTDataPort);
    t.start();
    //position--;
    position=(position+4)%4;
    System.out.println(" Key Pressed is left and outValue = "
    +stepData[position] );
    }



    the class that i had written is

    class ParallelRunThread extends Thread{
    public ParallelRunThread( ParallelTest t, int value, int port){
    parallel=t;
    val=value;
    p=port;
    }
    public void run(){
    parallel.sendParallel(val,p);
    }

    private ParallelTest parallel;
    private int val;
    private int p;
    }



    tell me if there's any problem class or what. the error i am getting is
    same as earlier

    An unexpected exception has been detected in native code outside the VM.
    Unexpected Signal : 11 occurred at PC=0x4CFCC4C5
    Function=(null)+0x4CFCC4C5
    Library=/usr/java/j2sdk1.4.2/jre/lib/i386/libCJParallelInterface.so

    NOTE: We are unable to locate the function name symbol for the error
    just occurred. Please refer to release documentation for possible
    reason and solutions.


    Current Java thread:
    at ParallelTest.cSendParallel(Native Method)
    at ParallelTest.sendParallel(ParallelTest.java:69)
    at ParallelRunThread.run(ParallelTest1.java:94)


    Please let me know what this means and how it could be solved. if needed
    then i could send all the files . its not big.
     
    arundeep, Apr 16, 2004
    #6
  7. On Fri, 16 Apr 2004 01:51:07 -0400, arundeep wrote:
    > tell me if there's any problem class or what. the error i am getting is
    > same as earlier
    >
    > An unexpected exception has been detected in native code outside the VM.
    > Unexpected Signal : 11 occurred at PC=0x4CFCC4C5
    > Function=(null)+0x4CFCC4C5
    > Library=/usr/java/j2sdk1.4.2/jre/lib/i386/libCJParallelInterface.so


    - is your application running as root?
    - does ioperm() report success?
    - are you calling ioperm() from the *same* thread that calls outb() etc?

    The man page for ioperm says "not inherited across fork" which on
    linux *may* also include threads, although that is not specifically
    mentioned and really I have no idea.

    I suggest that you first rule out threading issues by (temporarily)
    calling ioperm() in your native methods cSendParallel() (etc)
    immediately prior to calling outb() or inb().

    In an earlier post in this thread you asked:
    > or if ther's any way to capture key events in simple main program of
    > java.


    For a simple mechanism that lets you receive user input as each
    character is typed, see this:
    http://groups.google.com/groups?selm=bde5sk$o7f$

    It just requires that you use stty to initialize the input stream
    correctly, after that you can use System.in.read() in the usual
    manner.

    Or, since you're already looking at native solutions, why reinvent the
    wheel:

    http://java-readline.sourceforge.net/
    http://www.pitman.co.za/projects/charva/index.html
    http://www.bmsi.com/tuipeer/

    (my link to JNICurses seems broken:
    http://www.connix.com/~psantoro/jnicurses/jnicurses.html)

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Apr 16, 2004
    #7
  8. arundeep

    arundeep Guest

    thanks a lot Mr. Gordon. i bring the ioperm() and outb() function in the
    same native function cSendParallel an it worked. Thanks a lot.

    But i have one question, i don't know myself, but this concept of calling
    ioperm and before every callto outb or inb or like
    , doen't seems logical. the application is same so there must be some way
    to provde global address to the program. with the fork this thing seems
    digestable to me, as when we fork a new instance of the complete process
    is generated but withthread this is not the case. i guess either ioperm
    should be modified or sth else need to be done. may i have to find other
    methods that will not have such problem.

    Anyway thanks a lot Mr. Gordon. u really helped. as this problem i am
    unable to solve from last 2 months. thanks a lot.

    Arundeep Singh
     
    arundeep, Apr 19, 2004
    #8
  9. On Mon, 19 Apr 2004 02:02:18 -0400, arundeep wrote:
    > thanks a lot Mr. Gordon. i bring the ioperm() and outb() function in
    > the same native function cSendParallel an it worked. Thanks a lot.
    >
    > But i have one question, i don't know myself, but this concept of
    > calling ioperm and before every callto outb or inb or like, doen't
    > seems logical.


    No, it doesn't. But on Linux, threads and processes are closely
    related concepts.

    So it seems that your problem is that you were calling ioperm() from a
    different thread than outb() and inb(). The solution is to call them
    from the *same* thread.

    But obviously you don't want to call ioperm() "just in case" every
    time you call inb() or outb(). A workaround is to check in
    keyPressed(), something like this:


    private boolean setPermissionDone = false;

    public void keyPressed(KeyEvent event) {
    if (! setPermissionDone) {
    if (setPermission(...) == 0) {
    setPermissionDone = true;
    }
    }

    keyCode = event.getKeyCode();
    sendParallel(...);
    }

    I don't know whether keyPressed() will always be called from the same
    thread or not. Someone who knows Swing should be able to answer that.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Apr 19, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Big Daddy

    Java 1.2 Swing vs. Java 1.5 Swing

    Big Daddy, Apr 15, 2005, in forum: Java
    Replies:
    2
    Views:
    514
    Pete Barrett
    Apr 16, 2005
  2. mkrause
    Replies:
    0
    Views:
    692
    mkrause
    May 6, 2005
  3. lizard
    Replies:
    0
    Views:
    1,797
    lizard
    Jan 30, 2006
  4. S.T
    Replies:
    2
    Views:
    601
  5. Knute Johnson

    Swing is dead! Long live Swing.

    Knute Johnson, Feb 16, 2012, in forum: Java
    Replies:
    32
    Views:
    3,867
    Daniel Pitts
    Feb 29, 2012
Loading...

Share This Page