Java не может найти локальные файлы после вызова PrinterJob.getPrinterJob (). DefaultPage () - PullRequest
3 голосов
/ 08 декабря 2010

Я действительно озадачен этим вопросом.У меня проблема, когда Java не может найти файлы в локальном каталоге после вызова метода PrinterJob.getPrinterJob (). DefaultPage ();

На самом деле я свел его к следующей простой программе:

public static void main(String args[]) throws Exception
{
    File file = new File("test.txt");

    System.out.println("File: " + file.getAbsolutePath() + 
                       " exists=" + file.exists());

    PrinterJob.getPrinterJob().defaultPage();

    System.out.println("File: " + file.getAbsolutePath() + 
                       " exists=" + file.exists());
}

Что выводит следующее:

File: C:\OpenBug\test.txt exists=true
File: C:\OpenBug\test.txt exists=false

Файл Java, файл "test.txt" и файл класса находятся в одном каталоге.

Я компилирую и запускаю один Java-файл из командной строки.

Сегодня я сделал новую установку 1.6.0_22 на XP и получил те же результаты.

Что может произойтина?Загружает ли PrinterJob какой-то класс, который каким-то образом меняет мой взгляд на файловую систему?

Я добавил код в свою программу, чтобы выгрузить системную среду.Что-нибудь подозрительное здесь?

awt.toolkit = sun.awt.windows.WToolkit
file.encoding = Cp1252
file.encoding.pkg = sun.io
file.separator = \
java.awt.graphicsenv = sun.awt.Win32GraphicsEnvironment
java.awt.printerjob = sun.awt.windows.WPrinterJob
java.class.path = .
java.class.version = 50.0
java.endorsed.dirs = C:\Program Files\Java\jdk1.6.0_22\jre\lib\endorsed
java.ext.dirs = C:\Program Files\Java\jdk1.6.0_22\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
java.home = C:\Program Files\Java\jdk1.6.0_22\jre
java.io.tmpdir = C:\DOCUME~1\Andrew\LOCALS~1\Temp\
java.library.path = C:\Program Files\Java\jdk1.6.0_22\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jdk1.6.0_22\b
in;C:\Java\apache-ant-1.8.1\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;
java.runtime.name = Java(TM) SE Runtime Environment
java.runtime.version = 1.6.0_22-b04
java.specification.name = Java Platform API Specification
java.specification.vendor = Sun Microsystems Inc.
java.specification.version = 1.6
java.vendor = Sun Microsystems Inc.
java.vendor.url = http://java.sun.com/
java.vendor.url.bug = http://java.sun.com/cgi-bin/bugreport.cgi
java.version = 1.6.0_22
java.vm.info = mixed mode, sharing
java.vm.name = Java HotSpot(TM) Client VM
java.vm.specification.name = Java Virtual Machine Specification
java.vm.specification.vendor = Sun Microsystems Inc.
java.vm.specification.version = 1.0
java.vm.vendor = Sun Microsystems Inc.
java.vm.version = 17.1-b03
line.separator =

os.arch = x86
os.name = Windows XP
os.version = 5.1
path.separator = ;
sun.arch.data.model = 32
sun.boot.class.path = C:\Program Files\Java\jdk1.6.0_22\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar;C:\Program Files\Java\j
dk1.6.0_22\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\jce.jar;C:\Program File
s\Java\jdk1.6.0_22\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.6.0_22\jre\classes
sun.boot.library.path = C:\Program Files\Java\jdk1.6.0_22\jre\bin
sun.cpu.endian = little
sun.cpu.isalist = pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86
sun.desktop = windows
sun.io.unicode.encoding = UnicodeLittle
sun.java.launcher = SUN_STANDARD
sun.jnu.encoding = Cp1252
sun.management.compiler = HotSpot Client Compiler
sun.os.patch.level = Service Pack 3
user.country = US
user.dir = C:\OpenBug
user.home = C:\Documents and Settings\Andrew
user.language = en
user.name = Andrew
user.timezone =
user.variant =

Работая в подробном режиме, вы можете видеть, что следующие классы загружаются PrintJob

