Lately, I’ve had to debug JNI code used by my Java Web Start application. Some of the issues I’ve dealt with did not show up until the code was combined and used in the resulting application. The following was how I used GDB to debug the code.
First, start the application as usual. I was debugging a java web start application, so my command will be slightly different, but the debugging procedure will work regardless of what java program you are running. At the command line, you can type the following to launch the jnlp file:
javaws < location of jnlp file >
The jnlp file can be local or a remote file, just as long as it references and loads the code you are debugging. After it loads, the process ID needs to be found. Usually, I’ll just look at all the java processes running and can figure out which one is the application I’m interetesd in:
pa aux | grep java
Once the process ID has been identified, gdb can be launched and attached to the running process:
gdb -p < PID >
This starts gdb and attaches to the java program. At this point, the execution blocks until you do something with it. Just type “continue” and run the program as usual. When or if the program causes a segmentation fault, the debugger will block; you can then do a backtrace and see exactly what file and what line caused the issue.
Programming
debug, gdb, Java, jni
At work, I’ve been working on a new Java3D project. Java3D has been great so far, and I’ve gotten a simple visualization program up and running in two weeks. Today, I had to figure out how to determine the actual width of a Text2D object. Creating a BoundingBox with the Text2D object’s getBounds() method doesn’t actually help, since it seems the dimensions are always a power of two. I needed the actual width (the height can be calculated by multiplying the font size by the the rectangle scale factor), but the bounds returned didn’t give the the value I needed; it was close, but I needed to position the text to create an annotated axis.
My solution? I had to rewrite the Text2D class. I read a forum that mentioned this as a solution, but it didn’t give the code or how to do it. If you go to the Java3D website (https://java3d.dev.java.net), you can browse the source code. The Text2D class is located in the j3d-core-utils subproject. I just copied the source file, added two private variables (height and width), created accessor methods getWidth and getHeight, and stored the height and width when it is calculated in the setupImage method. Just change the package name and you can use the new class the same way the regular Text2D class is used.
What I don’t understand is, why didn’t Sun or the community in general do this? It’s a really simple fix. How do people even use the class if they can’t accurately determine how much space the actual text uses? I’ll post the code (with the simple modifications) tomorrow.
Text2D.java
Programming
Bug, Java, Java3D, Programming