Unsatisfiedlinkerror?!

kevinroot

Mitglied
Hallo

immer wenn ich mein Programm starte bekomme ich die folgende Fehlermeldung:

java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:83)
at serial.Example3.aktualisiereSerialPort(Example3.java:228)
at serial.Example3.initComponents(Example3.java:148)
at serial.Example3.<init>(Example3.java:72)
at serial.Example3$1.run(Example3.java:60)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Wie kann ich dieses Fehler beheben?
 

kevinroot

Mitglied
Java:
package serial;

import gnu.io.*;
//import javax.comm.*; 
import java.util.Enumeration;
import java.io.*;
import java.util.TooManyListenersException;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

//TODO Dialog zur Konfiguration der Schnittstellenparameter


public class Example3 extends JFrame{

	/**
	 * Variable declaration
	 */
	CommPortIdentifier serialPortId;
	Enumeration enumComm;
	SerialPort serialPort;
	OutputStream outputStream;
	InputStream inputStream;
	Boolean serialPortGeoeffnet = false;

	int baudrate = 9600;
	int dataBits = SerialPort.DATABITS_8;
	int stopBits = SerialPort.STOPBITS_1;
	int parity = SerialPort.PARITY_NONE;
	
	/**
	 * Fenster
	 */
	JPanel panel = new JPanel (new GridBagLayout());
	
	JPanel panelSetup = new JPanel(new GridBagLayout());
	JPanel panelKommuniziere = new JPanel(new GridBagLayout());
	
	JComboBox auswahl = new JComboBox();
	JButton oeffnen = new JButton("Öffnen");
	JButton schliessen = new JButton("Schließen");
	JButton aktualisieren = new JButton("Aktualisieren");
	
	JButton senden = new JButton("Nachricht senden");
	JTextField nachricht = new JTextField();
	JCheckBox echo = new JCheckBox("Echo");
	
