Opening a PDF file that is packaged in a JAR

KRJuggling

I am trying to open a PDF file that is packaged into the jar file during runtime. The basic idea is that the user clicks the help option and then it displays a help file that is a PDF. Right now I have this in LineFit.class in the linefit package to try and open the help PDF:

try {
  File test = new File(LineFit.class.getClass().getResource("/linefit/helpTest.pdf").toURI());
  try {
      Desktop.getDesktop().open(test);
  } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
  } 
} catch (URISyntaxException e2) {
  // TODO Auto-generated catch block
  e2.printStackTrace();
}

It works in eclipse when I run it but if I try to export it to a runnable JAR file it does not open the PDF file and when I look into the JAR, the PDF is in the same folder as when it was in eclipse.

Kenster

new File(URI) only works for file: URIs. When a classloader finds a resource in a jar, it returns a jar URI, for example jar:file:/C:/Program%20Files/test.jar!/foo/bar.

Fundamentally, the File class is for files on the filesystem. You can't use it to represent a file within a JAR or another archive. You're going to have to copy the file out of the jar and into a regular file, then create a File referencing this new file. To read the file from the jar, you could use JarURLConnection.getInputStream with the URL you have, or you could call ClassLoader.getResourceAsStream.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Opening a PDF file that is packaged in a JAR

From Dev

Opening a PDF file stored in a jar file with the desktop's default app

From Dev

Executing wmv file packaged in jar

From Dev

Opening a .jar file

From Dev

Opening pdf file

From Dev

Gradle - Exclude file from being packaged with jar

From Dev

NoClassDefFoundError as I run a Maven packaged JAR file

From Dev

Gradle - Exclude file from being packaged with jar

From Dev

netbeans jar build file not opening

From Dev

Runnable JAR file not opening JFrame

From Dev

Function to open .pdf file not opening file

From Dev

Cannot extract jar file in java application packaged for windows

From Dev

Download external pdf files to chrome packaged app's file system

From Dev

Opening pdf files with dates through a batch file

From Dev

Opening a pdf byte stream file with Cordova

From Dev

Adding and opening a PDF file to an Xcode project in Swift

From Dev

Opening PDF file after creating CFDocument

From Dev

Opening pdf file in C# form

From Dev

PDF file not opening in browser using servlet and jsp

From Dev

MIME PDF file attachment sent by pear is not opening

From Dev

Opening PDF file in ionic 2 app

From Dev

Error opening zip file or JAR manifest missing : jrebel.jar

From Dev

Opening file using mPdfViewer.fromFile(file) shows an empty pdf

From Dev

Error opening zip file or JAR manifest missing : C:\Program

From Dev

Changing which java file runs first when opening jar

From Dev

a java exception has occured while opening .jar file

From Dev

Opening a .jar .desktop file duplicates the icon on the favorite bar

From Dev

Translating a Jar-opening Batch file into its MacOS Equivalent

From Dev

.dat files not packaged in jar in Netbeans

Related Related

HotTag

Archive