JNI Exception

HSylvio Messages postés 116 Date d'inscription jeudi 22 juillet 2004 Statut Membre Dernière intervention 14 juin 2012 - 30 juin 2006 à 10:51
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 - 20 juil. 2006 à 17:31
Bonjour,

J'utilise une interface native pour lancer un algorithme en C++ depuis java :

       public native String construitZero(double []a);

Le probleme c'est une erreur, qui n'apparaît pas forcémant au même moment d'une execution à l'autre!!!

En effet je pensais avoir une grosse erreur dans mon code CPP mais  ce message :

# An unexpected error has been detected by HotSpot Virtual Machine:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x7c94426d, pid=1800, tid=2524
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_06-b05 mixed mode)
# Problematic frame:
# C  [ntdll.dll+0x3426d]
#
# An error report file with more information is saved as hs_err_pid1800.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#

Current thread (0x262ecc20):  JavaThread "AWT-EventQueue-0" [_thread_in_native, id=2524]
siginfo: ExceptionCode=0xc0000005, reading address 0x00000470

---------------  P R O C E S S  ---------------

Java Threads: ( => current thread )
  0x262f3d90 JavaThread "TimerQueue" daemon [_thread_blocked, id=172]
  0x00035b88 JavaThread "DestroyJavaVM" [_thread_blocked, id=2972]
>0x262ecc20 JavaThread "AWT-EventQueue-0" [_thread_in_native, id2524]
  0x262ad9c0 JavaThread "AWT-Windows" daemon [_thread_in_native, id=2492]

Parfois le process ciblé est JavaThread "AWT-Windows" daemon [_thread_in_native, id=2492]

Est-ce une erreur dans le code java? CPP? configurations VM? manque de resources ?

Et pourquoi ca plante a des endroits différents d'un lancer à l'autre???
Quelqu'un peut-il m'aider svp???

7 réponses

sheorogath Messages postés 2448 Date d'inscription samedi 21 février 2004 Statut Modérateur Dernière intervention 29 janvier 2010 17
30 juin 2006 à 11:02
ben montre nous le code cpp et le code java , enfin les appels

"n'est pas mort ce qui semble a jamais dormir et en d'etrange temps meme la mort peut mourrir"
0
HSylvio Messages postés 116 Date d'inscription jeudi 22 juillet 2004 Statut Membre Dernière intervention 14 juin 2012
30 juin 2006 à 11:36
OK :
Classe testJNI1  :
static {
       System.loadLibrary("Project1");
       System.out.println("Bib lue\n");
    }
public native String construitZero(double []a);

ActionListener :
if(null!=paramC){
        System.out.println("LANCEMENT DE L'ALGO ");
        System.out.println(construitZero(paramC));
    }else
        System.out.println("On peut pas lancer l'algo : tableau de parametres vide!");
    }

Classe DLLMain.cpp :    (raccourci....)

JNIEXPORT jstring JNICALL
Java_algoCPP_TestJNI1_construitZero(JNIEnv *env, jobject obj, jdoubleArray argsFromJava){
                                      
/*              Récupération des informations JAVA :          */
  cout << "On a recu ";
  int arrayLength = env->GetArrayLength(argsFromJava);
  cout << arrayLength << " elements : " << endl; 
  t = env->GetDoubleArrayElements(argsFromJava, 0);

[ ... on rempli des tableaux avec ce qu'il y a dans t ]

  rec reC;   // une classe CPP
  int monA = 82;
  bool creation = reC.init(monA);

  cerr<<"initialisé"<<endl;

  reC.setProp(tabLength, tab10, tab11, "arg1");
  reC.setProp(tabLength, tab20, tab21, "arg2");
  cerr<<"Fns mémorisées"<<endl;
  reC.setPM(d);      // ca plante ici desfois (=> void rec::setPM(double p){  PM = p; }
  cout<<"D mis à "<<d<<endl;
 
  reC.fitD();      // desfois ca plante pdt cette fn
  
  int randz = rand()%monA;
  for(int i=0;i<monA;i++){
          for(int j=0;j<monA;j++){
                      // on affiche un resultat de reC
          }
          cout<<endl;
  }                 
  reC.calculResultat();        // d'autres fois ca plante ici
  cin>>new char;
  return env->NewStringUTF(" Allez, au revoir");        // et ca a meme deja plante là, juste apres dire au revoir...
}

Voila donc un peu du code abrege; j'ai 'pas le droit' de le donner vraiment...
si vous en voulez plus je modifierai un peu d'autres classes pour vs en donner l'aperçu...

   Sinon j'ai esaye de faire de testJNI1 un thread, une classe statique, un singleton et c'est toujours pareil, ca plante à des endroits differents chaque lancer... c'est pour ca ke je vous met pas d'output...
0
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
30 juin 2006 à 12:07
Salut,


avec le peut d'info que tu donne il est pas possible de te depanner....


c'est quoi "t", "d"

que fait reC.init(monA), reC.setProp(tabLength, tab10, tab11, "arg1"),
reC.setProp(tabLength, tab20, tab21, "arg2"), reC.fitD(),
reC.calculResultat()


return env->NewStringUTF(" Allez, au revoir")// pa beau


creer c'est fonction et appel la dans ton code apres chaque
manipulation jni comme ca tu aura un peut plus d'info (mais pour moi tu
nous fait une jolie erreur de segmentation)


void printStackTrace(JNIEnv *e){

    if(e->ExceptionOccurred()){

        e->ExceptionDescribe();

        e->ExceptionClear();

    }

}


a ui meme si il est gros colle le contenu du rapport de la jvm soit le fichier hs_err_pid1800.log

------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

WORA
0
HSylvio Messages postés 116 Date d'inscription jeudi 22 juillet 2004 Statut Membre Dernière intervention 14 juin 2012
30 juin 2006 à 14:31
Bon ok j'vais vous balancer du code, mais c'est pas beau a voir...

... j'aimerai bien que ce soit une erreur de segmentation mais pkoi n'apparaitrait-t-elle pas toujours dans la meme fonction???

Pour comencer, voila le dernier .log :

#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x7c94426d, pid=1500, tid=204
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_06-b05 mixed mode)
# Problematic frame:
# C  [ntdll.dll+0x3426d]
#

---------------  T H R E A D  ---------------

Current thread (0x262ecc20):  JavaThread "AWT-EventQueue-0" [_thread_in_native, id=204]

siginfo: ExceptionCode=0xc0000005, reading address 0x00000000

Registers:
EAX=0x00000000, EBX=0x00000316, ECX=0x00031d48, EDX=0x00000000
ESP=0x265ad534, EBP=0x265ad754, ESI=0x00030340, EDI=0x00030490
EIP=0x7c94426d, EFLAGS=0x00010246

Top of Stack: (sp=0x265ad534)
0x265ad534:   262ecc20 00000148 00000148 7c9206eb
0x265ad544:   262ecc20 00000052 00000052 ffffffff
0x265ad554:   77c003ad 77c116d2 00000001 265ad611
0x265ad564:   00000007 0000002e 265ad630 00000008
0x265ad574:   77c0a52e 77c31b78 265ad588 77c0b967
0x265ad584:   00000011 265ad5c0 77c11785 00030348
0x265ad594:   77c1177c 265ad630 00000008 000301b8
0x265ad5a4:   00000001 265ad598 000301d8 265afae4

Instructions: (pc=0x7c94426d)
0x7c94425d:   11 89 95 64 ff ff ff 8b 40 0c 89 85 5c ff ff ff
0x7c94426d:   8b 00 3b 42 04 0f 85 13 01 00 00 3b c1 0f 85 0b

