user2484253
user2484253

Reputation: 11

Closing windows (not Swing) programmatically in Java

I'm writing a program to separate a large PDF into smaller PDFs based on whether the page is aligned vertically or horizontally. Occasionally, the program can't determine which orientation the page is, in which case it opens the PDF as an image in the user's default photo viewer (mine is Windows Photo Viewer) and asks the user to choose the orientation. My code for bringing up the picture is:

Desktop dt = Desktop.getDesktop();
File picture = new File(pic.getFileName());
Object[] options = { "Vertical", "Horizontal", "Cancel" };
dt.open(picture);
int n = JOptionPane.showOptionDialog(null,
        "This PDF orientation could not be determined.\n" +
        "Select your desired orientation below.",
        "PDF Orientation Error", JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE, null, options, options[2]);

if (n == JOptionPane.YES_OPTION) {orientation = 0;}
if (n == JOptionPane.NO_OPTION) {orientation = 1;}
if (n == JOptionPane.CANCEL_OPTION || n == JOptionPane.CLOSED_OPTION) {System.exit(0);}

The problem is that the program leaves the picture open in the default viewer after the user selects the orientation; sometimes the PDF may have twenty or thirty undetermined pages, which can be frustrating for the user to deal with. I was wondering if there's a way I can close the Windows Photo Viewer window (or exit the Windows Photo Viewer Program altogether) after the user selects the orientation. Any help is greatly appreciated.

Upvotes: 0

Views: 302

Answers (1)

dghalbr
dghalbr

Reputation: 109

Try using the taskkill command example:
taskkill /IM notepad.exe
this site has some of its uses, maybe it will help! :)
http://www.tech-recipes.com/rx/446/xp_kill_windows_process_command_line_taskkill/

try{
    Process process = runtime.exec("taskkill /IM notepad.exe");
} catch (IOException ioe){
    JFrame alertFrame = new JFrame();
    JOptionPane.showMessageDialog(alertFrame, "Error closing notepad.");
}

I haven't tested the taskkill command but this is from a working example

Upvotes: 1

Related Questions