Invoking HtmlParser from C++
On Friday, I said Next task is to repackage the htmlparser as a library, and to reimplement the HTML2XML tool itself in C++. This is now done. In the process, I also converted a SystemErrErrorHandler
class into C++ so that I could be sure that I was able to do both Java=>C++ and C++=>Java calls.
A few things learned along the way:
- Installing libjaxp1.3-java-gcj doesn’t give you headers, but you can generate these yourself using
gcjh
. - At least on Ubuntu, there is a bug that prevents gcjh from working. The workaround is to add
/usr/share/java/libgcj.jar
to the classpath. - In gcj, everything inherits directly from
java.lang.Object
. This means that you can’t even pass an instance to a subclass to a method that is expecting an instance of a superclass without an explicit cast. - If you wish to implement a class in C++ that can be used from Java, there is a lot of work to be done. Much of this can be avoided if you define the class in Java, marking the methods you wish to implement in C++ as
native
, compiling the Java, and generating a header. - Constructors can’t be native methods. The workaround is to have the constructor call a private method.
- If you with to try the executable, remember to set LD_LIBRARY_PATH.