Haiku on Java

Blog post by yourpalal on Thu, 2012-03-01 22:36

As part of my work at the University of Auckland (which is drawing to a close soon) I have been looking at Java on Haiku. There is already some support for Java on Haiku. By running installoptionalpackage DevelopmentJava you can get the JamVM java virtual machine, GNU Classpath (including gjar, gjavah and maybe some other utilities), and ecj (the Eclipse Java compiler, which is written in Java). This is enough to run and compile basic Java apps. For instance, the popular Java buildtool Ant can run on Haiku.

Anyway, the reason I was investigating this is because the University of Auckland has a big Java library (a database, actually) that has been developed here, and Christof and Gerald (my supervisors) want to bring it to Haiku. To do this, we require the ability to write code that integrates with both Java libraries, and the Haiku API. I looked at various solutions:

  • gcj compiles Java to C++, but seemed like too big of a port
  • Java Native Interface (JNI) allows for writing C++ code that hosts a JavaVM and calls Java code, but is tedious to write.
  • SWIG can generate Java bindings to C++ libraries, even allowing Java classes to inherit from, and override virtual methods in, C++ classes.

SWIG is a mature solution that handles writing lots of JNI code, and the input files are pretty much just C++ headers, so it doesn't require too much tedious work. Given these points, I decided to go with SWIG, and I have put the results (so far) on Github.

Currently, I have two tests, one that runs a custom handler on the BApplication thread (also works running it on an extra BLooper) and listens for messages. The other test makes a very simple GUI using the Haiku API. Check out the result of this test in the image on the right. That's a BWindow with a BButton and a BGroupLayout and a BStringView, all created with Java code!!!

There are still some issues, but I'm really excited about the progress that I've made already, and I'm very impressed with SWIG! As I said in the README on Github, having Java bindings also opens up the door for any JVM language to use the Haiku API, for instance Jython, JRuby, and Scala to name a few. If you're interested in contributing, head over to github and fork the project. If you just want to try it out, it's really easy to do that, there are instructions on github in the README.

Comments

Re: Haiku on Java

That is pretty cool.. what are the chances of getting AWT,SWT and Swing working *again*? I guess that is your goal if you are going to run software already written for the defacto java libraries.

I assume you are just generating direct bindings to Haiku's API right?

Re: Haiku on Java

At the moment AWT/Swing/SWT aren't part of my goals no. The main goal is to be able to make Haiku apps in Java, potentially using large 'business logic' libraries already written in Java. Yes, the bindings are generated directly for Haiku's API (see the source in the screenshot, where a BWindow etc. is instantiated).

Re: Haiku on Java

Nice ;-)

Can Haiku (now?) run the android SDK ? and can i develop under haiku for android?

stargater

Re: Haiku on Java

You might be able to run some parts of it (the command line utilities) but I doubt the device emulator would work, and Eclipse isn't available on Haiku, and won't be until someone implements the Java AWT lib (I believe).

Re: Haiku on Java

Eclipse uses SWT, which is not at all related to AWT.

Re: Haiku on Java

I seriously doubt that, as this is still just JamVM, if I understand the post correctly. I didn't try it though. The emulator is based on qemu, AFAIK. The other tools, like adb, should be open source.

Re: Haiku on Java

Let see if I've understood well is this a way for write Haiku Application using Java instead of C++, right?

It's not a classic JAVA application... if I've understood correctly.

I can't put that .jar let me say in Windows and obtaining a running application, right? You need all the Haiku API (or at least libhaiku-onJava.so), so we've a "jar" that runs in Haiku only, right?

A little strange to use JAVA in this way, IMHO...

If I'm wrong and it's "Haiku on Java" (that's Haiku re-written in JAVA and it runs on Windows/Linux/whatever!) for real... well I'm shocked, men!

But it's an interesting project however: someone can't like use C/C++ to develop Haiku application and think JAVA is better... well it's served, now!

... and possibly Jypthon, JRuby and so on...

Re: Haiku on Java

Yeah, that's right, these apps aren't portable or cross-platform. It's a binding of the Haiku C++ API to Java, for people who don't want to write their app in C++ (for instance, they have a big Java library they want to use).

Re: Haiku on Java

You should check avian, it's a very lightweight java VM, with ahead of time compilation, and has a BSD like license, not GPL as OpenJDK has

Re: Haiku on Java

If I've read correctly avian needs JDK to be installed to be build... so how to do if we need to create a JDK? It' s a loop unsolvable I suppose...

JamVM instead is written in C so more fast to have... I don't know if you could write native JAVA GUI application (AWT/SWING, not a Haiku's JApllication as demonstrated).