Stack: [0x26570000,0x265b0000),  sp=0x265ad534,  free space=245k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [ntdll.dll+0x3426d]
C  [MSVCRT.dll+0x1c3c9]
C  [MSVCRT.dll+0x1c3e7]
C  [MSVCRT.dll+0x1c42e]
C  [Project1.dll+0x6bb8]
C  [Project1.dll+0x6dda]
C  [Project1.dll+0x2659]
C  [Project1.dll+0x33da]
C  [Project1.dll+0x1e2c]
j  algoCPP.TestJNI1.construitZero([D)Ljava/lang/String;+0
j  algoCPP.TestJNI1.run()V+26
j  pAnalyser.PorosityAnalyser.testReconstruct(Lmateriaux/MatFromPictures;)V+17
j  gUI.MainFrame.actionPerformed(Ljava/awt/event/ActionEvent;)V+586
j  javax.swing.AbstractButton.fireActionPerformed(Ljava/awt/event/ActionEvent;)V+84
j  javax.swing.AbstractButton$Handler.actionPerformed(Ljava/awt/event/ActionEvent;)V+5
j  javax.swing.DefaultButtonModel.fireActionPerformed(Ljava/awt/event/ActionEvent;)V+35
j  javax.swing.DefaultButtonModel.setPressed(Z)V+117
j  javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Ljava/awt/event/MouseEvent;)V+35
j  java.awt.Component.processMouseEvent(Ljava/awt/event/MouseEvent;)V+64
j  javax.swing.JComponent.processMouseEvent(Ljava/awt/event/MouseEvent;)V+23
j  java.awt.Component.processEvent(Ljava/awt/AWTEvent;)V+81
j  java.awt.Container.processEvent(Ljava/awt/AWTEvent;)V+18
j  java.awt.Component.dispatchEventImpl(Ljava/awt/AWTEvent;)V+477
j  java.awt.Container.dispatchEventImpl(Ljava/awt/AWTEvent;)V+42
j  java.awt.Component.dispatchEvent(Ljava/awt/AWTEvent;)V+2
j  java.awt.LightweightDispatcher.retargetMouseEvent(Ljava/awt/Component;ILjava/awt/event/MouseEvent;)V+304
j  java.awt.LightweightDispatcher.processMouseEvent(Ljava/awt/event/MouseEvent;)Z+139
j  java.awt.LightweightDispatcher.dispatchEvent(Ljava/awt/AWTEvent;)Z+50
j  java.awt.Container.dispatchEventImpl(Ljava/awt/AWTEvent;)V+12
j  java.awt.Window.dispatchEventImpl(Ljava/awt/AWTEvent;)V+19
j  java.awt.Component.dispatchEvent(Ljava/awt/AWTEvent;)V+2
j  java.awt.EventQueue.dispatchEvent(Ljava/awt/AWTEvent;)V+46
j  java.awt.EventDispatchThread.pumpOneEventForHierarchy(ILjava/awt/Component;)Z+233
j  java.awt.EventDispatchThread.pumpEventsForHierarchy(ILjava/awt/Conditional;Ljava/awt/Component;)V+26
j  java.awt.EventDispatchThread.pumpEvents(ILjava/awt/Conditional;)V+4
j  java.awt.EventDispatchThread.pumpEvents(Ljava/awt/Conditional;)V+3
j  java.awt.EventDispatchThread.run()V+9
v  ~StubRoutines::call_stub
V  [jvm.dll+0x845a9]
V  [jvm.dll+0xd9317]
V  [jvm.dll+0x8447a]
V  [jvm.dll+0x841d7]
V  [jvm.dll+0x9ed69]
V  [jvm.dll+0x109fe3]
V  [jvm.dll+0x109fb1]
C  [MSVCRT.dll+0x2a3b0]
C  [kernel32.dll+0xb50b]

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  algoCPP.TestJNI1.construitZero([D)Ljava/lang/String;+0
j  algoCPP.TestJNI1.run()V+26
j  pAnalyser.PorosityAnalyser.testReconstruct(Lmateriaux/MatFromPictures;)V+17
j  gUI.MainFrame.actionPerformed(Ljava/awt/event/ActionEvent;)V+586
j  javax.swing.AbstractButton.fireActionPerformed(Ljava/awt/event/ActionEvent;)V+84
j  javax.swing.AbstractButton$Handler.actionPerformed(Ljava/awt/event/ActionEvent;)V+5
j  javax.swing.DefaultButtonModel.fireActionPerformed(Ljava/awt/event/ActionEvent;)V+35
j  javax.swing.DefaultButtonModel.setPressed(Z)V+117
j  javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Ljava/awt/event/MouseEvent;)V+35
j  java.awt.Component.processMouseEvent(Ljava/awt/event/MouseEvent;)V+64
j  javax.swing.JComponent.processMouseEvent(Ljava/awt/event/MouseEvent;)V+23
j  java.awt.Component.processEvent(Ljava/awt/AWTEvent;)V+81
j  java.awt.Container.processEvent(Ljava/awt/AWTEvent;)V+18
j  java.awt.Component.dispatchEventImpl(Ljava/awt/AWTEvent;)V+477
j  java.awt.Container.dispatchEventImpl(Ljava/awt/AWTEvent;)V+42
j  java.awt.Component.dispatchEvent(Ljava/awt/AWTEvent;)V+2
j  java.awt.LightweightDispatcher.retargetMouseEvent(Ljava/awt/Component;ILjava/awt/event/MouseEvent;)V+304
j  java.awt.LightweightDispatcher.processMouseEvent(Ljava/awt/event/MouseEvent;)Z+139
j  java.awt.LightweightDispatcher.dispatchEvent(Ljava/awt/AWTEvent;)Z+50
j  java.awt.Container.dispatchEventImpl(Ljava/awt/AWTEvent;)V+12
j  java.awt.Window.dispatchEventImpl(Ljava/awt/AWTEvent;)V+19
j  java.awt.Component.dispatchEvent(Ljava/awt/AWTEvent;)V+2
j  java.awt.EventQueue.dispatchEvent(Ljava/awt/AWTEvent;)V+46
j  java.awt.EventDispatchThread.pumpOneEventForHierarchy(ILjava/awt/Component;)Z+233
j  java.awt.EventDispatchThread.pumpEventsForHierarchy(ILjava/awt/Conditional;Ljava/awt/Component;)V+26
j  java.awt.EventDispatchThread.pumpEvents(ILjava/awt/Conditional;)V+4
j  java.awt.EventDispatchThread.pumpEvents(Ljava/awt/Conditional;)V+3
j  java.awt.EventDispatchThread.run()V+9
v  ~StubRoutines::call_stub

