Reputation: 365
I am using the following code to open a pdf file from java. The code works when I run the application from the IDE. However, when generating the jar and executing it, the code stops working. I do not know what I'm doing wrong. I have tried changing the jar of folders but it still does not work. It seems that the problem is how ubuntu 16.04 handles the routes because in windows this works correctly. The application does not throw exceptions
The way I get the pdf I do the same for another application but in it I get an image and it works both in the jar and in executing it in the ide.
jbTree.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/tree.png")));
Button code
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File (getClass().getResource("/help/help.pdf").toURI());
Desktop.getDesktop().open(myFile);
} catch (IOException | URISyntaxException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
}
The solution is run the application through the console. Trying to run it in another way does not work.
Upvotes: 0
Views: 263
Reputation: 1434
When you run the project from your IDE then the root of your project is the System.getProperty("user.dir")
e.g if it Your project root folder is PDFJar the it will look for the help.pdf in the PDFJar/src/project/help/
folder.
After building your project to jar file the executable jar is build and executed from a dist or bin folder which is now the System.getProperty("user.dir")
and the help.pdf will be seeked in the dist/src/project/help/
folder.
You either create the folder /src/project/help/ with help.pdf in it in your dist or bin directory or put your Jar file in your project root
EDITED
You can't access a resources file packed into your JAR archive as file except as input stream, The reason it works from you IDE is because the file exist in the src
folder as the executed directory is your project folder. You will need to create the file outside the JAR achive then read the stream into it so you can call Desktop to open it.
package stackoverflow;
import java.awt.Desktop;
import java.awt.HeadlessException;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author thecarisma
*/
public class StackOverflow {
public void openHelpFile() {
OutputStream outputStream = null;
try {
File outFile = new File("./help.pdf");
outFile.createNewFile(); //create the file with zero byte in same folder as your jar file or a folder that exists
InputStream in = getClass().getResourceAsStream("/help/help.pdf");
outputStream = new FileOutputStream(outFile);
int read = 0;
//now we write the stream into our created help file
byte[] bytes = new byte[1024];
while ((read = in.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
if (outFile.exists()) {
String path= outFile.getAbsolutePath();
JOptionPane.showMessageDialog(null, path);
if (Desktop.isDesktopSupported()) {
JOptionPane.showMessageDialog(null, "Enter");
try {
File myFile = new File (path);
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Exception");
}
}
} else {
JOptionPane.showMessageDialog(null, "Error Occur while reading file");
}
} catch (HeadlessException | IOException ex) {
Logger.getLogger(StackOverflow.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
outputStream.close();
} catch (IOException ex) {
Logger.getLogger(StackOverflow.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
StackOverflow stackOverFlow = new StackOverflow();
stackOverFlow.openHelpFile();
//the bellow example works for file outside the JAR archive
/**
String path= new File("help.pdf").getAbsolutePath();
JOptionPane.showMessageDialog(null, path);
if (Desktop.isDesktopSupported()) {
JOptionPane.showMessageDialog(null, "Enter");
try {
File myFile = new File (path);
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Exception");
}
} **/
}
}
DETAIL
When your resources file is packed into the JAR archive it cannot be accessed as a file except as a stream of file. The location of the file will be absolute in the JAR archive such as the /help/help.file
.
If you just want read the content of the resources such as conf, xml, text file or such you can just read it into BufferReader
i.e
InputStream in = getClass().getResourceAsStream("/conf/conf.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
else if it a binary file you will need to create a file outside the jar file with 0 byte
then read the resources stream from the JAR archive into the created file.
NOTE: You should check if the help file already exist with the same size before reading from the JAR archive to prevent reading multiple time and skip the process to increase you run time. Take note while creating your file as creating a file in a folder that does not exist in JAVA is not possible.
YOU CAN OPEN YOUR .jar
FILE WITH AN ARCHIVE MANAGER, TO VIEW IT STRUCTURE
Upvotes: 2