Re: Haiku on Java

No, it doesn't need JDK. Avian has an own, minimal class library, which is licensed freely as a BSD code, but it can work with the more complete openjdk class library. And of course the Avian VM is written in C. JamVM is GPL, so Avian has a clear advantage here, as it is doesn't require you to license your code as GPL. And it has a simple AOT support, so you can compile your code to native code.

Re: Haiku on Java

I looked at the wikipedia page, and avian sounds interesting. Jamvm doesn't JIT compile, so avian could be a step up in performace.

Re: Haiku on Java

The problem is, if I've understood correctly, Avian needs an installed Java SDK installed (is partly written in Java?) and I fear JamVM is not enough... we need the Sun official one?

Let see here:
http://oss.readytalk.com/avian/building.html

What do you think?

I hope to be wrong, this, in particular, is very interesting:
http://oss.readytalk.com/avian/examples.html

JAVA applications with the VM embedded in the "executable"!

I've tried them on WinXP, they worked fast as hell!!!

You could release your hybrid Haiku/Java (JHaiku?) application using the University of Auckland Java libraries as it was a native binary in the end, spectacular, right?

Re: Haiku on Java

That does look pretty cool, but again, these are just Java apps using the Haiku API, so there's no need really to have them as a single, bundled executable file. This is really just straight up Java with bindings for the Haiku API, so we also don't need to name it JHaiku. These bindings are much like, say, Java bindings for SDL or Qt.

Re: Haiku on Java

Yeah but Java/Qt or Java/SDL could be considered portable, at least if in your system we've the Qt or SDL libraries compiled (and if done in the right way it searches the so/dll automatically in transparent manner for the point of view)... Java remains itself compile one run everywhere...

This more a Java dialect (it cannot compile in Linux as you have not Haiku "runtime" there), so the "JHaiku" name could be more correct, I'd say... but it's only a point
of view :-)

Re: Haiku on Java

I don't see, why you call gcj, JNI and SWIG three different options for your problem.

  • Gcj is a Java compiler that compiles to native code (NOT to C++!) instead of JVM byte code. Gcj does not magically translate your GUI code to some native toolkit.
  • JNI is part of the Java specification and allows developers to use Java code from C/C++ and vice versa.
  • SWIG is a JNI generation tool that helps you to … well, generate JNI bindings for C/C++ APIs.

These are three totally different tools. However, it's nice to see some progress in Java on Haiku.

Re: Haiku on Java

Yup, you're right, gcj, JNI and SWIG are three different (but related) things, and they are three possible solutions to the problem I'm solving (using a large Java library alongside the Haiku api). These aren't the only solutions; for instance, another possibility is serializing everything into a data exchange format and using pipes.

GCJ is actually a very cool solution, and the CNI (Compiled Native Interface) allows you to interface with Java classes as if they were C++ classes. In fact, GCJ uses the same object representation as G++ (since they both use the same backend), so in a way, the Java code is being compiled to C++! You get C++ headers and object files (as much as an object file can be C++), but not C++ source code. However, getting GCJ to work looked to be a bit of a struggle, especially since it involves both runtime and compiletime components (eg. you need a garbage collector for Java). No, GCJ is not a magical solution that automates everything and creates unicorns and pixie dust, but it would let me write a native Haiku C++ app that uses a large, already written Java library, which was my goal.

Your descriptions of JNI and SWIG are accurate. Of course, because I could use JNI directly to solve my problem, I could use SWIG to solve it as well. However, there is certainly a difference between my plan of writing JNI code using the JNI invocation API, where the C++ code is driving the application, and using SWIG bindings to expose the Haiku API to Java, thereby creating apps where the Java code is in control.

Re: Haiku on Java

I ported GCJ a few months ago, and I think it was more or less working. I know you already chose SWIG, but if you're interested I could upload the GCJ patches.

Re: Haiku on Java

Cool! It would be ideal to get your patches up on haiku ports, at least in a ticket so that they'll be found if anyone else investigates GCJ.

Re: Haiku on Java

Will this be able to do stuff like OpenOffice or Minecraft or things like that? Sorry that I don't know, I have no idea how it works.

Re: Haiku on Java

OpenOffice no, Minecraft could maybe be ported with a wrapper around BGLView, but without hardware accel, it would probably eat your processor for lunch (even though the graphics are pretty basic).

Re: Haiku on Java

Guys,

Sorry for reviving this thread, but has anyone successfully compiled Ant on Haiku? If so, where can I find it?

Best regards,