Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Opening and closing a document

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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?
 
Saloon Keeper
Posts: 2751
359
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 20
  • Mark post as helpful
  • send pies
  • 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: 20
  • Mark post as helpful
  • send pies
  • 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?
 
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
You don't like waffles? Well, do you like this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!