Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

Arduino 1:1.8.9-3(compiled with jdk9?) ByteBuffer breaks libraries

Open DWShuo opened this issue 5 years ago • 33 comments

Using RF24 library for nRF24L01 upgraded arduino (1:1.8.9-1 -> 1:1.8.9-3) OS: arch linux

Following error is thrown after upgrade Exception in thread "EventThread /dev/ttyUSB1" java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer; at processing.app.Serial.serialEvent(Serial.java:185) at jssc.SerialPort$LinuxEventThread.run(SerialPort.java:1299)

Relevant github issue? https://github.com/apache/felix/pull/114

DWShuo avatar May 21 '19 05:05 DWShuo

Just rolled back to package version 1:1.8.9-1, and everything works fine. Seems like 1:1.8.9-3 is indeed the issue

DWShuo avatar May 21 '19 06:05 DWShuo

@NicoHood maybe you lost this :wink:

facchinm avatar May 21 '19 07:05 facchinm

Same issue here.

Rolling back to 1:1.8.9-1 working :)

harkor avatar May 29 '19 06:05 harkor

What should I do to fix this??

NicoHood avatar May 29 '19 16:05 NicoHood

I think compiling with java8 should fix it

facchinm avatar May 29 '19 16:05 facchinm

But shouldn't Arduino support java 8 and all higher versions?

NicoHood avatar May 30 '19 06:05 NicoHood

I think you need to compile the jars with java 8 exactly, so the dependency here should be java-runtime=8 (I think)

facchinm avatar May 30 '19 07:05 facchinm

Ok figure it out, for some reason arch defaults to jdk8, you have to manually set java to a higher version. Do this to check which versions of java you have installed archlinux-java status if java-8-openjdk/jre is your only option then run this pacman -Syu jdk-openjdk run archlinux-java status again should have java11 now as an option switch to java11 sudo archlinux-java set java-11-openjdk

DWShuo avatar May 30 '19 18:05 DWShuo

I am not very familiar with java programming. Code compiled with java will only run with java8 interpreter? Or will it run with 8 and every version above? Is the problem that I've compiled the code with java 11, but with an old java8 interpreter it crashes? Changing the OS java version does not sound like a solution, more a workaround. Can somebody please explain me better how this works?

NicoHood avatar May 31 '19 13:05 NicoHood

Kind of a moot point, since the Arduino IDE bundles the correct Java JRE which precisely matches the version of the Java SDK they used to build the code.

PaulStoffregen avatar May 31 '19 13:05 PaulStoffregen

@NicoHood as @PaulStoffregen says, the Java SDK should probably be used. The reason is not all Java 8's are the same.

On my arch linux system, just updated:

$ uname -a
Linux wink-desktop 5.1.9-arch1-1-ARCH #1 SMP PREEMPT Tue Jun 11 16:18:09 UTC 2019 x86_64 GNU/Linux

I have java 8 installed as my default:

$ archlinux-java status
Available Java environments:
  java-10-openjdk
  java-11-openjdk
  java-8-openjdk (default)

And the version reported by java is 1.8.0_212:

$ /usr/bin/java -version
openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-b01)
OpenJDK 64-Bit Server VM (build 25.212-b01, mixed mode)

Which is slightly different from the version provided by arduino-1.8.9, 1.8.0_191:

$ ./bin/arduino-1.8.9/java/bin/java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

And I don't get the "NoSuchMethodError" when I'm using arduino-1.8.9 directly but I do get this error when I install the Arch Linux arduino package. A workaround that is suggested in the Arch Linux bug report 62704 of changing to java 10 or 11 does resolve this bug but then I get some warnings:

$ /usr/share/arduino/arduino
Picked up JAVA_TOOL_OPTIONS: 
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by processing.app.linux.GTKLookAndFeelFixer (file:/usr/share/arduino/lib/arduino-core.jar) to field com.sun.java.swing.plaf.gtk.GTKLookAndFeel.styleFactory
WARNING: Please consider reporting this to the maintainers of processing.app.linux.GTKLookAndFeelFixer
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

