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.