---------------  P R O C E S S  ---------------

Java Threads: ( => current thread )
  0x262f8a10 JavaThread "TimerQueue" daemon [_thread_blocked, id=3024]
  0x00035b88 JavaThread "DestroyJavaVM" [_thread_blocked, id=2968]
>0x262ecc20 JavaThread "AWT-EventQueue-0" [_thread_in_native, id204]
  0x262ad9c0 JavaThread "AWT-Windows" daemon [_thread_in_native, id=3540]
  0x262ad5b8 JavaThread "AWT-Shutdown" [_thread_blocked, id=3236]
  0x26282d80 JavaThread "Java2D Disposer" daemon [_thread_blocked, id=4052]
  0x00a8bad0 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=3856]
  0x00a8a730 JavaThread "CompilerThread0" daemon [_thread_blocked, id=2120]
  0x00a899c8 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=3152]
  0x00a80a40 JavaThread "Finalizer" daemon [_thread_blocked, id=3952]
  0x00a7f5a8 JavaThread "Reference Handler" daemon [_thread_blocked, id=3060]

Other Threads:
  0x00a7b408 VMThread [id=1120]
  0x00aa65e0 WatcherThread [id=3744]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
 def new generation   total 11072K, used 6169K [0x02ad0000, 0x036d0000, 0x05140000)
  eden space 9856K,  62% used [0x02ad0000, 0x030d6578, 0x03470000)
  from space 1216K,   0% used [0x03470000, 0x03470000, 0x035a0000)
  to   space 1216K,   0% used [0x035a0000, 0x035a0000, 0x036d0000)
 tenured generation   total 147456K, used 0K [0x05140000, 0x0e140000, 0x21ed0000)
   the space 147456K,   0% used [0x05140000, 0x05140000, 0x05140200, 0x0e140000)
 compacting perm gen  total 8192K, used 6414K [0x21ed0000, 0x226d0000, 0x25ed0000)
   the space 8192K,  78% used [0x21ed0000, 0x225139a0, 0x22513a00, 0x226d0000)