So it seems it would be best to change Arch Linux arduino PKGBUILD to use the java provided by arduino and not the system installed version.

winksaville avatar Jun 13 '19 22:06 winksaville

Seems to me this is a problem with Arch Linux. The software Arduino.cc distributes always has the correct JRE bundled, so I do not believe reporting the problem here is correct.

PaulStoffregen avatar Jun 13 '19 22:06 PaulStoffregen

Hi, i confirm with Java 12 i had issue with the menu theme of Arduino Ide. Reinstalling Java 8 and setting: archlinux-java set java-8-openjdk/jre i can get the correct theme again.

This was the output running arduino ide with Java 12 as default:

$ arduino                   
Picked up JAVA_TOOL_OPTIONS: 
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by processing.app.linux.GTKLookAndFeelFixer (file:/usr/share/arduino/lib/arduino-core.jar) to field com.sun.java.swing.plaf.gtk.GTKLookAndFeel.styleFactory
WARNING: Please consider reporting this to the maintainers of processing.app.linux.GTKLookAndFeelFixer
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

toketin avatar Jul 11 '19 18:07 toketin

I can not access Serial using Java 8 and Arduino 1.8.9-3, but it works with Arduino 1.8.9-1.

Java 12 is not affected by that problem, but shows warnings as reported from some of you.

fam4r avatar Oct 09 '19 16:10 fam4r

I had this same issue on Arch Linux. Using java-8-jdk, the error didn't come up again (and the monitor functioned properly) after switching to java-10-openjdk. This was done by issueing the command:

archlinux-java set java-10-openjdk

serra-pablo avatar Jan 15 '20 13:01 serra-pablo

Is there a way to fix java compatibility in the arduino IDE, so users do not need to switch their java version?

And another question for packaging arduino: Is the compiling java version a problem, or the running java version? From the comments I understood that only the java runtime version is important. I could add a simple wrapper around the arduino executable that forces to start with java8 (unless the bugs are fixed with newer java versions).

NicoHood avatar Jan 21 '20 13:01 NicoHood

on arch, same problem, fixed by sudo archlinux-java set java-11-openjdk. Thanks @DWShuo

@PaulStoffregen, you say

Kind of a moot point, since the Arduino IDE bundles the correct Java JRE which precisely matches the version of the Java SDK they used to build the code.

Then why it isn't used when people start on arch using "#> arduino"?

molecular avatar Jun 27 '20 07:06 molecular

@molecular A new arduino version was released on arch. Could you please try if it work with java 11 and 14 on your system?

sudo archlinux-java set java-14-openjdk

It should work with both, but please try it out.

NicoHood avatar Jul 11 '20 09:07 NicoHood

On my system with Arduino 1.8.13 from the Arch repository, the Serial monitor works with both java 11 and 14 (though it has been a little circumstantial in the past).

I don't know if java 8 is still relevant, but with java-8-openjdk, I still get an error:

Exception in thread "EventThread /dev/ttyACM0" java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer;
	at processing.app.Serial.processSerialEvent(Serial.java:210)
	at processing.app.Serial.serialEvent(Serial.java:183)
	at jssc.SerialPort$LinuxEventThread.run(SerialPort.java:1299)

gkatev avatar Jul 19 '20 09:07 gkatev

@facchinm Is this something the arduino team is able to fix, or should I bump the minimum required java version to 11 for the arch package?

NicoHood avatar Aug 09 '20 13:08 NicoHood

@NicoHood There's a branch https://github.com/cmaglie/Arduino/tree/java-11 for java11 full compatibility but it still needs some testing. I think that forcing java-8-openjdk in AUR could be the best idea (so replacing the >=8 with ==8)

facchinm avatar Aug 12 '20 08:08 facchinm

@facchinm My experience is that the Serial monitor does not work with java-8-openjdk, so I don't see why this version should be forced..

On another note, what is the root cause of this bug? A method that is available in the "bundled JRE" but not in the openjdk? If it's just one misbehaving method, could we replace it with something else that works across the board?

gkatev avatar Aug 12 '20 09:08 gkatev

