Example usage for java.awt Desktop open

List of usage examples for java.awt Desktop open

Introduction

In this page you can find the example usage for java.awt Desktop open.

Prototype

public void open(File file) throws IOException 

Source Link

Document

Launches the associated application to open the file.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    File f = new File("c:\\temp\\test.bmp");
    Desktop dt = Desktop.getDesktop();
    dt.open(f);
    System.out.println("Done.");
}

From source file:Test.java

public static void main(String[] a) {
    try {/*from w  ww. jav a 2  s  . com*/
        Desktop desktop = null;
        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
        }

        desktop.open(new File("c:\\a.doc"));
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

}

From source file:Test.java

public static void main(String[] a) {
    try {// ww  w. ja  va 2 s  .  co m
        Desktop desktop = null;
        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
        }

        desktop.open(new File("c:\\a.txt"));
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

}

From source file:com.liferay.sync.engine.util.SyncClientUpdater.java

public static void update(SyncVersion syncVersion) throws Exception {
    if (syncVersion == null) {
        syncVersion = getLatestSyncVersion();
    }/*from   w  ww . j a  v a2s.c om*/

    if (syncVersion == null) {
        return;
    }

    HttpResponse httpResponse = execute(syncVersion.getUrl());

    if (httpResponse == null) {
        return;
    }

    HttpEntity httpEntity = httpResponse.getEntity();
    Path filePath = getFilePath(httpResponse);

    Files.copy(httpEntity.getContent(), filePath, StandardCopyOption.REPLACE_EXISTING);

    Desktop desktop = Desktop.getDesktop();

    desktop.open(filePath.toFile());
}

From source file:com.igormaznitsa.sciareto.ui.UiUtils.java

public static void openInSystemViewer(@Nonnull final File file) {
    final Runnable startEdit = new Runnable() {
        @Override/*from  w w  w. ja  v a 2s  .c  o  m*/
        public void run() {
            boolean ok = false;
            if (Desktop.isDesktopSupported()) {
                final Desktop dsk = Desktop.getDesktop();
                if (dsk.isSupported(Desktop.Action.OPEN)) {
                    try {
                        dsk.open(file);
                        ok = true;
                    } catch (Throwable ex) {
                        LOGGER.error("Can't open file in system viewer : " + file, ex);//NOI18N
                    }
                }
            }
            if (!ok) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        DialogProviderManager.getInstance().getDialogProvider()
                                .msgError("Can't open file in system viewer! See the log!");//NOI18N
                        Toolkit.getDefaultToolkit().beep();
                    }
                });
            }
        }
    };
    final Thread thr = new Thread(startEdit, " MMDStartFileEdit");//NOI18N
    thr.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(final Thread t, final Throwable e) {
            LOGGER.error("Detected uncaught exception in openInSystemViewer() for file " + file, e);
        }
    });

    thr.setDaemon(true);
    thr.start();
}

From source file:com.tascape.qa.th.Utils.java

public static void openFile(File file) {
    Desktop desktop = Desktop.getDesktop();
    try {// w  w w.j  a  v  a2s.  co m
        desktop.open(file);
    } catch (IOException ex) {
        LOG.warn("Cannot open file {}", file, ex);
    }
}

From source file:com.igormaznitsa.ideamindmap.utils.IdeaUtils.java

public static void openInSystemViewer(@Nonnull final DialogProvider dialogProvider,
        @Nullable final VirtualFile theFile) {
    final File file = vfile2iofile(theFile);

    if (file == null) {
        LOGGER.error("Can't find file to open, null provided");
        dialogProvider.msgError("Can't find file to open");
    } else {/*from  w w w  . java 2 s  . c  o  m*/
        final Runnable startEdit = new Runnable() {
            @Override
            public void run() {
                boolean ok = false;
                if (Desktop.isDesktopSupported()) {
                    final Desktop dsk = Desktop.getDesktop();
                    if (dsk.isSupported(Desktop.Action.OPEN)) {
                        try {
                            dsk.open(file);
                            ok = true;
                        } catch (Throwable ex) {
                            LOGGER.error("Can't open file in system viewer : " + file, ex);//NOI18N
                        }
                    }
                }
                if (!ok) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            dialogProvider.msgError("Can't open file in system viewer! See the log!");//NOI18N
                            Toolkit.getDefaultToolkit().beep();
                        }
                    });
                }
            }
        };
        final Thread thr = new Thread(startEdit, " MMDStartFileEdit");//NOI18N
        thr.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(final Thread t, final Throwable e) {
                LOGGER.error("Detected uncaught exception in openInSystemViewer() for file " + file, e);
            }
        });

        thr.setDaemon(true);
        thr.start();
    }
}

From source file:audiomanagershell.commands.PlayCommand.java

@Override
public void execute() throws CommandException, IOException {
    String OS = System.getProperty("os.name").toLowerCase();
    Path file;//w ww . j  a  v  a 2  s.  c  om
    if (OS.equals("windows"))
        file = Paths.get(this.pathRef.toString() + "\\" + this.arg);
    else
        file = Paths.get(this.pathRef.toString() + "/" + this.arg);
    String fileName = file.getFileName().toString();
    List<String> acceptedExtensions = Arrays.asList("wav", "mp3", "flac", "mp4");
    //Get the extension of the file
    String extension = FilenameUtils.getExtension(fileName);
    if (Files.isRegularFile(file) && Files.isReadable(file)) {
        if (acceptedExtensions.contains(extension)) {
            Desktop desktop = Desktop.getDesktop();
            desktop.open(file.toFile());
            System.out.printf("The file %s will open shortly...\n", fileName);
        } else {
            throw new NotAudioFileException(fileName);
        }
    } else {
        throw new CommandException(file.toString() + " not a file or can't read");
    }
}

From source file:net.brtly.monkeyboard.Configuration.java

public boolean openPreferencesFile() {
    Desktop dt = Desktop.getDesktop();
    try {//from ww w.  j  a  v  a2 s  . c om
        dt.open(getPreferencesFile());
    } catch (Exception e) {
        return false;
    }
    return true;
}

From source file:clienteescritorio.Grafica.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    try {/*  ww w . j  ava2s  .c om*/
        String directorio = System.getProperty("user.dir") + "/temporal/";
        String imagen = "grafica.png";
        String so = System.getProperty("os.name");
        System.err.println("El sso " + so);
        File f = new File(directorio + imagen);
        ChartUtilities.saveChartAsPNG(f, chart, 600, 400);
        Runtime run = Runtime.getRuntime();
        if (so.equals("Linux"))
            run.exec("eog " + directorio + imagen);
        else {
            Desktop dt = Desktop.getDesktop();
            dt.open(f);
        }
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(rootPane, "No se pudo guardar la grfica :(, Intntelo ms tarde!");
        ex.printStackTrace();
    }

}