Is it possible to run Acrobat Reader without putting the entire path of your executable?

6

I am opening files in PDF format by Java and for this I am running Acrobat through Java also, follow the path line to the Acrobat executable:

String cmd = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe";

However, sometimes it may be that the Acrobat executable is not in this path, is it possible to run Acrobat without having to indicate all its path in Java?

The complete code is this:

String cmd = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe";

String param1 = "/A";
String param2 = "page=" + Integer.toString(i) + "&zoom=150";

String[] cmds = new String[]{cmd, param1, param2, pdfFile.getAbsolutePath()};
Process p = new ProcessBuilder(cmds).start();
    
asked by anonymous 19.08.2016 / 22:26

2 answers

5
  

However, sometimes it may be that the Acrobat executable is not in this path, is it possible to run Acrobat without having to indicate all its path in Java?

Will the file have to be opened exclusively with Acrobat? if it is not, you can open the file with the associated program for that file type with the Desktop#open :

if (Desktop.isDesktopSupported()) {
   try {
       File myFile = new File("c:\teste1.pdf");
       Desktop.getDesktop().open(myFile);
   } catch (IOException ex) {
       // Não há programas instalados para abrir esse arquivo
   }
}

If you need to pass arguments to the file, the Desktop#open method may not work in this case.

What you will have to do is look for the file type of the extension with the command % w / > and use the assoc command to get the program associated with the file type.

In this SOen response shows how to implement this:

public static void openPdfWithParams(File pdfFile, String params){  
    try {
        // encontra o tipo de arquivo da extensão pdf
        Process p = Runtime.getRuntime().exec("cmd /c assoc .pdf");
        p.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = reader.readLine();
        String type = line.substring(line.indexOf("=")+1);
        reader.close();

        // encontra o executável associado ao tipo de arquivo
        p = Runtime.getRuntime().exec("cmd /c ftype " + type);
        p.waitFor();
        reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        line = reader.readLine();
        reader.close();
        String exec = line.substring(line.indexOf("=") + 1);

        // Substitui "%1" parâmetros e o caminho de arquivo
        String commandParams = String.format("/A \"%s\" \"%s\"", params ,pdfFile.getAbsolutePath()); 
        String command = exec.replace("\"%1\"", commandParams);
        p = Runtime.getRuntime().exec(command);


    } catch (Exception e) {
        System.out.println("Erro ao tentar abrir o arquivo PDF");
        e.printStackTrace();
    }
}

To use, do so:

public static void main (String[] args) throws java.lang.Exception
{
    openPdfWithParams(new File("C:\teste1.pdf"), "page=5&zoom=150");
}

The above line will try to open the C: \ test1.pdf file on the 5 page with zoom of 150 in> with the associated program to open ftype files.

    
19.08.2016 / 23:23
1

There would be another alternative to opening a .pdf file, however:

public static void main(String args[]) {
    try {
        // o arquivo myfile.pdf sera aberto assim
        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "c:\myfile.pdf");

    } catch (Exception e) {
        System.out.println("Error" + e); //imprime o erro
    }
}
    
19.08.2016 / 22:53