@gkatev it doesn't work because the package gets compiled using java11 if it's already installed. If you force the PKGBUILD to use jdk8 the package will run seamlessly (I'm on Arch BTW :wink: )

facchinm avatar Aug 12 '20 09:08 facchinm

@facchinm thanks for this note, I will update the package! However I was unable to reproduce the bug @gkatev reported with java8. On my system the serial monitor seems to work. And I made sure, that archlinux-java was set to 8 of course.

@gkatev once the new arduino version is in the arch packages, please let me know if that works for you now on all java versions. Thanks for the feedback!

NicoHood avatar Aug 16 '20 08:08 NicoHood

@NicoHood I too have had some hit and miss results in the past. As far as I can tell from recent testing, the bug appears as soon as the device sends data on the Serial port, and not before.

I upgraded arduino to 1.8.13-2 and was still able to consistently reproduce the bug, with java-8-openjdk.

$ realpath $(which java)
/usr/lib/jvm/java-8-openjdk/jre/bin/java

I also checked some random .class file from the jars (/usr/share/arduino/lib/arduino-core.jar/processing/app/Platform.class) with a hex editor, and saw that the bytes at offset 6 & 7 are 0x0034 (=Java SE 8, according to wikipedia), confirming that they have been compiled with Java 8?

If you cannot seem to reproduce the bug yourself, I can look around and try to see what's going. Any pointers welcome!

gkatev avatar Aug 17 '20 15:08 gkatev

Well then I have to ask @facchinm again :-D

NicoHood avatar Aug 18 '20 12:08 NicoHood

Problem with arch (besides the possible problems on java>8) is based on the shell runner /usr/bin/arduino:

#!/bin/sh
export PATH=/usr/lib/jvm/default-runtime/bin/:"$PATH"
exec /usr/share/arduino/arduino "$@"

PATH is wrong from different viewpoints:

  • if no default runtime is defined
  • if a default runtime is defined that is incompatible with arduino code (see above)
  • if the default runtime is java-8-openjdk because of the error that started this thread

If I understood Nico correctly, arduino is built for Arch. So I would make the java that is used for building a hard requirement and explicitly use it here for the PATH.

I think it only runs with jdk8 if it is build with jdk8, because of an incompatible change for ByteBuffer.flip() (from hazelcast#14214):

JDK9 breaks compatibility with older versions for ByteBuffer.flip() by returning ByteBuffer instead of Buffer

kolewu avatar Oct 20 '20 21:10 kolewu

This worked for me on Arch Linux: sudo archlinux-java set java-14-openjdk but only after I then reinstalled the arduino package.

StarsoftAnalysis avatar Oct 31 '20 15:10 StarsoftAnalysis

This worked for me on Arch Linux: sudo archlinux-java set java-14-openjdk but only after I then reinstalled the arduino package.

I instead used java-11-openjdk and it worked too

Haradai avatar Nov 01 '20 11:11 Haradai

Problem with arch (besides the possible problems on java>8) is based on the shell runner /usr/bin/arduino:

#!/bin/sh
export PATH=/usr/lib/jvm/default-runtime/bin/:"$PATH"
exec /usr/share/arduino/arduino "$@"

PATH is wrong from different viewpoints:

  • if no default runtime is defined
  • if a default runtime is defined that is incompatible with arduino code (see above)
  • if the default runtime is java-8-openjdk because of the error that started this thread

If I understood Nico correctly, arduino is built for Arch. So I would make the java that is used for building a hard requirement and explicitly use it here for the PATH.

I think it only runs with jdk8 if it is build with jdk8, because of an incompatible change for ByteBuffer.flip() (from hazelcast#14214):

JDK9 breaks compatibility with older versions for ByteBuffer.flip() by returning ByteBuffer instead of Buffer

It happens because of this.

My default is Java 8 for other programs but arduino requires newer version because of this error and when I tried to launch arduino following this it still uses default Java version because /usr/bin/arduino adds default one to the front of the PATH.

I cannot come up with an idea to solve the problem but I think the problem lies in /usr/bin/arduino.

berkersal avatar Mar 21 '21 01:03 berkersal