Salut
actuellement je fait une application où je doit intégrer un code écrit en C a mon programme qui est en Java pour cela j'utilise la JNA(Java Native Access) donc en faisant un essai où j'appelle la fonction "sethello" qui se trouve dans le programme en C après avoir appuyé sur un bouton de mon interface java.
j'ai suivis les instruction de ce tutoriel Exécuter du code natif en Java : JNI VS JNA le problème c'est que j'arrive pas à faire que l'interface "User" trouve le chemin du programme en C
voici la classe java
l'interface :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 package hello; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import com.sun.jna.Native; public class World { private JFrame frame; /** * Launch the application * @param args */ public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { try { World window = new World(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application */ public World() { createContents(); } /** * Initialize the contents of the frame */ private void createContents() { frame = new JFrame(); frame.getContentPane().setLayout(null); frame.setBounds(100, 100, 345, 196); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button = new JButton(); button.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent arg0) { User lib = User.demo; lib.sethello(4); } }); button.setText("New JButton"); button.setBounds(113, 56, 106, 26); frame.getContentPane().add(button); } }
et voici le code C
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 package hello; import com.sun.jna.Native; import com.sun.jna.win32.StdCallLibrary; public interface User extends StdCallLibrary{ public int sethello (int i); User demo = (User) Native.loadLibrary("hello", User.class); }
quand j'appuie sur le bouton j'obtiens ce message:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <stdio.h> #include <stdlib.h> int main(void) { int i=sethello(4); printf("%ld\n",i); return EXIT_SUCCESS; } int sethello (int i){ int j; for (j = 0 ; j < i ; j++) { puts("!!!Hello World!!!\n"); /* prints !!!Hello World!!! */ } return i; }
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: Unable to load library 'hello': Le module spécifié est introuvable.
merci pour votre aide
Partager