No shared spaces configured.

Dynamic libraries:
0x00400000 - 0x0040c000     C:\WINDOWS\system32\java.exe
0x7c910000 - 0x7c9c7000     C:\WINDOWS\system32\ntdll.dll
0x7c800000 - 0x7c904000     C:\WINDOWS\system32\kernel32.dll
0x77da0000 - 0x77e4c000     C:\WINDOWS\system32\ADVAPI32.dll
0x77e50000 - 0x77ee1000     C:\WINDOWS\system32\RPCRT4.dll
0x77be0000 - 0x77c38000     C:\WINDOWS\system32\MSVCRT.dll
0x6d670000 - 0x6d804000     C:\Program Files\Java\jre1.5.0_06\bin\client\jvm.dll
0x77d10000 - 0x77da0000     C:\WINDOWS\system32\USER32.dll
0x77ef0000 - 0x77f37000     C:\WINDOWS\system32\GDI32.dll
0x76ae0000 - 0x76b0f000     C:\WINDOWS\system32\WINMM.dll
0x6d280000 - 0x6d288000     C:\Program Files\Java\jre1.5.0_06\bin\hpi.dll
0x76ba0000 - 0x76bab000     C:\WINDOWS\system32\PSAPI.DLL
0x6d640000 - 0x6d64c000     C:\Program Files\Java\jre1.5.0_06\bin\verify.dll
0x6d300000 - 0x6d31d000     C:\Program Files\Java\jre1.5.0_06\bin\java.dll
0x6d660000 - 0x6d66f000     C:\Program Files\Java\jre1.5.0_06\bin\zip.dll
0x6d000000 - 0x6d167000     C:\Program Files\Java\jre1.5.0_06\bin\awt.dll
0x72f50000 - 0x72f76000     C:\WINDOWS\system32\WINSPOOL.DRV
0x76320000 - 0x7633d000     C:\WINDOWS\system32\IMM32.dll
0x774a0000 - 0x775dd000     C:\WINDOWS\system32\ole32.dll
0x736b0000 - 0x736f9000     C:\WINDOWS\system32\ddraw.dll
0x73b10000 - 0x73b16000     C:\WINDOWS\system32\DCIMAN32.dll
0x73890000 - 0x73960000     C:\WINDOWS\system32\D3DIM700.DLL
0x6d240000 - 0x6d27d000     C:\Program Files\Java\jre1.5.0_06\bin\fontmanager.dll
0x74690000 - 0x746db000     C:\WINDOWS\system32\MSCTF.dll
0x7c9d0000 - 0x7d1f2000     C:\WINDOWS\system32\shell32.dll
0x77f40000 - 0x77fb6000     C:\WINDOWS\system32\SHLWAPI.dll
0x77390000 - 0x77492000     C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9\comctl32.dll
0x58b50000 - 0x58be7000     C:\WINDOWS\system32\comctl32.dll
0x6d4c0000 - 0x6d4d3000     C:\Program Files\Java\jre1.5.0_06\bin\net.dll
0x719f0000 - 0x71a07000     C:\WINDOWS\system32\WS2_32.dll
0x719e0000 - 0x719e8000     C:\WINDOWS\system32\WS2HELP.dll
0x6d4e0000 - 0x6d4e9000     C:\Program Files\Java\jre1.5.0_06\bin\nio.dll
0x69800000 - 0x69857000     C:\Documents and Settings\Housseman\JAVA\workspace01\MaterialMaker\Project1.dll

VM Arguments:
jvm_args: -Xms156m -Xmx500m
java_command: pAnalyser/PorosityAnalyser
Launcher Type: SUN_STANDARD

