1

I am doing some serial communication via Java Applet and rxtx library. Applet is working fine but when loaded more then once I am having issues with

UnsatisfiedLinkError: rxtxSerial.dll already loaded in another classloader

Following How to unload library (DLL) from java JVM I am trying to unload dll, which I believe could help with this issue.

From what I understood I could have custom class loader for rxtx class and force garbage collector to clean everything, including loaded dll, at some point.

So the following code should help (I am trying to load it in the function responsible for starting communication with serial port).

cl = new CustomClassLoader();
ca = cl.findClass("gnu.io.CommPortIdentifier");
a = ca.newInstance();
p = ca.getMethod("getPortIdentifier");
portId = (CommPortIdentifier) p.invoke(a, comportUsed);

unfortunately it throws ClassNotFoundException

I guess something is wrong with my custom class loader, but have no clue what.

Please help me finding that or a way to overcome UnsatisfiedLinkError.

Community
  • 1
  • 1
norbi771
  • 814
  • 2
  • 12
  • 29

1 Answers1

0

I had to unload a buggy DLL at runtime some time ago. Unfortunately there were no reliable solutions for this. Even if you use a custom class loader, some OS as Windows Vista just kill the VM on DLL unload. At the end I had to replace DLL with my own driver. You can check the discussion we had before

https://forums.oracle.com/forums/thread.jspa?threadID=1546756&tstart=1185

Probably you will have to revise your design to avoid loading of DLL several times. A small dedicated Java server/app would do the job

Sergey
  • 1,332
  • 2
  • 18
  • 30
  • 1
    Thank you for your answer. I don't know hot to revise my code. I load rxrtxSerial dll only once ... in the applet. Via this applet I print some pay-slips on a fiscal printer. I can print many of them, many times and it is working fine. However I use the same applet (with some different parameters) for sending different commands (like printDailyreport). And if I try to print daily report after printing some pay-slips then the crash occur ... and vice versa if I start from printing daily report I am no longer able to print pay-slips. Strange, but all that goes to the UnsatisfiedLinkError. – norbi771 Jan 07 '13 at 13:52
  • I see 2 possible solutions: share the same ClassLoader between applets (think the URL must be the same) or ensure that modules without Serial Port functionality (like printDailyreport) do not load the DLL. – Sergey Jan 07 '13 at 14:13
  • could you advice how to share the same ClassLoader between applets? My applet has the same url, codebase, everything. The only difference is that when I do daily report, the action parameter of the applet is set to "dailyReport" ... the only thing I could think of is to pass the action in a different way (with other data that is base64 encapsulated json encoded table ... I am sending this data from PHP). Daily report is some functionality of the fiscal printer - you send the command (via serial) to the printer and it prints the report. – norbi771 Jan 07 '13 at 20:28
  • You might try [jssc](http://code.google.com/p/java-simple-serial-connector/). (it has shown stable enough to be used in production environment as opposed to RxTx) – linski Jan 07 '13 at 20:40
  • Thanks, I am just considering switching to jssc. Unfortunately didn't find that project when I started programming my serial port applet and now it is a bit tricky for me to do the change. Also I have another project where I have to use applet and JNA with some other dll ... and in this second project there are similar problems. That's way I am interested solving the issue not by replacing the library ... but jssc will probably be my next move anyway. – norbi771 Jan 08 '13 at 00:15