Bonjour,
J'ai un petit souci actuellement avec l'utilisation de JNA. J'essaye d'exécuter Matlab avec JNA ce qui fonctionne avec du java pure. Mais pose problème lorsque je veux créer un plugin sous Eclipse. Ce plugin aura pour but d'ouvrir matlab grace un clique sur un bouton.
Mon problème est lorsque je clique sur mon bouton j'obtiensJe comprends bien qu'il doit y avoir un problème au niveau de l'importation de la librairie JNA. Mais ce problème se pose uniquement au moment du linkage car la compilation se passe normalement
Code : Sélectionner tout - Visualiser dans une fenêtre à part java.lang.NoClassDefFoundError: com/sun/jna/Library
Voici mon code en java pure qui fonctionne:
Classe1:
Main:
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 public class CallMatblabByJNA implements Library { int i; boolean myBool = true; Pointer myEngine; private interface EngineLibrary extends Library { String LIBRARY_NAME = "libeng"; EngineLibrary INSTANCE = (EngineLibrary) Native.loadLibrary(LIBRARY_NAME, EngineLibrary.class); int engEvalString(Pointer ep, String string); Pointer engOpenSingleUse(String startcmd, Pointer reserved, IntByReference retstatus); int engSetVisible(Pointer ep, boolean newVal); int engGetVisible(Pointer ep, ByteByReference bVal); Pointer engOpen(); int engClose(Pointer ep); Pointer engGetVariable(Pointer ep, String name); int engPutVariable(Pointer ep, String name, Pointer ap); int engOutputBuffer(Pointer ep, ByteBuffer buffer, int buflen); } public void openMatlab() { myEngine = EngineLibrary.INSTANCE.engOpen(); } public void closeMatlab() { EngineLibrary.INSTANCE.engClose(myEngine); } public void callEvalString() { EngineLibrary.INSTANCE.engEvalString(myEngine, "surf(peaks)"); } }
Maintenant voici mon code sous forme de plugin
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 public class MainJNA { static CallMatblabByJNA mat = new CallMatblabByJNA(); public static void open() { mat.openMatlab(); } public static void execute() { mat.callEvalString(); } public static void close() { mat.closeMatlab(); } public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setSize(600, 200); shell.setText("Button Example"); final Button button = new Button(shell, SWT.PUSH); button.setBounds(40, 50, 50, 20); button.setText("Open"); final Button bExe = new Button(shell, SWT.PUSH); bExe.setBounds(100, 50, 70, 20); bExe.setText("Execute"); final Button bClose = new Button(shell, SWT.PUSH); bClose.setBounds(250, 50, 70, 20); bClose.setText("Close"); final Text text = new Text(shell, SWT.BORDER); text.setBounds(400, 50, 70, 20); button.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent event) { text.setText("Open"); MainJNA.open(); } public void widgetDefaultSelected(SelectionEvent event) { text.setText("No worries!"); } }); bClose.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent event) { text.setText("Close"); MainJNA.close(); } public void widgetDefaultSelected(SelectionEvent event) { text.setText("No worries!"); } }); bExe.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent event) { text.setText("Execute"); MainJNA.execute(); } public void widgetDefaultSelected(SelectionEvent event) { text.setText("No worries!"); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
Classe1:
Classe2:
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public class ConsoleMatlabView extends ViewPart { static private StyledText viewer; private Action openAction; private Action clearAction; private Action terminateAction; URL oImageURL = null; MatlabEngine matlabEngine = new MatlabEngine(); @Override public void createPartControl(Composite parent) { viewer = new StyledText(parent, SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL); viewer.setEditable(false); createActions(); contributeToActionBars(); } @Override public void setFocus() { // TODO Auto-generated method stub } private void createActions() { openAction = new Action() { public void run() { try { matlabEngine.openMatlab(); } catch(Exception ex) { ex.printStackTrace(); } } }; openAction.setText("Open"); openAction.setToolTipText("Open tooltip"); oImageURL = Platform.getBundle(Activator.PLUGIN_ID).getResource("icons/open.ico"); openAction.setImageDescriptor(ImageDescriptor.createFromURL(oImageURL)); clearAction = new Action() { public void run() { MessageDialog.openInformation(viewer.getShell(), "Clear", "Clear message"); } }; clearAction.setText("Clear"); clearAction.setToolTipText("Clear tooltip"); oImageURL = Platform.getBundle(Activator.PLUGIN_ID).getResource("icons/clear.ico"); clearAction.setImageDescriptor(ImageDescriptor.createFromURL(oImageURL)); terminateAction = new Action() { public void run() { MessageDialog.openInformation(viewer.getShell(), "Terminate", "Terminate message"); } }; terminateAction.setText("Terminate"); terminateAction.setToolTipText("Terminate tooltip"); oImageURL = Platform.getBundle(Activator.PLUGIN_ID).getResource("icons/terminate.ico"); terminateAction.setImageDescriptor(ImageDescriptor.createFromURL(oImageURL)); } private void contributeToActionBars() { IActionBars bars = getViewSite().getActionBars(); fillToolBar(bars.getToolBarManager()); } private void fillToolBar(IToolBarManager manager) { manager.add(openAction); manager.add(terminateAction); manager.add(clearAction); } }
J'utilise la version modeling d'Eclipse 3.5
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 public class MatlabEngine implements Library { int i; boolean myBool = true; Pointer myEngine; private interface EngineLibrary extends Library { String LIBRARY_NAME = "libeng"; EngineLibrary INSTANCE = (EngineLibrary) Native.loadLibrary(LIBRARY_NAME, EngineLibrary.class); int engEvalString(Pointer ep, String string); Pointer engOpenSingleUse(String startcmd, Pointer reserved, IntByReference retstatus); int engSetVisible(Pointer ep, boolean newVal); int engGetVisible(Pointer ep, ByteByReference bVal); Pointer engOpen(); int engClose(Pointer ep); Pointer engGetVariable(Pointer ep, String name); int engPutVariable(Pointer ep, String name, Pointer ap); int engOutputBuffer(Pointer ep, ByteBuffer buffer, int buflen); } public void openMatlab() { myEngine = EngineLibrary.INSTANCE.engOpen(); } public void closeMatlab() { EngineLibrary.INSTANCE.engClose(myEngine); } public void callEvalString() { EngineLibrary.INSTANCE.engEvalString(myEngine, "surf(peaks)"); } }
Vous remerciant par avance de vos réponse.
Partager