Environment Variables:
CLASSPATH=.;C:\Program Files\Java\jre1.5.0_06\lib\ext\QTJava.zip
PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\QuickTime\QTSystem\
USERNAME=Housseman
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 15 Model 4 Stepping 1, GenuineIntel

---------------  S Y S T E M  ---------------

OS: Windows XP Build 2600 Service Pack 2

CPU:total 2 family 15, cmov, cx8, fxsr, mmx, sse, sse2, ht

Memory: 4k page, physical 514116k(88904k free), swap 1256680k(490696k free)

vm_info: Java HotSpot(TM) Client VM (1.5.0_06-b05) for windows-x86, built on Nov 10 2005 11:12:14 by "java_re" with MS VC++ 6.0

Bon maintenant la fonction dans laquelle ca plante le plus souvent, donc en fait c jamais allé plus loin que ca...

void reconstruit::calculResultat(){
     //calcule les resultatCorrel par un balayage de tout le cube
    
     cerr<<"creation  : "<<objectiveCL.getNbValues();
     cubeCL = resultatCorrel(objectiveCL.getNbValues()); 
     cerr<<"done"<<endl;
     unsigned long totalChords [2], totalCorrel [2];
     int positif = 0;
     totalChords[0] = 0;
     totalChords[1] = 0;
    
     try{
         cerr<<"dbt boucle readData"<<endl;
         for(int k=0;k>new char;
}    

Puis un ou deux outputs différents avant plantage ou coupure du prog. :

   1)
DEBUT calcul des propriÚtÚs du cube reconstruit
creation  : 41 CrÚation de resultatCorrel

done
dbt boucle readData
// plus de reponse du prog pendant ..........

2)
DEBUT calcul des propriÚtÚs du cube reconstruit
creation  : 372CrÚation de resultatCorrel

#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x7c921639, pid=3904, tid=2476
#  bla bla bla

3)DEBUT calcul des propriÚtÚs du cube reconstruit
creation  : 41CrÚation de resultatCorrel
done
dbt boucle readData
0 / 82 nbChords : 163 , 93
1 / 82 nbChords : 282 , 186

// plus de reponse ou une exception ca depend...

Le pire est que uand j'enleve la fn "calculResultat" ben ca plante toujours...
 cout<<"DEBUT calcul des propriétés du cube reconstruit"<<endl;
 
  /*reC.calculResultat();
  cerr<<reC.cube2Pts.getNbValues()<<" = ";
  cerr<<nbCorrelDist<<" ??? ";
  cerr<<nbChordLengths<<endl;*/
  return....

=>
DEBUT calcul des propriÚtÚs du cube reconstruit
#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x7c93ae22, pid=2712, tid=1252
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_06-b05 mixed mode)
# Problematic frame:
# C  [ntdll.dll+0x2ae22]
#
# An error report file with more information is saved as hs_err_pid2712.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#

Autres infos : t c'est un tablea de doubles, d c'est un double calculable selon t.

Sinon séparer toutes les fns et mettre mon main en java serait possible mais je pensais que les valeurs seraient effacees entre 2 appels de fn; et puis j'en vois pas vraiment l'avantage sauf oui pour debugger un peu ptetr...
 le truc c que je cree un bool[2k][2k][2k] (theCube) en C et les fns travaillent dessus
apres cubeCL c'est des propriétés du cube dans son etat actuel.

Voila n'hesitez pas a poser des questions parce que là je commence a serrer avec certte histoire de JNI;
j'vais faire 2 prog differents et ptetr que ca marchera mieux!!!
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
30 juin 2006 à 21:02
Salut,

bon c'est juste pour t'aiguiller vu que je ne sais pas vraiment le pourquoi du comment de ce que tu fais, je t'ai fais deux trois manipulations possible avec les doubles java, donc voilou pour info je suis avec le jdk1.5 et sous linux bien que je reste sous la norme ANSI niveau cpp donc normalement ca devrait fonctionner sous windows (oublie pas LD_LIBRARY_PATH) .