[Loaded java.awt.print.PrinterJob from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]
[Loaded java.awt.print.PrinterJob$1 from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]
[Loaded sun.java2d.DisposerTarget from shared objects file]
[Loaded sun.print.RasterPrinterJob from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]
[Loaded sun.awt.windows.WPrinterJob from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]
[Loaded java.awt.Toolkit from shared objects file]
[Loaded java.awt.Toolkit$3 from shared objects file]
[Loaded java.util.ResourceBundle$Control from shared objects file]
[Loaded sun.util.CoreResourceBundleControl from shared objects file]
[Loaded java.util.Arrays$ArrayList from shared objects file]
[Loaded java.util.Collections$UnmodifiableCollection from shared objects file]
[Loaded java.util.Collections$UnmodifiableList from shared objects file]
[Loaded java.util.Collections$UnmodifiableRandomAccessList from shared objects file]
[Loaded java.util.ResourceBundle from shared objects file]
[Loaded java.util.ResourceBundle$1 from shared objects file]
[Loaded java.util.ResourceBundle$RBClassLoader from shared objects file]
[Loaded java.util.ResourceBundle$RBClassLoader$1 from shared objects file]
[Loaded java.util.ResourceBundle$CacheKey from shared objects file]
[Loaded java.util.ResourceBundle$CacheKeyReference from shared objects file]
[Loaded java.util.ResourceBundle$LoaderReference from shared objects file]
[Loaded java.util.ResourceBundle$SingleFormatControl from shared objects file]
[Loaded java.util.AbstractList$Itr from shared objects file]
[Loaded java.util.ListResourceBundle from shared objects file]
[Loaded sun.awt.resources.awt from shared objects file]
[Loaded sun.security.action.LoadLibraryAction from shared objects file]
[Loaded java.awt.Toolkit$1 from shared objects file]
[Loaded java.io.FileNotFoundException from shared objects file]
[Loaded java.awt.GraphicsEnvironment from shared objects file]
[Loaded java.awt.GraphicsEnvironment$1 from shared objects file]
[Loaded java.awt.Insets from shared objects file]
[Loaded sun.awt.DebugHelper from shared objects file]
[Loaded sun.awt.NativeLibLoader from shared objects file]
[Loaded sun.awt.DebugHelperStub from shared objects file]
[Loaded java.awt.Toolkit$2 from shared objects file]
[Loaded sun.awt.WindowClosingSupport from shared objects file]
[Loaded sun.awt.WindowClosingListener from shared objects file]
[Loaded sun.awt.ComponentFactory from shared objects file]
[Loaded sun.awt.InputMethodSupport from shared objects file]
[Loaded sun.awt.SunToolkit from shared objects file]
[Loaded sun.awt.windows.WToolkit from shared objects file]
[Loaded java.util.logging.Logger from shared objects file]
[Loaded java.util.logging.Handler from shared objects file]
[Loaded java.util.logging.Level from shared objects file]
[Loaded java.util.logging.LogManager from shared objects file]
[Loaded java.util.logging.LogManager$1 from shared objects file]
[Loaded java.beans.PropertyChangeSupport from shared objects file]
[Loaded java.util.logging.LogManager$LogNode from shared objects file]
[Loaded java.util.logging.LoggingPermission from shared objects file]
[Loaded java.util.logging.LogManager$Cleaner from shared objects file]
[Loaded java.lang.ApplicationShutdownHooks from shared objects file]
[Loaded java.lang.ApplicationShutdownHooks$1 from shared objects file]
[Loaded java.lang.Shutdown from shared objects file]
[Loaded java.lang.Shutdown$Lock from shared objects file]
[Loaded java.util.IdentityHashMap from shared objects file]
[Loaded java.util.logging.LogManager$RootLogger from shared objects file]
[Loaded java.util.logging.LogManager$2 from shared objects file]
[Loaded java.util.Properties$LineReader from shared objects file]
[Loaded java.util.Hashtable$Enumerator from shared objects file]
[Loaded java.util.EventObject from shared objects file]
[Loaded java.beans.PropertyChangeEvent from shared objects file]
[Loaded java.util.logging.LogManager$3 from shared objects file]
[Loaded java.util.concurrent.locks.Condition from shared objects file]
[Loaded java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject from shared objects file]
[Loaded sun.awt.AWTAutoShutdown from shared objects file]
[Loaded sun.awt.AWTAutoShutdown$PeerMap from shared objects file]
[Loaded java.util.WeakHashMap from shared objects file]
[Loaded java.util.WeakHashMap$Entry from shared objects file]
[Loaded sun.java2d.FontSupport from shared objects file]
[Loaded java.util.EventListener from shared objects file]
[Loaded sun.awt.DisplayChangedListener from shared objects file]
[Loaded sun.java2d.SunGraphicsEnvironment from shared objects file]
[Loaded sun.awt.Win32GraphicsEnvironment from shared objects file]
[Loaded sun.java2d.SunGraphicsEnvironment$1 from shared objects file]
[Loaded java.io.FilenameFilter from shared objects file]
[Loaded sun.java2d.SunGraphicsEnvironment$TTFilter from shared objects file]
[Loaded sun.java2d.SunGraphicsEnvironment$T1Filter from shared objects file]
[Loaded sun.java2d.windows.WindowsFlags from shared objects file]
[Loaded sun.java2d.windows.WindowsFlags$1 from shared objects file]
[Loaded java.awt.Transparency from shared objects file]
[Loaded sun.java2d.Surface from shared objects file]
[Loaded sun.java2d.SurfaceData from shared objects file]
[Loaded java.lang.IllegalStateException from shared objects file]
[Loaded sun.java2d.InvalidPipeException from shared objects file]
[Loaded sun.java2d.NullSurfaceData from shared objects file]
[Loaded sun.java2d.loops.SurfaceType from shared objects file]
[Loaded sun.awt.image.PixelConverter from shared objects file]
[Loaded sun.awt.image.PixelConverter$Xrgb from shared objects file]
[Loaded sun.awt.image.PixelConverter$Argb from shared objects file]
[Loaded sun.awt.image.PixelConverter$ArgbPre from shared objects file]
[Loaded sun.awt.image.PixelConverter$Xbgr from shared objects file]
[Loaded sun.awt.image.PixelConverter$Rgba from shared objects file]
[Loaded sun.awt.image.PixelConverter$RgbaPre from shared objects file]
[Loaded sun.awt.image.PixelConverter$Ushort565Rgb from shared objects file]
[Loaded sun.awt.image.PixelConverter$Ushort555Rgb from shared objects file]
[Loaded sun.awt.image.PixelConverter$Ushort555Rgbx from shared objects file]
[Loaded sun.awt.image.PixelConverter$Ushort4444Argb from shared objects file]
[Loaded sun.awt.image.PixelConverter$ByteGray from shared objects file]
[Loaded sun.awt.image.PixelConverter$UshortGray from shared objects file]
[Loaded sun.awt.image.PixelConverter$Rgbx from shared objects file]
[Loaded sun.awt.image.PixelConverter$Bgrx from shared objects file]
[Loaded sun.awt.image.PixelConverter$ArgbBm from shared objects file]
[Loaded java.awt.image.ColorModel from shared objects file]
[Loaded java.awt.image.PackedColorModel from shared objects file]
[Loaded java.awt.image.DirectColorModel from shared objects file]
[Loaded java.awt.color.ColorSpace from shared objects file]
[Loaded java.awt.color.ICC_Profile from shared objects file]
[Loaded sun.awt.color.ProfileDeferralInfo from shared objects file]
[Loaded sun.awt.color.ProfileDeferralMgr from shared objects file]
[Loaded java.awt.color.ICC_ProfileRGB from shared objects file]
[Loaded sun.awt.color.ProfileActivator from shared objects file]
[Loaded java.awt.color.ICC_Profile$1 from shared objects file]
[Loaded java.awt.color.ICC_ColorSpace from shared objects file]
[Loaded sun.java2d.pipe.PixelDrawPipe from shared objects file]
[Loaded sun.java2d.pipe.PixelFillPipe from shared objects file]
[Loaded sun.java2d.pipe.ShapeDrawPipe from shared objects file]
[Loaded sun.java2d.pipe.TextPipe from shared objects file]
[Loaded sun.java2d.pipe.DrawImagePipe from shared objects file]
[Loaded sun.java2d.pipe.NullPipe from shared objects file]
[Loaded java.awt.image.IndexColorModel from shared objects file]
[Loaded sun.java2d.pipe.ParallelogramPipe from shared objects file]
[Loaded sun.java2d.pipe.LoopPipe from shared objects file]
[Loaded sun.java2d.pipe.OutlineTextRenderer from shared objects file]
[Loaded sun.java2d.pipe.GlyphListPipe from shared objects file]
[Loaded sun.java2d.pipe.GlyphListLoopPipe from shared objects file]
[Loaded sun.java2d.pipe.SolidTextRenderer from shared objects file]
[Loaded sun.java2d.pipe.AATextRenderer from shared objects file]
[Loaded sun.java2d.pipe.LCDTextRenderer from shared objects file]
[Loaded sun.java2d.pipe.CompositePipe from shared objects file]
[Loaded sun.java2d.pipe.AlphaColorPipe from shared objects file]
[Loaded sun.java2d.pipe.PixelToShapeConverter from shared objects file]
[Loaded sun.java2d.pipe.PixelToParallelogramConverter from shared objects file]
[Loaded sun.java2d.pipe.TextRenderer from shared objects file]
[Loaded sun.java2d.pipe.SpanClipRenderer from shared objects file]
[Loaded sun.java2d.pipe.Region from shared objects file]
[Loaded sun.java2d.pipe.RegionIterator from shared objects file]
[Loaded sun.java2d.pipe.Region$ImmutableRegion from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]
[Loaded sun.java2d.pipe.AlphaPaintPipe from shared objects file]
[Loaded sun.java2d.pipe.SpanShapeRenderer from shared objects file]
[Loaded sun.java2d.pipe.SpanShapeRenderer$Composite from shared objects file]
[Loaded sun.java2d.pipe.GeneralCompositePipe from shared objects file]
[Loaded sun.java2d.pipe.DrawImage from shared objects file]
[Loaded sun.java2d.loops.RenderCache from shared objects file]
[Loaded sun.java2d.loops.RenderCache$Entry from shared objects file]
[Loaded java.awt.Image from shared objects file]
[Loaded java.awt.image.VolatileImage from shared objects file]
[Loaded sun.java2d.DestSurfaceProvider from shared objects file]
[Loaded sun.awt.image.SunVolatileImage from shared objects file]
[Loaded java.awt.ImageCapabilities from shared objects file]
[Loaded sun.awt.image.SurfaceManager$ImageAccessor from shared objects file]
[Loaded java.awt.Image$1 from shared objects file]
[Loaded sun.awt.image.SurfaceManager from shared objects file]
[Loaded sun.awt.image.VolatileSurfaceManager from shared objects file]
[Loaded java.awt.image.ImageObserver from shared objects file]
[Loaded java.awt.MenuContainer from shared objects file]
[Loaded java.awt.Component from shared objects file]
[Loaded java.awt.Component$AWTTreeLock from shared objects file]
[Loaded sun.awt.windows.WObjectPeer from shared objects file]
[Loaded java.awt.peer.ComponentPeer from shared objects file]
[Loaded java.awt.dnd.peer.DropTargetPeer from shared objects file]
[Loaded sun.awt.windows.WComponentPeer from shared objects file]
[Loaded java.awt.Font from shared objects file]
[Loaded java.awt.geom.AffineTransform from shared objects file]
[Loaded sun.font.AttributeValues from shared objects file]
[Loaded java.lang.Enum from shared objects file]
[Loaded sun.font.EAttribute from shared objects file]
[Loaded java.text.AttributedCharacterIterator$Attribute from shared objects file]
[Loaded java.lang.Class$4 from shared objects file]
[Loaded sun.reflect.NativeMethodAccessorImpl from shared objects file]
[Loaded sun.reflect.DelegatingMethodAccessorImpl from shared objects file]
[Loaded java.awt.font.TextAttribute from shared objects file]
[Loaded java.lang.Integer$IntegerCache from shared objects file]
[Loaded sun.awt.AWTAccessor$ComponentAccessor from shared objects file]
[Loaded java.awt.Component$1 from shared objects file]
[Loaded sun.awt.AWTAccessor from shared objects file]
[Loaded java.awt.AWTEvent from shared objects file]
[Loaded sun.awt.RequestFocusController from shared objects file]
[Loaded java.awt.Component$DummyRequestFocusController from shared objects file]
[Loaded sun.awt.windows.WToolkit$1 from shared objects file]
[Loaded java.awt.Toolkit$DesktopPropertyChangeSupport from shared objects file]
[Loaded sun.awt.ModalityListener from shared objects file]
[Loaded sun.awt.SunToolkit$ModalityListenerList from shared objects file]
[Loaded sun.awt.SunToolkit$1 from shared objects file]
[Loaded java.util.MissingResourceException from shared objects file]
[Loaded java.awt.EventQueue from shared objects file]
[Loaded java.awt.Queue from shared objects file]
[Loaded sun.awt.AppContext from shared objects file]
[Loaded sun.awt.AppContext$1 from shared objects file]
[Loaded sun.awt.AppContext$2 from shared objects file]
[Loaded sun.awt.PostEventQueue from shared objects file]
[Loaded sun.misc.PerformanceLogger from shared objects file]
[Loaded sun.misc.PerformanceLogger$TimeData from shared objects file]
[Loaded sun.java2d.DisposerRecord from shared objects file]
[Loaded sun.awt.windows.WToolkit$ToolkitDisposer from shared objects file]
[Loaded sun.java2d.Disposer from shared objects file]
[Loaded sun.java2d.Disposer$1 from shared objects file]
[Loaded sun.awt.windows.WToolkit$2 from shared objects file]
[Loaded sun.awt.windows.WToolkit$2$1 from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]
[Loaded java.awt.peer.CanvasPeer from shared objects file]
[Loaded sun.awt.windows.WCanvasPeer from shared objects file]
[Loaded java.awt.peer.ContainerPeer from shared objects file]
[Loaded java.awt.peer.PanelPeer from shared objects file]
[Loaded sun.awt.windows.WPanelPeer from shared objects file]
[Loaded java.awt.peer.WindowPeer from shared objects file]
[Loaded sun.awt.windows.WWindowPeer from shared objects file]
[Loaded java.awt.peer.DialogPeer from shared objects file]
[Loaded sun.awt.windows.WPrintDialogPeer from shared objects file]
[Loaded java.beans.PropertyChangeListener from shared objects file]
[Loaded sun.awt.windows.WWindowPeer$ActiveWindowListener from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]
[Loaded sun.awt.windows.WWindowPeer$GuiDisposedListener from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]
[Loaded java.awt.print.Pageable from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]
[Loaded java.awt.print.Book from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]
[Loaded sun.awt.windows.WPrinterJob$HandleRecord from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]
[Loaded java.awt.print.PageFormat from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]
[Loaded java.awt.print.Paper from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]
[Loaded java.awt.Shape from shared objects file]
[Loaded java.awt.geom.RectangularShape from shared objects file]
[Loaded java.awt.geom.Rectangle2D from shared objects file]
[Loaded java.awt.geom.Rectangle2D$Double from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]


File: C:\OpenBug\test.txt exists=false


[Loaded java.util.Collections$SynchronizedCollection from shared objects file]
[Loaded java.util.IdentityHashMap$Values from shared objects file]
[Loaded java.util.IdentityHashMap$IdentityHashMapIterator from shared objects file]
[Loaded java.util.IdentityHashMap$ValueIterator from shared objects file]
[Loaded java.util.HashMap$KeySet from shared objects file]
[Loaded java.util.HashMap$HashIterator from shared objects file]
[Loaded java.util.HashMap$KeyIterator from shared objects file]
[Loaded sun.awt.AppContext$PostShutdownEventRunnable from shared objects file]
[Loaded sun.awt.MostRecentKeyValue from shared objects file]
[Loaded sun.awt.AWTAutoShutdown$1 from shared objects file]
[Loaded sun.awt.AWTAccessor$AWTEventAccessor from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]
[Loaded java.awt.AWTEvent$1 from C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar]
[Loaded java.util.IdentityHashMap$KeySet from shared objects file]
[Loaded java.util.IdentityHashMap$KeyIterator from shared objects file]

Заранее спасибо, Эндрю

Ответы [ 2 ]

1 голос
/ 08 декабря 2010

Я не совсем уверен, является ли ваш принтер или система удаленной.Вы можете переместить ваш файл во временную папку Java, а через десять вы можете получить абсолютный путь прямо оттуда.

File f2 = new File(f1.getAbsolutePath());
0 голосов
/ 16 января 2012

Я обнаружил проблему довольно давно и просто понял, что никогда не давал ответа. Проблема была с драйвером принтера принтера по умолчанию. Я считаю, что я либо обновил драйвер принтера, либо просто изменил разрешение по умолчанию.

Однако удивительно, что драйвер принтера может как-то вызвать нестабильность представления файловой системы в java.

...