1

I'm working on a packet analyser program and have encountered some issues:

  1. I want to open an exe file that would capture packets and write to another file.
  2. I want to open it in elevated mode.

I have written somewhat :

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import javax.swing.JOptionPane;

public class Options extends javax.swing.JFrame {
    private void recActionPerformed(java.awt.event.ActionEvent evt) {
        if (Desktop.isDesktopSupported()) {
            try {
                File myFile = new File(
                        "C:\\Users\\HP\\Documents\\response\\Server_Analyser\\src\\server_analyser\\serverdump\\reader.exe");
                Desktop.getDesktop().open(myFile);
            } catch (IOException e) {
                JOptionPane.showMessageDialog(null, "Sorry cannot find desired file !");
            }
        }
    }
}
MWiesner
  • 8,868
  • 11
  • 36
  • 70
Biswajit Roy
  • 508
  • 2
  • 7
  • 19
  • Instead of using Desktop, create a [ProcessBuilder](http://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html), and call its inheritIO() method before starting the process. That will tell you what’s going wrong—which I suspect will turn out to be a message about insufficient permissions. – VGR Jun 05 '17 at 15:48
  • 2
    "Opening an exe file" doesn't mean what you think it means. Your code opens it, but what you want to do is running it. – Jeremy Grand Jun 08 '17 at 12:51

1 Answers1

1

The Desktop.getDesktop().open(FILE) command opens the file with the associated program only. This would work for example with a .txt file that is associated with notepad. As a .exe is not associated, as it is a program on its own, it won't work.

To run a .exe from your java code you can use this command

Process process = new ProcessBuilder("C:\\Users\\HP\\Documents\\response\\Server_Analyser\\src\\server_analyser\\serverdump\\reader.exe").start();

This way you also can control the executed program over your java application and e.g. shut it down again.

Herr Derb
  • 4,977
  • 5
  • 34
  • 62
  • I got that.Help me out on how to open an exe in elevated mode. – Biswajit Roy Jun 08 '17 at 14:13
  • This is answered here https://stackoverflow.com/questions/1385866/java-run-as-administrator I don't know your program. It might be easier if you start your program already elevated to skip this step. But I don't know if the use of your program allows that. – Herr Derb Jun 08 '17 at 14:34