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.

4 Answers 4

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) {
    }    
}
Sign up to request clarification or add additional context in comments.

2 Comments

@HovercraftFullOfEels (that's the other reason the close statement is in a try-catch ;))
@HovercraftFullOfEels no probs, your correction is more correct, I'm just lazy ;)
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

Comments

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 \.

Comments

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();
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.