I am trying to print an HTML file using java.awt.Desktop.print but the print dialog throws an IOException.
menuPrint.setOnAction((ActionEvent t) -> {
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.PRINT)) {
try {
File output = new File(System.getProperty("java.io.tmpdir")+"/Preview.html");
desktop.print(output);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
java.io.IOException: Failed to print C:\Users\XXX\AppData\Local\Temp\Preview.html.
Error message: A device attached to the system is not functioning.
I have a PDF printer installed and can open the print dialog using Ctrl+P. Though this same code is working on a separate machine which is connected to an actual printer.
Any clues appreciated. How to make it work?
Related
I have a Java application, and when I use java.awt.Desktop:
Desktop.getDesktop().open(file);
It works fine on Windows (opens a file in my default program), but on Ubuntu (with openJdk 13), the Java application gets stuck and I do not even get any log error or anything. I have to force quit the app in order to recover.
The file path it correct, otherwise I would actually get an Exception. Also, isDesktopSupported a isSupported(Action.OPEN) returns true.
What can I do? Can I check some system settings or logs? Or perhaps get some logs from java.awt.Desktop? Or does this not work on Ubuntu/Linux?
Are there any alternatives?
From here:
In order to use the API, you have to call java.awt.EventQueue.invokeLater() and call methods of the Desktop class from a runnable passed to the invokeLater():
void fxEventHandler() {
EQ.invokeLater(() -> {
Desktop.open(...);
});
}
I am just going to add an example function
private static void OpenFile(String filePath){
try
{
//constructor of file class having file as argument
File file = new File(filePath);
if(!Desktop.isDesktopSupported())//check if Desktop is supported by Platform or not
{
System.out.println("not supported");
return;
}
Desktop desktop = Desktop.getDesktop();
if(file.exists()) { //checks file exists or not
EventQueue.invokeLater(() -> {
try {
desktop.open(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
I am creating a simple download program that opens a file with the web browser to download it. On Mac, there is a dialog when you open a downloaded executable JAR file that says you
can't open it because it is from an unidentified developer.
Is there a way to open the JAR file without the dialog by using Java code? Here is my code:
File newFile = new File(System.getProperty("user.home")+"/Library/AppTest/Application.jar");
File file = new File(System.getProperty("user.home")+"/Downloads/AppTest.jar");
try {
Files.move(file.toPath(), newFile.toPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Desktop.getDesktop().open(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The problem with the code is when it executes, it just shows this dialog, and will not let you open the file. I want to fix this without having to go to settings, because other users may have the same problem.
To open a JAR file on a mac, use Runtime:
String command = "java -jar path/to/jar/file";
Process p = Runtime.getRuntime().exec(command);
the above code is from this link.
I tried to use the following code for opening of the PDF file, but its not working. I tried to use the run time but still not opening. Even after debugging it is invoking the open() w/o any exception still, no sign of the pdf. What Have i done wrong?
#Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_F1)
{
if (Desktop.isDesktopSupported())
{
try
{
File myFile = new File("Doc.pdf");
Desktop.getDesktop().open(myFile);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
});
Got the problem, I had a problem with my pdf viewer. Had to re-install it, now the code works just great.
I have this piece of code:
public void openSelectedFiles(MouseEvent mouseEvent){
ListView<String> listView = (ListView<String>) ((Node) mouseEvent.getSource())
.getScene().lookup("#listOfReferenceFiles");
String selectedFileString = listView.getSelectionModel().getSelectedItem();
System.out.println(Desktop.isDesktopSupported());
File fileToOpen = new File(selectedFileString);
System.out.println(fileToOpen.exists());
try {
Desktop.getDesktop().open(fileToOpen);
} catch (IOException e) {
e.printStackTrace();
}
}
I am using Java 8, and it seems to be working, until this line:
Desktop.getDesktop().open(fileToOpen);
It does not throw an exception, it just freezes my application. Is this a bug?
You must check first if desktop is allowed and if file exists to avoid this kind of problems:
//first check if Desktop is supported by Platform or not
if(!Desktop.isDesktopSupported()){
System.out.println("Desktop is not supported");
return;
}
Desktop desktop = Desktop.getDesktop();
// after check if file exists and open it
if(file.exists()) desktop.open(file);
I would like to know any java method can be used to check if the browser is closed.
I used the following code to open a default browser:
if(Desktop.isDesktopSupported()){
Desktop desktop = Desktop.getDesktop();
desktop.browse(new URI(url));
}
Then I want to perform some user actions on the opened website to capture the web traffic. After the browser is closed the web traffic will be saved.
So how can I determine if the browser is closed by using java code?
The best you can do is use Process builder:
public class Main {
public static void main(String[] args) {
try {
ProcessBuilder pb = new ProcessBuilder("C:\\Windows\\System32\\calc.exe");
Process p1 = pb.start();
p1.waitFor();
System.out.println(p1.exitValue());
} catch (Exception e) {
System.out.print(e);
}
}
}
You can launch a browser with the webpage as an argument and keep track of it like that.