Credit: Nongnuch_L / Shutterstock Java Toolbox: ‘Programming Java threads in the real world,’ by Allen Holub Read Allen Holub helps reader with synchronization of files across JVMs Allen, I’ve read some of your articles on Java multithreading, but I have not seen an issue I came up against a few months ago. When writing a Java simulator for a Web-based e-commerce system, I needed to provide synchronization of a file across several JVMs. Have you written about this? I assume I would need to write a JNI wrapper to a native program that implemented operating system semaphore (for me, it would be for both NT and Solaris). Any suggestions, comments? Bill Olmstead Bill, I don’t think you should use JNI — it’s not platform-independent, it’s hard to write and maintain, and more to the point, there are other ways for JVMs to communicate. If all you need is a simple resource lock, the easiest way is to use the existence of a file to signify locking. Create the file to lock the resource. Destroy the file when you’re done with the resource. Test for existence to acquire the resource. (Many Unix systems support a blocking open for write access to a nonexistent file expressly for this purpose, but the technique can work anywhere.) Remember, you’ll need to use a spin lock around the acquisition code. A better alternative is to have the JVMs communicate with each other via any of the standard mechanisms: CORBA, RMI, a simple socket, and so on. I prefer this approach since it’s cleaner. In other words, write a little Java program to manage the lock, and then have your other processes talk to the lock program using RMI. Allen Holub ‘Java Tip 71: Use dynamic messaging in Java,’ by Alex Blewitt Read Can DWIS.java grab Java classes off the Web? Alex, I read “Java Tip 71: Use dynamic messaging in Java,” with great interest — it’s really a good one. I wonder if the example you showed (DWIS.java) can be extended so that DWIS fetches Java classes off the Web and executes arbitrary methods in them. Could two such classes possibly communicate with each other? Sonam Chauhan Sonam, You pose an interesting question here: that classes can be downloaded from anywhere and executed, in conjunction with this article. In fact, it happens all the time with the AppletClassLoader, which loads classes from anywhere on the Web (via a Web server). I think a useful follow-up tip would include a demo program that could load a class from anywhere (using the ClassLoader) so that you could remotely run applications in the same way that you can remotely run applets. Would such an article be useful to you? Alex Blewitt Media Programming: ‘3D graphics programming in Java” series, by Bill Day Read Graphics and Swing Bill, I’m doing a presentation on Swing. The graphics stuff is nice and would add a little bit more spice to the boring Computer Topics class I’m taking. However, would you know any meatier Swing topics available on the Web? That is, how Swing is used by a business for business transaction, databases, and so on. I’m interested in the utility side of Swing. Dean Dean, I suggest you browse the Swing Connection to search for applications you might be interested in discussing. Find it at: https://java.sun.com/products/jfc/tsc/index.html You might also check out Sun’s Java Industry Connection site at: https://industry.java.sun.com/ This site might have some press releases of interesting Swing-based development. Thanks again for your message and good luck with the presentation. Bill Day ‘Java Tip 60: Saving bitmap files in Java,’ by Jean-Pierre Dubé Read Bitmap bugs: Reader helps Jean-Pierre Dubé with his code Jean-Pierre, I may have discovered a bug or two in your Java code for writing out a bmp file. Now, with a few changes to your code, the output images are good (that is, can be opened by several image viewers, in addition to Paint). I thought you might like to know what I did. I found that a bmp file produced using your code can be opened and displayed in the Windows program Paint, but not in the viewer Lview. I also discovered the bmp file could not be opened in xv, a Unix viewer (actually, xv complained about the file being truncated, then “winged it” and produced an image). In looking at your code I think I found the problems. I was trying an image that was 320 x 240 (that is, 320/4 = 80, with no remainder: no need to pad the lines in the bmp file). In looking at the output file, there were a few problems. For one thing, the file header’s size for the file (bfSize) was too large — the program thought padding was necessary. Second, the biSizeImage was also too large (again, the padding problem). Third, the output file was 3 bytes shorter than it should have been. The file should have been 320 x 240 x 3 + 54 = 230,454 bytes, where the 54 bytes cover the header information. Instead the file was 230,451 bytes in size. Apparently the output file was missing the 3 bytes covering the last image pixel. I made a few minor changes to the BMPFile class and now it seems to work. I can output my 320 x 240 image as a bmp file and open it in Paint, Lview, or xv. I also tried an 765 x 510 image (since 765/4 = 191.25, and padding is necessary). The resulting bmp file was also readable in Paint, Lview, or xv. Here are the changes I made: In convertImage(), I replaced the line: pad = (4 - ((parWidth * 3) % 4)) * parHeight; With the following if/then block: int test = (parWidth*3) % 4;if (test == 0) { pad = 0;}else if (test == 1) { pad = 3 * parHeight;}else if (test == 2) { pad = 2 * parHeight;}else { // i.e., test = 3 pad = 1 * parHeight;} This is essentially the same sort of fix you made in writeBitmap(), where you used the code: if (pad == 4) // <==== Bug correction pad = 0; // <==== Bug correction In writeBitmap(), I replaced the line: size = (biWidth * biHeight)- 1; With the line: size = (biWidth * biHeight);**** John Simonetti John, Thanks for the fix, I will included it in my code. I’m sorry for all the extra work you had to go through to get my code to work. Next time I will test my examples with more viewers. I guess all this hard work has made you a bitmap expert! Jeane-Pierre Dubé ‘Java Tip 61: Cut, copy, and paste in Java,’ by Jeane-Pierre Dubé Read Trusted applets to help copy and paste Jeane-Pierre, I am writing an applet, and I need to be able to copy and paste from the system’s clipboard. When I compiled and ran listing1, I got the following error: sun.applet.AppletSecurityException: checksystemclipboardaccess at sun.applet.AppletSecurity.checkSystemClipboardAccess(AppletSecurity.java:739) at sun.awt.motif.MToolkit.getSystemClipboard(MToolkit.java:271) at Example1.init(Example1.java:22) at sun.applet.AppletPanel.run(AppletPanel.java:281) at java.lang.Thread.run(Thread.java) I could really use some help to resolve this. Shawn Sommer Shawn, To solve your problem you need to create a trusted applet. Here are the steps required: Create a jar file for your applet: jar -cvf NewApplet.jar *.class Create the signer in the Javakey database: javakey -cs NewSigner true Generate the key pair and the X509 certificate: javakey -gk NewSigner DSA 512 MyKey.public MyKey.private Import the key pair in the javakey database: javakey -ic NewSigner NewSigner.x509 Sign your applet: javakey -gs NewApplet.sign NewApplet.jar From step 5 you will obtain a new jar (NewApplet.jar.sig). Modify your HTML file to use this archive You now have a trusted applet that can bypass the sandbox and access all the resources on the client machine. I hope this will help you. Jeane-Pierre Dubé Java