From java code I am able to open Text, JPEG files but for PDF file getting error
There was an error opening this document. This file is already open or in use by another application.
Code as below :
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File(filename);
if(myFile.exists() && myFile.isFile())
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
ex.printStackTrace();
// no application registered for PDFs
}
}
Note : Same file is opening from Adobe Reader.
Using Java7, Tomcat7 and Adobe Reader XI
Close any other application that may be using be using the file, such as Adobe Reader, as that error pretty clearly suggests it is open and in use by something else.
Related
I know that this question it is similar to this one but it is different. I am trying to open a pdf file that it is in the resources folder of netbeans.
Right now I am in the EventoService.java and I have created a file object to open the pdf file (justificante.pdf) in "Other Resources" folder. I have tried to reach the pdf file like in the link before but it doesn't work because of the constructor. How can I reach it? Thank you in advance.
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File(getClass().getClassLoader().getResource("resources/justificante.pdf"));
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
}
}
I am attempting to read in files into a java application running via netbeans. I have been successful in previewing the files, but I can only preview .txt files. How can I alter my code to read in any file(s)? (eg. .doc, .docx, .pdf, .jpg, .png).
JFileChooser share = new JFileChooser();
share.showOpenDialog(null);
File f = share.getSelectedFile();
String fileName = f.getAbsolutePath();
try {
FileReader reader = new FileReader(fileName);
BufferedReader br = new BufferedReader(reader);
jTextArea1.read(br, null);
br.close();
jTextArea1.requestFocus();
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, "File not found", "Error", JOptionPane.ERROR_MESSAGE);
}
you should use something like apache tika
http://tika.apache.org/
this will allow you to read almost any kind of file
also have a look at java.io.File.list() to find out the types of files you have at a location
You can read them alright ; it's just that they are huge blobs of binary data you can't make any sense of without the appropriate tools. Open one of them with a notepad and you'll get what I'm saying.
Their associated software (Word, Reader, etc...) usually do the decoding, but you may find java libraries that can do as much.
I want my application to open a pdf file when I click a dedicated button. How would i approach this? Also if I run the application from netbeans it shows the pdf but when compiled nothing comes up?
My code
private void showHelpMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
File f = new File("ana.pdf");
try {
Desktop.getDesktop().open(f);
} catch (Exception ex) {
System.out.println(ex);}
You can explicitly give the entire file path, which might solve your problem. Also the OS you are using must support the operation. This might help:
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File("C:\\Users\\klinks\\Documents\\pdf.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException e) {
// System probably doesn't have a default PDF program
}
}
Your code gets the file from the current directory. The file is there when you run it from netbeans, but the file is not there when you run it.
Unfortunately, there's no easy way to do this. I think the best idea would be write the documentation as HTML, put it on a server, and open the web browser (using Desktop.browse). If someone else has a better idea, please comment.
I have made a Swing application and will include a file, help.pdf in the .jar file. When the user selects Help->User Guide from a JMenuItem, it should load the file in the default PDF viewer on the system.
I have the code to load the PDF,
private void openHelp() {
try {
java.net.URL helpFile = getClass().getClassLoader().getResource("help.pdf");
File pdfFile = new File(helpFile.getPath());
if (pdfFile.exists()) {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(pdfFile);
} else {
System.out.println("Awt Desktop is not supported!");
}
} else {
System.out.println("File does not exist!");
}
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
This works in the eclipse IDE, however, when I pack it into a jar for other people it no longer works.
How do I fix this problem?
The problem is that a File cannot name a component of a JAR file. What you need to do is to copy the resource from the JAR file into a temporary file in the filesystem, and open using the File for the temporary file.
File names in a .jar file are case sensitive. In your text you write Help.pdf but in the code you use help.pdf. The upper/lowercase in the Java code must match the case of the file, even if you are using a system where the filesystem is not case sensitive.
Try
getResource("Help.pdf");
instead (assuming the filename in your posting text is correct)
I think you have to retrieve the location of the jar, open it and load the pdf file from within your application. The .jar file is just a zipped archive, which can be read with java easily...
I am trying to open a file i just created in my code (so i am sure that the file exists)
The code is like this:
File file = new File(filename);
file.createNewFile();
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
...
bw.close();
try {
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
desktop.open(file);
} catch (Exception e) {
...
}
But as the title says i get a "java.io.IOException: The system cannot find the path specified" from the desktop.open(file) istruction.
The problem surely is that the file pathname contains spaces (which are translated into "%20"). Is there a way to avoid this?
I found the real problem.
It wasn't either the %20 as i supposed.
I just hadn't the privileges to directly access the file location. It's a bit complicated to explain...
i'm just sorry i coulnd't figure out the real problem before.
Thanks for your suggestions anyway!
Are you using an IDE? What is inside the variable 'filename' (it's actual contents). Line two is unnecessary.
Is the error from the stack trace pointing to BufferedWriter bw = new BufferedWriter(new FileWriter(file)); or desktop.open(file);
EDIT:
You can also try the following code
File myCSVFile; //reference to your csv file here
String execString = "excel " + myCSVFile.getAbsolutePath();
Runtime run = Runtime.getRuntime();
try {
Process pp = run.exec(execString);
} catch(Exception e) {
e.printStackTrace();
}
The java.io error is appearing because it's failing to open the file. The code above will force excel open with your file as the argument. You'll need to set your environment variable to ensure that the command 'excel' in the command line opens the Excel application.
If you're planning on releasing this application for use you can ensure that excel is installed by checking the registry, then checking the install location of Excel from there.
Try to open a different file with other applications and see if other file types are supported. As Clarisse said, IOException is thrown from the 'open' method if the specified file has no associated application or the associated application fails to be launched. If the specified file doesn't exists IllegalArgumentException is thrown, which is not in your case. If for some reason opening a CSV file with Desktop doesn't work for you, try using krslynx approach. Same can be found here. You can quickly assemble a test application for opening anything on your machine using the code found here
In the Desktop javadoc it's written :
IOException - if the specified file has no associated application or the associated application fails to be launched
So are you sure your filetype has a default application associated ?
As krslynx says, file.createNewFile() is unnecessary. However file.mkdirs() may be necessary instead, if the intermediate directories don't exist yet.
EDIT: it's not clear from your question whether this is happening in new FileWriter() or in Desktop.open(). Please clarify.