Java on Arduino

I've seen some posts saying that this is not implemented in any way, but they are old so I figured I'd inquire again in case this has changed.

I want to build a neat toolkit based on Arduino for use in AP Computer Science classrooms. It seems like a fantastic fit -- low cost, large community, and kids get to make things in the real world do cool stuff with a minimum of programming skill.

Trick is, APCS courses are in Java, and it is unrealistic to expect a teacher to ask their students to use multiple programming languages because the focus is on learning Java well enough to pass the AP exam, not to learn to program generally.

Does something which will compile Java code to an arduino hex file exist? It seems like a long shot, but it would be amazing if this existed. If not, is there a Java VM that can be run on an arduino? That seems similarly like a long shot given the limitations of the microprocessor, but might be good enough given that my arduino is running almost no other code (I put most of the auxiliary control hardware on other chips to leave more cycles free on the arduino to work with).

Does something which will compile Java code to an arduino hex file exist?

Not that I know of.
However, most of Java that does stuff, is actually in C so there is not a lot of difference.

Just lean Java at collage, and learn c++ (arduino) your self. They are similar enough for the beginner. With a beginners guide to ansi c++ and some classes in Java you will be fine as far as learning arduino go. Obviously a lot of what you will learn in Java will not be relevant to arduino. It's the program structure your interested in. There is some difference. Java has no functions just class's. You will uses a lot of functions working with the arduino. Variables are different in c++ (arduino) you have to define the type (int,float,char..) in Java you dont.
Other than that they are very similar to the beginner.

You may be in luck. There is a Java Virtual Machine for the AVR family of processors of which the ATmega328p is a member:

http://www.harbaum.org/till/nanovm/index.shtml

Looks like your Java byte code goes into EEPROM.

The source code can be downloaded from NanoVM download | SourceForge.net

I agree 100% that writing code in C or C++ or processing or whatever is minimally different than Java, but for what I'm making to be usable by a high school teacher it has to fit a need in the curriculum (which is Java).

Personally, I don't even know Java, I use C and python almost exclusively because I am always doing either embedded work or writing trivial computer software for numerical analysis (where the time it takes is not very important). I only even use C++ when someone else wrote it and I need to adapt or fix it.

But if the schools say their kids need to learn Java, I don't really have a great deal of flexibility to say that they should learn something else. I could say "use this for after school projects", but kids are so busy it seems unreasonable to expect them to do that if it is at all possible to use what they will already be familiar with.

I am excited about the java VM that uploads the bytecode. I wonder if that can be incorporated into the bootloader (or if that's even necessary). I will definitely look more into that, thanks very much for the reference.

the focus is on learning Java well enough to pass the AP exam, not to learn to program generally.

If they have to learn Java for an exam, don't show them an Arduino ( or other "small" microcontrollers ).
Java is about being hardware independent.

On the other hand, here you won't be successful if you are not aware that a char is 8 bits (and the meaning of non ASCII chars is undefined), and that all variables and stack live in 2k of RAM without garbage collection. Operating system consists of a Reset, Timers and 2 Interrupts and the Arduino IDE just adds a main() to your code.
Being aware of the limits and seeing the direct interaction with reality (electricity of 5V and very few mA, at least), that's what such a school project would teach.

The language itself is "roughly the same", the environment where and how the code is running, that's the difference to teach (or confuse).

Java is about being hardware independent.

Remind me again why the Blessed K&R developed C?

Yeah, I'm a little confused about this. I don't think about how garbage collection works when I write in C either (I'm not a very good programmer, so I don't really use memory allocation). But I do know that when I write in assembly the code is not portable. But when I write in C is generally is.

Maybe you mean that java is designed to be platform independent? Certainly you need a different compiler to write code for linux and windows despite them both operating on the same hardware.

And given that this is the case, what is the difference between running java bytecode in a JVM on an arduino versus a JVM on i386?

Granted, it might run slow as molasses if they try to do nothing but 32-bit floating arithmetic, but speed isn't necessarily all that important for a teaching tool. But perhaps I could use the new arduino setup that runs on ARM instead of AVR (or a maple). But my suspicion is that the JVM for AVR might be totally sufficient for the simple kinds of operations most likely to be desired.

I don't think about how garbage collection works when I write in C either

That's a useful mindset to have, because C doesn't do garbage collection.

Well then.

All I know is that I first programmed microcontrollers in assembly, so for me C is an amazing blessing in comparison :wink: I assume Java is minimally different.

That's right, sorry for confusing you in my previous post.

Of course it were a different challenge for a java teacher to do an excursion into microcontroller assembly language, eeprom handling and such stuff.
In that respect Java and C are "minimally different".

The point I wanted to emphasize is that programming microcontrollers is about being aware of the limits.
c and AVR c/c++ are trying to hide such limits, but Java is one abstraction layer beyond (e.g. "garbage").

For me, c is a good compromise between ease of usage, and still being aware of what's going on.

what is the difference between running java bytecode in a JVM on an arduino versus a JVM on i386

None, that's what a JVM is for. The goal of a JVM is to hide any difference. Which makes it worse as soon as the differences (limits) show up.

The more I think about it, this is also the goal of the Arduino IDE.
The first experience (blink) showing how easy it is to control real things ( LEDs ) is something that fascinates long before becoming aware of limits.

If Blink.java is less than 5 lines of code, and it's as easy to upload, go for that, even if it requires a big microcontroller to host the JVM.
If you want to show there's something else fascinating, only "minimally different" to Java, show them Arduino.