Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6710022

Current working directory is changed by java.awt.print.PrinterJob.print()

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 6
    • Fix Version/s: None
    • Component/s: client-libs
    • Labels:
    • Subcomponent:
      2d
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      FULL PRODUCT VERSION :
      java version "1.6.0_10-beta"
      Java(TM) SE Runtime Environment (build 1.6.0_10-beta-b24)
      Java HotSpot(TM) Client VM (build 11.0-b12, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Microsoft Office Document Imaging and Foxit PDF Creator are installed so that there are "Microsoft Office Document Image Writer" and "Foxit PDF Printer" in Printers and Faxes window.

      A DESCRIPTION OF THE PROBLEM :
      java.awt.print.PrinterJob.print() is used to take printing task.

      In print dialog, if you select "Microsoft Office Document Image Writer" or "Foxit PDF Printer" as printer name and press OK button, then "Save as" dialog window(if you print to Microsoft Office Document Image Writer) or "Print to PDF Document - Foxit PDF Printer" dialog window(if you print to Foxit PDF Printer) is shown to select file path and fill file name. After you press the Save button, print task is done. At this time, the current working directory is changed to the path selected in the dialog.

      The impact is: after print job is done, java.io.File operation with relative path will cracked because the current working directory has been changed.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Compile the test case source code in D:\test
              Expected result:
                     a. there are two .class files in D:\test : HelloWorldPrinter.class and HelloWorldPrinter$1.class.
      2. Open Command Prompt window and change dir to D:\test.
      3. Run the HelloWorldPrinter program by command: java HelloWorldPrinter
              Expected result:
                     a. the Hello World Printer window launched.
      4. Press Print Hello World button
              Expected result:
                     a. Print dialog launched.
      5. Select "Microsoft Office Document Image Writer" or "Foxit PDF Printer" as printer name and press OK button(if there is none of these printers in the combobox, install at least one of them).
              Expected result:
                     a. "Save as" dialog window(if you print to Microsoft Office Document Image Writer) or "Print to PDF Document - Foxit PDF Printer" dialog window(if you print to Foxit PDF Printer) launched.
                      b. in Command Prompt window, the user.dir and contents of path: new java.io.File(".") are listed.
      6. Select file path(D:/newdir) and fill file name and press Save button.
              Expected result:
                     a. printed file is shown.
                     b. in Command Prompt window, the user.dir and contents of path: new java.io.File(".") are listed.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      1. In step 5, contents of path: new java.io.File(".") should be contents of path: D:\temp.
      2. In step 6, contents of path: new java.io.File(".") should be contents of path: D:\temp too.
      ACTUAL -
      1. In step 5, contents of path: new java.io.File(".") are contents of path: D:\temp.
      2. In step 6, contents of path: new java.io.File(".") are contents of path: D:\newdir.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
       * Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved.
       *
       * Redistribution and use in source and binary forms, with or without
       * modification, are permitted provided that the following conditions
       * are met:
       *
       * - Redistributions of source code must retain the above copyright
       * notice, this list of conditions and the following disclaimer.
       *
       * - Redistributions in binary form must reproduce the above copyright
       * notice, this list of conditions and the following disclaimer in the
       * documentation and/or other materials provided with the distribution.
       *
       * - Neither the name of Sun Microsystems nor the names of its
       * contributors may be used to endorse or promote products derived
       * from this software without specific prior written permission.
       *
       * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
       * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
       * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
       * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
       * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
       * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
       * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       */

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import java.awt.print.*;

      public class HelloWorldPrinter implements Printable, ActionListener {

        public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

          if (page > 0) { /* We have only one page, and 'page' is zero-based */
            return NO_SUCH_PAGE;
          }

          /*
           * User (0,0) is typically outside the imageable area, so we must translate
           * by the X and Y values in the PageFormat to avoid clipping
           */
          Graphics2D g2d = (Graphics2D) g;
          g2d.translate(pf.getImageableX(), pf.getImageableY());

          /* Now we perform our rendering */
          g.drawString("Hello world!", 100, 100);

          /* tell the caller that this page is part of the printed document */
          return PAGE_EXISTS;
        }

        public void actionPerformed(ActionEvent e) {
          PrinterJob job = PrinterJob.getPrinterJob();
          job.setPrintable(this);
          boolean ok = job.printDialog();
          //In "Print" dialog window, select "Microsoft Office Document Image Writer" or
          //"Foxit PDF Printer" as printer name, and then press OK button.
          //If you have none of these printers, please install at least one of them.
          if (ok) {
            try {
              System.out.println("The user.dir before job.print() is: " + System.getProperty("user.dir"));
              System.out.println("The contents of current working directory are: \n");
              String contents = "";
              //Use new java.io.File(".") to check current working directory
              String[] contentsOfCurrentWorkingDirectory = new java.io.File(".")
                  .list();
              for (int i = 0; i < contentsOfCurrentWorkingDirectory.length; i++) {
                contents += contentsOfCurrentWorkingDirectory[i];
                contents += ", ";
              }
              System.out.println(contents);
              System.out
                  .println("\n====================================================\n");

              //In "Save as" dialog window(if you print to Microsoft Office Document Image Writer)or
              //"Print to PDF Document - Foxit PDF Printer" dialog window(if you print to Foxit PDF Printer),
              //select a path other than user.dir.
              job.print();

              System.out.println("The user.dir after job.print() is: " + System.getProperty("user.dir"));
              contents = "";
              contentsOfCurrentWorkingDirectory = new java.io.File(".").list();
              for (int i = 0; i < contentsOfCurrentWorkingDirectory.length; i++) {
                contents += contentsOfCurrentWorkingDirectory[i];
                contents += ", ";
              }
              System.out.println("The contents of current working directory are: \n");
              System.out.println(contents);
            } catch (PrinterException ex) {
              /* The job did not successfully complete */
            }
          }
        }

        public static void main(String args[]) {

          UIManager.put("swing.boldMetal", Boolean.FALSE);
          JFrame f = new JFrame("Hello World Printer");
          f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
              System.exit(0);
            }
          });
          JButton printButton = new JButton("Print Hello World");
          printButton.addActionListener(new HelloWorldPrinter());
          f.add("Center", printButton);
          f.pack();
          f.setVisible(true);
        }
      }

      ---------- END SOURCE ----------

        Activity

        igor Igor Nekrestyanov (Inactive) created issue -

          People

          • Assignee:
            jgodinez Jennifer Godinez (Inactive)
            Reporter:
            igor Igor Nekrestyanov (Inactive)
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Imported:
              Indexed: