• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Opening and closing a document

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a program which opens, then prints any given document. I did this using the Desktop class like this:



Which works perfectly. I want to be able to close it once it prints though, for which there isn't a method in the Desktop class. I've read a couple things online tat suggested I get the process id and close it like that, but I'm trying to figure out a pretty straightforward way to do that. Can anybody give me a hand with this?
 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if you know that you can call Desktop#print without opening a document first. If you do need to actually display it on the desktop first, then you will need to call Desktop#open as you have in your example.

Closing it is not simple because you don't know the PID for the launched application. If you were able to guess which application associated with the file got launched (such as notepad.exe for .txt files), running something like taskkill /IM notepad.exe (or the equivlent for other operating systems) using Runtime#exec, you would end-up killing all processes running that same application, not just the intended one.


 
Joel Blake
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah and that's part of the problem. If I already had say a .docx document open, and used my app to open another one, then just killed WINWORD.exe, they would both go. I think you solution of just printing without opening is the way I'm going to go. But just for knowledge/curiosity, is there a different way anybody knows of to open a file so that you can get the specific PID for that process?
 
Joel Blake
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I ended up going with Ron's suggestion and just going right to print without opening. It doesn't really need to be opened anyway. I'm having another weird issue though. If I open a PDF (Adobe Reader), a Notepad or Word document it just flashes the app for a second then closes it, and prints. Perfect. When I try and print an image file (.jpeg, .png etc...) it opens Windows Photo Viewer (my default app for those extensions), but takes me to the print screen and I have to manually click PRINT. Any idea why the behaviors would be different between the apps?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would have to ask each person who wrote an app which supports a "Print" option to explain why they chose their way.
reply
    Bookmark Topic Watch Topic
  • New Topic