ui je le dis dans un des 3 commentaire (:P) ne modifie pas une variable java passée en paramètres

fichier java contenant les methodes native :

package org.cs.java.test;

public class TestDoubleJNI {

    static{
        System.loadLibrary("DoubleJNI");
    }
   
    public TestDoubleJNI(){
        init();//init de l'objet
    }
   
    public native double construitZero(double []a);
    public native String toString();
    private native void init();//init de l'objet
    public native void destroy();//destruction de l'objet
}

----------------------------------------
main java

import org.cs.java.test.TestDoubleJNI;

import static java.lang.Math.random;
import static java.lang.System.out;

public class Test {

    public static void main(String [] args){
        double a [] = new double [5];
        for(int i = 0; i < a.length; i++)
            a[i] = random();
       
        TestDoubleJNI t = new TestDoubleJNI();
        out.println("somme des elements = "  + t.construitZero(a));
        out.println(t);
        t.destroy();
    }
}

script ant pour la generation du header

<?xml version="1.0" encoding="UTF-8"?>

   
   
    <target name="mjni">
        <javah classpath="./bin" verbose="yes" force="yes"  destdir="${javah.dest.dir}">
            <class name="org.cs.java.test.TestDoubleJNI"/>
        </javah>
    </target>

fichier cpp (je colle pas le fichier header de la classe java)

#include "org_cs_java_test_TestDoubleJNI.h"

#include "JNIDouble.h"

using namespace org::cs::java::test;

JNIDouble *jniDouble = NULL;

/*
 * Class:     org_cs_java_test_TestDoubleJNI
 * Method:    construitZero
 * Signature: ([D)D
 */
JNIEXPORT jdouble JNICALL Java_org_cs_java_test_TestDoubleJNI_construitZero (JNIEnv *env, jobject testDoubleJNI, jdoubleArray jDouble){
    if(jniDouble == NULL) return 0.0;
    //attention ne modifie pas directement un double java
      return jniDouble->makeZero(jDouble);
}

/*
 * Class:     org_cs_java_test_TestDoubleJNI
 * Method:    toString
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_org_cs_java_test_TestDoubleJNI_toString (JNIEnv *env, jobject testDoubleJNI){
    if(jniDouble == NULL) return NULL;
      return jniDouble->toString();
}

/*
 * Class:     org_cs_java_test_TestDoubleJNI
 * Method:    init
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_org_cs_java_test_TestDoubleJNI_init (JNIEnv *env, jobject testDoubleJNI){
      jniDouble = new JNIDouble(env);
}

/*
 * Class:     org_cs_java_test_TestDoubleJNI
 * Method:    destroy
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_org_cs_java_test_TestDoubleJNI_destroy (JNIEnv *env, jobject testDoubleJNI){
    if(jniDouble != NULL)
          delete jniDouble;
}

object cpp qui prend en charge les manipulations du tableau

#ifndef JNIDOUBLE_H_
    #define JNIDOUBLE_H_
   
    #include <jni.h>
   
    namespace org {
        namespace cs {
            namespace java {
                namespace test {
                   
                    class JNIDouble {
                        public:
                            JNIDouble(JNIEnv *env);
                            ~JNIDouble();
                            jdouble makeZero(jdoubleArray jda);
                            jstring toString();
                        private:
                            JNIEnv *env;
                            unsigned int arrayLength;
                            double *cdouble;
                    };
                }
            }
        }
    }

#endif /*JNIDOUBLE_H_*/

#include "JNIDouble.h"

#include
#include <sstream>

using namespace std;

namespace org {
    namespace cs {
        namespace java {
            namespace test {
                JNIDouble::JNIDouble(JNIEnv *env) {
                    JNIDouble::env = env;
                    arrayLength = 0;
                    cdouble = NULL;
                }
               
                JNIDouble::~JNIDouble() {
                    env = NULL;
                    arrayLength = 0;
                    if(cdouble != NULL) delete cdouble;
                }
               