	JTextArea empfangen = new JTextArea();
	JScrollPane empfangenJScrollPane = new JScrollPane();
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Programm gestartet");
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new Example3();
			}
		});
		System.out.println("Main durchlaufen");
	}
	
	/**
	 * Konstruktor
	 */
	public Example3()
	{
		System.out.println("Konstruktor aufgerufen");
		initComponents();
	}
	protected void finalize()
	{
		System.out.println("Destruktor aufgerufen");
	}
 
	void initComponents()
	{
		GridBagConstraints constraints = new GridBagConstraints();
		
		setTitle("Öffnen und Senden");
		addWindowListener(new WindowListener());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// TODO schliessen.setEnabled(false);
		// TODO senden.setEnabled(false);
		
		oeffnen.addActionListener( new oeffnenActionListener());
		schliessen.addActionListener(new schliessenActionListener());
		aktualisieren.addActionListener(new aktualisierenActionListener());
		senden.addActionListener(new sendenActionListener());
		
		empfangenJScrollPane.setVerticalScrollBarPolicy(
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		empfangenJScrollPane.setHorizontalScrollBarPolicy(
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		empfangenJScrollPane.setViewportView(empfangen);
		
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.insets = new Insets(5, 5, 5, 5);
		panelSetup.add(auswahl, constraints);
		
		constraints.gridx = 1;
		constraints.weightx = 0;
		panelSetup.add(oeffnen, constraints);
		
		constraints.gridx = 2;
		panelSetup.add(schliessen, constraints);
		
		constraints.gridx = 3;
		panelSetup.add(aktualisieren, constraints);
		
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.weightx = 1;
		panel.add(panelSetup, constraints);

		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.weightx = 0;
		panelKommuniziere.add(senden, constraints);
		
		constraints.gridx = 1;
		constraints.weightx = 1;
		panelKommuniziere.add(nachricht, constraints);
		
		constraints.gridx = 2;
		constraints.weightx = 0;
		panelKommuniziere.add(echo, constraints);
		
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.weightx = 1;
		panel.add(panelKommuniziere, constraints);
		
		constraints.gridx = 0;
		constraints.gridy = 2;
		constraints.weightx = 1;
		constraints.weighty = 1;
		constraints.fill = GridBagConstraints.BOTH;
		panel.add(empfangenJScrollPane, constraints);
		
		aktualisiereSerialPort();
		
		add(panel);
		pack();
		setSize(600, 300);
		setVisible(true);

		System.out.println("Fenster erzeugt");
	}
	
	boolean oeffneSerialPort(String portName)
	{
		Boolean foundPort = false;
		if (serialPortGeoeffnet != false) {
			System.out.println("Serialport bereits geöffnet");
			return false;
		}
		System.out.println("Öffne Serialport");
		enumComm = CommPortIdentifier.getPortIdentifiers();
		while(enumComm.hasMoreElements()) {
			serialPortId = (CommPortIdentifier) enumComm.nextElement();
			if (portName.contentEquals(serialPortId.getName())) {
				foundPort = true;
				break;
			}
		}
		if (foundPort != true) {
			System.out.println("Serialport nicht gefunden: " + portName);
			return false;
		}
		try {
			serialPort = (SerialPort) serialPortId.open("Öffnen und Senden", 500);
		} catch (PortInUseException e) {
			System.out.println("Port belegt");
		}
		try {
			outputStream = serialPort.getOutputStream();
		} catch (IOException e) {
			System.out.println("Keinen Zugriff auf OutputStream");
		}
		try {
			inputStream = serialPort.getInputStream();
		} catch (IOException e) {
			System.out.println("Keinen Zugriff auf InputStream");
		}
		try {
			serialPort.addEventListener(new serialPortEventListener());
		} catch (TooManyListenersException e) {
			System.out.println("TooManyListenersException für Serialport");
		}
		serialPort.notifyOnDataAvailable(true);
		try {
			serialPort.setSerialPortParams(baudrate, dataBits, stopBits, parity);
		} catch(UnsupportedCommOperationException e) {
			System.out.println("Konnte Schnittstellen-Paramter nicht setzen");
		}
		
		serialPortGeoeffnet = true;
		return true;
	}
	
	void schliesseSerialPort()
	{
		if ( serialPortGeoeffnet == true) {
			System.out.println("Schließe Serialport");
			serialPort.close();
			serialPortGeoeffnet = false;
		} else {
			System.out.println("Serialport bereits geschlossen");
		}
	}
	
	void aktualisiereSerialPort()
	{
		System.out.println("Akutalisiere Serialport-Liste");
		if (serialPortGeoeffnet != false) {
			System.out.println("Serialport ist geöffnet");
			return;
		}
		auswahl.removeAllItems();
		enumComm = CommPortIdentifier.getPortIdentifiers(); //TODO Fehlerbehebung
		while(enumComm.hasMoreElements()) {
			serialPortId = (CommPortIdentifier) enumComm.nextElement();
			if (serialPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
				System.out.println("Found:" + serialPortId.getName());
				auswahl.addItem(serialPortId.getName());
			}
		}
	}
	
	void sendeSerialPort(String nachricht)
	{
		System.out.println("Sende: " + nachricht);
		if (serialPortGeoeffnet != true)
			return;
		try {
			outputStream.write(nachricht.getBytes());
		} catch (IOException e) {
			System.out.println("Fehler beim Senden");
		}
	}
	
	void serialPortDatenVerfuegbar() {
		try {
			byte[] data = new byte[150];
			int num;
			while(inputStream.available() > 0) {
				num = inputStream.read(data, 0, data.length);
				System.out.println("Empfange: "+ new String(data, 0, num));
				empfangen.append(new String(data, 0, num));
			}
		} catch (IOException e) {
			System.out.println("Fehler beim Lesen empfangener Daten");
		}
	}
	
	class WindowListener extends WindowAdapter {
		public void windowClosing(WindowEvent event) {
			schliesseSerialPort();
			System.out.println("Fenster wird geschlossen");
		}
	}
	
	class oeffnenActionListener implements ActionListener {
		public void actionPerformed (ActionEvent event) {
			System.out.println("oeffnenActionListener");
			// TODO sperre Button Öffnen und Aktualisieren
			// TODO entsperre Nachricht senden und Schließen
			oeffneSerialPort((String) auswahl.getSelectedItem());
		}
	}
	class schliessenActionListener implements ActionListener {
		public void actionPerformed (ActionEvent event) {
			System.out.println("schliessenActionListener");
			// TODO entsperre Button Öffnen und Aktualisieren
			// TODO sperre Nachricht senden und Schließen
			schliesseSerialPort();
		}
	}
	class aktualisierenActionListener implements ActionListener {
		public void actionPerformed (ActionEvent event) {
			System.out.println("aktualisierenActionListener");
			aktualisiereSerialPort();
		}
	}
	class sendenActionListener implements ActionListener {
		public void actionPerformed (ActionEvent event) {
			System.out.println("sendenActionListener");
			if ( echo.isSelected() == true)
				empfangen.append(nachricht.getText() + "\n");
			sendeSerialPort(nachricht.getText() + "\n");
		}
	}
	/**
	 * 
	 */
	class serialPortEventListener implements SerialPortEventListener {
		public void serialEvent(SerialPortEvent event) {
			System.out.println("serialPortEventlistener");
			switch (event.getEventType()) {
			case SerialPortEvent.DATA_AVAILABLE:
				serialPortDatenVerfuegbar();
				break;
			case SerialPortEvent.BI:
			case SerialPortEvent.CD:
			case SerialPortEvent.CTS:
			case SerialPortEvent.DSR:
			case SerialPortEvent.FE:
			case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
			case SerialPortEvent.PE:
			case SerialPortEvent.RI:
			default:
			}
		}
	}	
}
 

Michael...

Top Contributor
In der importierten Klasse gnu.io.CommPortIdentifier wird eine extener Lib namens rxtxSerial (vermutlich dll) benötigt. Diese wird aber nicht im java.library.path gefunden:

Mögliche Lösung:
- Herausfinden auf welche Verzeichnisse der library.path verweist
- die Datei rxtxSerial.* Datei suchen
- die Datei in ein Verzeichnis des library.path verschieben oder den libray.path versuchen anzupassen.
 

Michael...

Top Contributor
Im library.path steckt überlicher Weise das System Verzeichnis von Windows und die Einträge in der PATH Variablen drin.
Die referenzierten Verzeichnisse kann man sich auch aus Java heraus anzeigen lassen.
Java:
System.out.println(System.getProperty("java.library.path"));
Beim Kopieren besteht das Risiko, das die Library wiederum anderer Libraries anzieht.
Daher ist es m.M. besser das aktuelle Verzeichnis beim Starten über Parameter -Djava.library.path=... mit auzunehmen. Eine Erweiterung der PATH Variablen sollte auch funktionieren.

Üblicherweise sollte es aber auch "Installationshinweise" geben, wenn man fremde Klassen/Biblieotheken einbindet ;-)
Installation for Windows - Rxtx
Auch wenn ich die Vorgehensweise fremde Libs ins Java Verzeichnis zu kopieren nicht gerade schön finde.
 

Ähnliche Java Themen

Neue Themen


Oben