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
}
}
Related
I can't load an resource file after extracting a project as a runnable jar.
It works in Eclipse but after exporting it throws a FileNotFoundException.
I have tried to put the res folder next to the .jar file but nothing helps. I've tried with JarSplice and got it running with all the libraries but it stops with the resource file. The object file is located in a source folder.
What can I do?
Code
FileReader fr = null;
try {
fr = new FileReader(new File("res/" + fileName + ".obj"));
} catch (FileNotFoundException e) {
System.err.println("Couldn't load object file!");
e.printStackTrace();
}
EDIT: By opening the runnable .jar file in 7zip I can see that the whole /res folder has disappeared during the exporting and the files in the directory now lies directly in the root folder of the .jar file.
Based on your code, the res folder should be placed directly off the present working directory, which should be the directory where you are running the Java command from. For an example, see this question on how to determine the current working directory from inside Java.
Use Path instead of new File(string).
FileReader fr = null;
try {
fr = new FileReader(Paths.get("res/" + fileName + ".obj").toFile());
} catch (FileNotFoundException e) {
System.err.println("Couldn't load object file!");
e.printStackTrace();
}
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.
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 a String like this "D:/Data/files/store/file.txt" now I want to check ,is directory is already exist or not, if not I want to create directory along with text file. I have tried mkdirs() but its creating directory like this data->files->store->file.txt. means its creates file.txt as folder, not a file. can any one kindly help me to do this. thanks in advance.
You need to run mkdirs() on parent directory, not the file itself
File file = new File("D:/Data/files/store/file.txt");
file.getParentFile().mkdirs();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Here you go...
boolean b = (new File("D:/Data/files/store/file.txt").getParentFile()).mkdirs();
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...