2

I am trying to access a file I have contained in the jar.

The beginning of my code is as follows:

import java.io.*;
import java.net.*;

public class intro {
public static void main(String[] args) throws IOException{

    URL jarUrl = intro.class.getResource("myFile.jar");
    File myJar = new File(jarUrl.toString());

    FileInputStream fis = new FileInputStream(myjar);
}

I'm getting the following error:

Exception in thread "main" java.io.FileNotFoundException: file:\...myFile.jar (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at intro.main(intro.java:10)

It seems to have no problem finding the URL or initializing the file, but I can't get the FileInputStream to work. Does anybody know what I'm doing wrong? Help would be appreciated.

user1672817
  • 21
  • 1
  • 2

4 Answers4

3

You can't access Jar'ed resources in this manner. A file in a Jar is not a File (ie as in a file system file), they are different concepts.

You should use the URL.openStream instead.

InputStream is = null;
try {
    is = jarURL.openStream();
    // ... Read from stream
} catch (IOException exp) {
} finally {
    try {
      if (is != null) {
        is.close();
      }
    } catch (Exception exp) {
    }    
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Your approach is incorrect on an number of levels:

  1. This is wrong, because a URL is not a file name:

    File myJar = new File(jarUrl.toString());
    

    Assuming that jarUrl has type java.net.URL, it should be written as:

    File myJar = new File(new URI(jarUrl));
    
  2. This is wrong because it would (assuming that myJar was correct) open the JAR file as byte stream:

    FileInputStream fis = new FileInputStream(myJar);
    

    In fact, you need to open it using the JAR / ZIP file classes, and then use them to open a stream on the file contained by the JAR. It should be something like this:

    ZipFile zip = new ZipFile(myJar);
    ZipEntry entry = zip.getEntry(filePathInZip);
    InputStream is = entry.getInputStream();
    

    (I've left out all of the necessary exception handling and code to close things to prevent resource leaks.)

  3. This looks wrong as a URL:

    file:\...myFile.jar
    

    What is with the backslash and the three dots? A well-formed URL would not use \.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

If this is a known resource, you can just use:

InputStream is =intro.class.getResourceAsStream("myFile.jar");

The other answers are making some assumptions when they say you're doing this wrong. But it is suspicious that you are trying to get to a Jar first as a resource, and then as a file...

Also, see this: getResourceAsStream() vs FileInputStream

Community
  • 1
  • 1
ykaganovich
  • 14,736
  • 8
  • 59
  • 96
0

I won't beat around the bush, and I will straight away go to the point. You can't access the file inside the jar in this way. Instead try the below code.

URL jarUrl = intro.class.getResource("myFile.jar");
InputStream is = null;
String s = new String();

try {
       is = jarURL.openStream();
       Scanner scan = new Scanner(is);

       while(scan.hasNextLine){    
          s = scan.nextLine() + s;    
       }       
 } catch (IOException exp) {
 } finally {        
     is.close();
 }
rgettman
  • 176,041
  • 30
  • 275
  • 357
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75