Ich versuche den Parallel Port unter Ubuntu mittels jni anzusprechen:
ParallelPort.java:
```import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ParallelPort extends JFrame implements ActionListener{
private JButton btnOn;
private JButton btnOff;
private JLabel status;
private JPanel label;
private JPanel buttons;
public ParallelPort(){
setTitle("access Parallel Port");
status = new JLabel("Port: Off");
status.setForeground(Color.red);
btnOn = new JButton("On");
btnOn.setActionCommand("on");
btnOn.addActionListener(this);
btnOff = new JButton("Off");
btnOff.setActionCommand("off");
btnOff.addActionListener(this);
label = new JPanel();
label.add(status);
buttons = new JPanel();
buttons.add(btnOn);
buttons.add(btnOff);
Container container = getContentPane();
container.add(label, BorderLayout.NORTH);
container.add(buttons, BorderLayout.CENTER);
}
public static void main(String[] args){
ParallelPort f = new ParallelPort();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
String event = arg0.getActionCommand();
if(event.equals("on")){
status.setForeground(Color.green);
accessPortC.access1(255, 15, 1);
}
else if(event.equals("off")){
status.setForeground(Color.red);
accessPortC.access1(0, 15, 1);
}
}
static{
System.loadLibrary("accessPortC");
}
}```
accessPortC.java:
```
public class accessPortC {
public static native int access1(int Pdata, int Pcontrol, int Pbaseport);
}```
accessPortC.h:
```/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class accessPortC */
#ifndef _Included_accessPortC
#define _Included_accessPortC
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: accessPortC
* Method: access1
* Signature: (III)I
*/
JNIEXPORT jint JNICALL Java_accessPortC_access1
(JNIEnv *, jclass, jint, jint, jint);
#ifdef __cplusplus
}
#endif
#endif```
accessPortC.c
```#include "accessPortC.h"
#include <stdio.h>
#define LPT1 0x378
#define LPT2 0x278
#define LPT3 0x3BC
JNIEXPORT jint JNICALL Java_accessPortC_access1
(JNIEnv * env, jclass cl, jint Pdata, jint Pcontrol, jint Pbaseport)
{
int data = 0;
int control = 0;
int BASEPORT = 0;
switch(Pbaseport)
{
case '1': BASEPORT = LPT1; break;
case '2': BASEPORT = LPT2; break;
case '3': BASEPORT = LPT3; break;
default: BASEPORT = LPT1;
}
data = Pdata;
if(data < 0 || data > 255)
{perror("Error: data value out of range"); exit(1);}
control = Pcontrol;
if(control < 0 || control > 255)
{perror("Error: control value out of range"); exit(1);}
if(ioperm(BASEPORT, 3, 1))
{perror("Error: cannot access ioport open"); exit(1);}
outb(data, BASEPORT);
outb(control ^ 0x0B, BASEPORT +2);
if(ioperm(BASEPORT, 3, 0))
{perror("Error: cannot access ioport"); exit(1);}
exit(0);
}
```
Da ich es nicht schaffe als normaler User auf den parallelPort zuzugreifen ("Error: cannot access ioport open: Operation not permitted"), versuche ich das ganze als root auszuführen.
Als root erhalte ich folgende Fehlermeldung: java: symbol lookup error: .../src/libaccessPortC.so: undefined symbol: outb
libaccessPortC.so erstelle ich mit: gcc -I jdk/include -I jdk/include/linux -shared -o libaccessPortC.so accessPortC.c
Weiterhin füge ich noch das aktuelle Verzeichnis dem Library Path hinzu: export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
Kann mir jemand sagen wo der Fehler liegt. Wenn ich das c-programm alleine ausführe funktioniert der Zugriff auf den parallel port.