                jdouble JNIDouble::makeZero(jdoubleArray jda){
                    double addDouble = 0.0;
                      jboolean isCopy = JNI_FALSE;
                    jdouble* doubleArrayElements = env->GetDoubleArrayElements(jda, &isCopy);
                    arrayLength = env->GetArrayLength(jda);
                    cdouble = new double [arrayLength];
                    for (unsigned int i = 0; i < arrayLength; i++)                           addDouble +(cdouble[i] doubleArrayElements[i]);//copie des elements du tableau java dans un tableau C++
                    if (isCopy == JNI_TRUE)//si la copie est good
                           env -> ReleaseDoubleArrayElements(jda, cdouble, JNI_ABORT);//on relache les ressources du tableau java
                    return addDouble;
                }
               
                jstring JNIDouble::toString(){
                    string cstr = "Nombre d'elements du tableau de double (java) : ";
                    // creer un flux de sortie
                    ostringstream oss;
                        // ecrit la taille dans le flux
                    oss << arrayLength;
                    // recupere la chaine et l'ajoute au string courrant
                    cstr.append(oss.str());
                    cstr.append("\nelements du tableau [");
                    for(unsigned int i = 0; i < arrayLength; i++){
                        oss << cdouble[i];//conversion du double courrant
                        cstr.append(oss.str());//ajout de ce double
                        if(i < (arrayLength-1)) cstr.append(", ");//juste pour la lisibilitee
                    }
                    cstr.append("].");//pour le fun
                    return env->NewStringUTF(cstr.c_str());//on retourne la chaine
                }
            }
        }
    }
}

------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

WORA
0
foulla002 Messages postés 47 Date d'inscription mardi 4 juillet 2006 Statut Membre Dernière intervention 5 septembre 2006
20 juil. 2006 à 17:20
j'ai un probleme


 



package apii;
import com.ice.jni.*;
//import com.ms.dll.*;
public class TestWinAPI {
 public static void main (String[] args)   {
   // Give us some room,
   //   let's say 128 characters (MAX 127 + '\0').
   long[] buffer =  { 128 } ;



   try {
System.loadLibrary ("KERNEL32");
} catch ( Exception e ) {
 e.printStackTrace();
 System.exit ( 1 );
}



   StringBuffer username= new StringBuffer((int)buffer[0]);
   GetUserNameA(username, buffer);
   System.out.println("UserName: " + username);
   // IO.PressAnyKey();
   }



 /** @dll.import("ADVAPI32") */
 static native void GetUserNameA
  (StringBuffer userName, long buffer[]);
 // string by ref are passed with a StringBuffer
 // basic scalar type by ref are passed via an array
}


///////////////////////////////////
voici mon fichier .h genere avec javah -jni


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



#ifndef _Included_apii_TestWinAPI
#define _Included_apii_TestWinAPI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     apii_TestWinAPI
 * Method:    GetUserNameA
 * Signature: (Ljava/lang/StringBuffer;[J)V
 */
JNIEXPORT void JNICALL Java_apii_TestWinAPI_GetUserNameA
  (JNIEnv *, jclass, jobject, jlongArray);



#ifdef __cplusplus
}
#endif
#endif
///////////////////////////////////
je ne sais rien faire de plus sachant que qd je compile avec jbuilder je trouve
/////////////////////////////////////////





java.lang.UnsatisfiedLinkError: GetUserNameA
 at apii.TestWinAPI.GetUserNameA(Native Method)
 at apii.TestWinAPI.main(TestWinAPI.java:18)
Exception in thread "main"




qu'est ce que je fais d'autre pour terminer et comment corriger l'erreur c'est urgent merci
0
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
20 juil. 2006 à 17:31
Salut,

[auteurdetail.aspx?ID=813883 foulla002]


1 - merci de dire bonjour, salut, ou autre formule de politesse on est pas des chiens

2 - merci de ne pas pourrir les postes d'autres utilisateur et faire son propre poste

3 - merci d'eviter les mail perso pour des demandes d'aide

------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

WORA
0
Rejoignez-nous