Program won't exit when reading in gif?!?

Hi,
I have written a class (see code below) that has a single method to read in two gifs, superimpose them, add text and then write the result to a jpeg file.
I test this in a simple main method (again see below). javac test.java, java test
The problem is that the program will not terminate. The final System.out.println() command is displayed in the terminal. It has to be halted with ctrl+c.
I have traced the problem to the image being read in! If I comment out the images being read in - in the MapMaker constructor (also commenting out where the images are used) - the program terminates correctly.
There are no exceptions flying around either!
Any ideas? I guess some system resource isn't being cleared, but don't know how to do this!
I can't use System.exit() because this class will be used in a .jsp page that will use the jpeg created!
Thanks in advance
Matt
MapMaker.java:
import java.awt.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.geom.AffineTransform;
import java.io.*;
import javax.swing.*;
import java.awt.MediaTracker;
import java.awt.Canvas;
public class MapMaker extends Canvas implements java.io.Serializable{
private Image arrow;
// Read in the 2 .gif files of the arrow and the map
public MapMaker() {
// When commenting out these lines, the program works fine
arrow = Toolkit.getDefaultToolkit().getImage ("arrow2.gif");
try {
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(arrow, 0);
tracker.waitForID(0);
} catch (InterruptedException e) {
System.out.println("Interrupted exception caught ");
e.printStackTrace();
// createMap generates a .jpg file fo the map with an
// arrow and wind direction
public void createMap() {
// Create a new Buffered image and associated Graphics 2D object
// Draw to the Graphics 2D object
BufferedImage bi = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
Graphics2D big = bi.createGraphics();
// Set the rendering hints
big.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
big.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
big.setBackground(Color.white);
big.clearRect(0, 0, 200, 200);
// Here I would add the images to big
// Add text
big.setFont(new Font("Dialog", Font.BOLD, 12));
big.setColor(Color.green);
big.drawString("12", 138, 128);
try {
// write the buffered image to a jpg file
File file = new File("test.jpg");
FileOutputStream out = new FileOutputStream(file);
     // encodes bi as a JPEG data stream
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(bi);
out.close();
} catch (Exception ex) {
System.out.println("Failure to write JPEG file");
return;
} // createMap
// This tests the above class
import MapMaker;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
public class test {
public static final void main( String args[] ) {
try{
System.out.println("Making new MapMaker object");
MapMaker mapMaker = new MapMaker();
System.out.println("Making new map");
mapMaker.createMap();
System.out.println("Finished making the new map");
catch (Exception e){
System.out.println("Top level exceptions\n");
e.printStackTrace();
System.out.println("All the way");
return;
}

If anyones interested. It turns out that this is a feature!
When windows resources (i.e. reading in an image) are used. Java programs won't exit automatically.
They have to be closed with System.exit()

Similar Messages

  • My Program Won't Print when its supposed to

    I've been trying to get a program that will automatically print txt files just by running it. Here's what I've come up with so far:
    import java.io.*;
    import javax.print.*;
    import javax.print.attribute.*;
    import javax.print.attribute.standard.*;
    import javax.print.event.*;
    import java.awt.print.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.net.*;
    import java.*;
    import javax.*;
    public class PrintTests8
    public static void main(String[] args)
    try
    // Open the text file
    InputStream is = new BufferedInputStream(new FileInputStream("test.txt"));
    // Find the default service
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();
    // Create the print job
    DocPrintJob job = service.createPrintJob();
    Doc doc = new SimpleDoc(is, flavor, null);
    // Print it
    job.print(doc, null);
    // It is now safe to close the input stream
    is.close();
    catch (PrintException e)
    e.printStackTrace();
    catch (IOException e)
    e.printStackTrace();
    }This will print out the file, but the problem is that it will only print it when another job, printed using in the normal, Ctrl+P type way, is printed. I hope that I can get help from you guys on this.

    This error comes up:
    sun.print.PrintJobFlavorException: invalid flavor
    at sun.print.Win32PrintJob.print(Win32PrintJob.java:327)
    at PrintTests8.main(PrintTests8.java:42)
    And sorry for being impatient.

  • MAIL PROGRAM WON'T OPEN when click on email link

    When I click on an email address when on the web, nothing happens.
    Something trys to open, the general "mail" icon pops up briefly in the dock, but fails.
    I would ideally like my gmail to open, but cannot find a way to set this up in Safari.
    Anyone know what to do?
    Thanks!

    From the Safari menu bar, select
              Safari ▹ Preferences... ▹ Extensions
    Turn all extensions OFF and test. If the problem is resolved, turn extensions back ON and then disable them one or a few at a time until you find the culprit.

  • Mail program won't open when clicking link in safari

    When I click on a "contact us" mail to link in safari, a new tab opens with top sites.Mail application never opens. In mail preferences , Mail is set to default. I am using maverick 10.9.2 and safari 7.0.3. It used to work.
    Thanks!!!!
    Shelley

    From the Safari menu bar, select
              Safari ▹ Preferences... ▹ Extensions
    Turn all extensions OFF and test. If the problem is resolved, turn extensions back ON and then disable them one or a few at a time until you find the culprit.

  • Windows 7 Backup and Restore Program won't respond when I click on Back up Now or Change Settings.

    The system rebooted when I was doing a backup to NAS. After it came back up it said I had 'canceled' the backup but now it won't respond to Back up Now or Change settings. It does respond to other options.

    Hi,
    You can attempt to run "sdclt.exe /configure" to start windows backup.
    Thanks :)
    Andy Altmann
    TechNet Community Support

  • Have a  problem with Lightroom 5.4.  Since the program crashed yesterday it won't launch, it comes up with the message "Lightroom encountered an error when reading its preview cache and needs to quit".  "  Lightroom will attempt to fix this problem net ti

    Have a  problem with Lightroom 5.4.  Since the program crashed yesterday it won't launch, it comes up with the message "Lightroom encountered an error when reading its preview cache and needs to quit".  "  Lightroom will attempt to fix this problem next time it launches".  Except that it doesn't, I keep getting the same message and the program closes.  Does anyone know what I  can do to repair it?  Can't back up, can't do anything.

    There are dozens of threads in this forum that describe the fix

  • I've tried to troubleshoot downloading Firefox but it won't open on Windows XP. The Firefox Update box with the hour glass symbol runs for hours. Then I have trouble even shutting down. When I click on Firefox, no window, program won't quit, etc....

    Windows XP. I used to use Firefox and it worked okay. I'm not sure why I downloaded a new version but it won't open. The window that says "Firefox Update" continues to try to load. Hour glass symbol, for hours. If I try to click on the Desktop symbol, Nothing. Then when I try to shut down, it's slow, programs won't quit, or it freezes. I've removed Firefox completely and tried downloading again with the same results. Even with the latest version. Shit. Whassup?

    Hi zenithtwc, to test whether this is a settings issue, could you do a three-minute experiment?
    '''Create a new Firefox profile'''
    A new profile will have your system-installed plugins (e.g., Flash) and extensions (e.g., security suite toolbars), but no themes, other extensions, or other customizations. It also should have completely fresh settings databases and a fresh cache folder.
    Exit Firefox and start up in the Profile Manager using Start > search box (or Run):
    firefox.exe -P
    ''Don't delete anything here.'' Any time you want to switch profiles, exit Firefox and return to this dialog.
    Click the Create Profile button, assign a name like Test731, and skip the option to change the folder location. Then start Firefox in the new profile you created.
    Can it reach the internet?
    When returning to the Profile Manager, you might be tempted to use the Delete Profile button. But... it's a bit too easy to accidentally delete your "real" profile, so I recommend resisting the temptation. If you do want to clean up later, I suggest making a backup of all your profiles first in case something were to go wrong.

  • Just downloaded Lion and now iMovie won't respond when I try to open it. Even tried deleting the program (since it was an older version) and bought it new from the app store, but still won't respond.

    Just downloaded Lion and now iMovie won't respond when I try to open it. Even tried deleting the program (since it was an older version) and bought it new from the app store, but still won't respond.

    Thanks for this mha007 - I can now open FF with a new profile. Can I copy my settings from the old profile or will this bring over the same problem, maybe a corrupt file. If it would bring the same problem, is there any way I can check which file is corrupt, apart from taking them over one by one?

  • My Itunes won't open when I click on the program.

    My Itunes won't open when I click on the icon/program. It will flash like it's going to open on the bottom bar on windows 7 but then doesn't do anything, I have tried deleting it and redownloading it, however this still hasn't done anything. What can i do??? Thanks.

    Can you right mouse click iTunes icon and choose "Run as Administrator"?
    If this fails:
    Try SCAN your Windows system files for possible corruption:
    Go to command prompt (START/ALL PROGRAMS/ACCESSORIES right mouse click "command prompt" and choose "Run as Administrator"
    type in
    sfc /scannow
    let Windows fix any system files that need to be repaired.
    Restart the computer if it did actually repair any files.

  • SunStudio debugger session exits when program finished

    I'm running SunStudio 12 on a Ultra 25 with one or more projects opened.
    Usually the debugging session was still available when the program execution ended.
    Now, for some reason, the debugging session also exit when finishing the program.
    I have to start a new dbx session after that.
    A colleague of mine can debug the same executable,
    but his debugging session is still available when finishing program execution.
    I'm not aware of that I changed something.
    Also I couldn't find any option in SunStudio that I might set wrong.
    How do I come back to the previous behavior?
    Regards
    Peter

    To control this "feature" ....
    From the main menu choose Tools->Options
    Select the Miscellaneous category
    Turn Debugging Options
    Select the Session Startup panel.
    Check No for "Finish Debug Session When Program Exits.
    NOTE: This property is not available in SunStudio 12 FCS, but
    is available either in the later MultiLingual release and/or patches.
    (I.e. what you colleague has :-)

  • HT2729 When I hit camera then try 'Video' the program won't start just photos. What's wrong?

    When I hit camera then try 'Video' the program won't start just photos. What's wrong?

    Sounds to me like you are trying to tap on the word VIDEO.
    To change to video you don't tap on the word VIDEO. Instead, you drag the centre of the screen down. That will change the option from PHOTO to VIDEO.
    Of course, to go back to PHOTO or to SQUARE you drag the centre of the screen up.

  • Firefox won't open/connect tp the internet. Explorer opens and connects. Windows firewall turned off. No other antivirus program running. Happened when I downloaded new Firefox update.

    Firefox won't open/connect tp the internet. Explorer opens and connects. Windows firewall turned off. No other antivirus program running. Happened when I downloaded new Firefox update.

    I had the same problem when i dl-ed a new anti-virus softward, but what I did help me reconnect Firefox again!
    Here is what I did,
    Go to open firefox browser-->option-->advanced-->network-->settings-->and change it to auto-detect proxy settings for this network!
    It worked for you, hope it will work for you too =)!
    Cheers

  • I keep receiving this error every time I open LR, "Lightroom encountered and error when reading from its preview cache and needs to quit" and the app won't open. It said LR would try and fix this error next time I opened it but the error remains. I tried

    I keep receiving this error every time I open LR, "Lightroom encountered and error when reading from its preview cache and needs to quit" and the app won't open. It said LR would try and fix this error next time I opened it but the error remains. I tried uninstalled and installing the app and the error remains. I have windows 8. Can anyone please help???

    Use Windows Explorer to open the folder containing your catalog. The normal location is Pictures\Lightroom. Locate a folder with the extension .lrdata and delete that folder. Then you should be able to start Lightroom again, and Lightroom will begin building and new previews folder. Do not delete anything else.

  • When opening the program LR 5 encounters an error when reading from it's preview cache.

    When opening the program LR 5 encounters an error when reading from it's preview cache. It says it will correct the problem when it re opens. Why does this happen and how do I correct it?

    It means there is an issue with the preview cache.
    If it persists after closing and re-opening Lightroom then you will need to delete the preview folder.
    There is no permanent harm and Lightroom will regenerate the previews on an asneeded basis or the whole image collection if you ask it.
    The previews folder exists in the Lightroom folder and has the name "Your catalog name PREVIEW.LRDATA".
    Make sure it is the the correct folder before deleting it.
    Tony Jay

  • I am getting an error when reading the preview cache and the program shuts down.

    I had to reload Lightroom on a new computer and after I did that it gives me an error when reading from its preview cache and then it shuts the program down.  How can I fix this problem?

    Delete the preview cache ... it is a folder whose name ends with Previews.LRDATA in the same folder as your catalog file.

Maybe you are looking for

  • Tnventory Management Weekly snapshot 0IC_C03

    Hi, We are trying to instal the stocks Flow, with the 0IC_C03 cube. Our Business Content (Release 703 Level 8) has the new Infosources (2LIS_03_BF_TR, 2LIS_03_BX_TR and 2LIS_03_UM_TR) and the related transformations from the Infosources to the cube.

  • Photos from laptop to mac

    *Hello* **I recently got an imac. I am trying to put my photos from my old laptop to the imac. I can't use the usb or the cd dvd drive on the laptop to transfer my photos. I have tried emailing the photos. But my email only lets me email 4 photos at

  • Standard Bapis or FMs

    Hi, Can you plz update me on the standard Bapis or Function Modules for Purchase Order, Sales Order, Goods Receipt and Invoice?? Thanks in Advance, Srinu.

  • Reg BDC

    Hi,     In BDC, Call transaction method how to store error in bdcmsgcoll table. Thank you,

  • Usb installaton not booting on "phoneix awardbios"

    I have wrote the iso to USB using "dd bs=4M if=archlinux-2014.10.01-dual.iso of=/dev/sdb". On a computer with "phoneix awardbios" the usb does not boot instead it shows "isolinux.bin missing or corrupt" I tried on a laptop, it booted and shows the ar