Access to file from jar archive - java

I have file.jar
at the root of the file located readme.txt
I want to get acces to this file from my application.
Can you help me?

You cannot get the file,since it is not a file on your file system.
You have to read it as stream, like
InputStream in = getClass().getResourceAsStream("/readme.txt");
or read docs of JarURLConnection
update:
Paste your file in src and just write , File f =new File("/readme.txt").
It it gives you.
If not update your post with your file structure.

If the jar file is not on the classpath, instead at any arbitrary location, you can read it as a ZipInputStream (or jarInputStream) and process the content. An example is shown here: http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/

You can do the following:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("readme.txt");

Related

File doesn't read after creating jar using netbeans

i have a small application which checks for values from a file and display the result in a jframe.
A file contain list of word to check. this file is placed in project folder "testing" and the main source testing.java file is present in location "testing\src\testing"
input file : c:\document..\netbeans\testing\
java file : c:\document..\netbeans\testing\src\testing\
when i place the input file inside folder "c:\document..\netbeans\testing\src\testing\
" the input file is not taken as input, it works only when kept on folder "c:\document..\netbeans\testing\"
so when a jar file is created it has not included the input file in that, even i manually input that is not taking the input file in and working.
some path setting issue? what can be done to solve this issue?
any help pls??
Once you create the jar, the file becomes an embedded resource. If you try to read it as a File it will no long be the same file system path as you originally use in the program. It must now be read from the class path.
To read the file from the class path, you will want to use getClass().getResourceAsStream(), which return an InputStream. If your file is in the same location (package) as your class file, then you should use
InputStream is = getClass().getResourceAsStream("input.txt");
Then you can read from the InputStream
BufferedReader reader = new BufferedReader (new InputStreamReader(is));
This generally happens, when you don't use absolute path...!
As when you run your program from IDE(Netbeans) then the HOME_FOLDER is your ProjectFolder. Relative to which you would have given the file_path(that has to be accessed in your program).
But after building, jar is present in ProjectFolder/dist. When you run the jar file the HomeFolder is not ProjectFolder rather it is ProjectFolder/dist.
So, to make it successful, to need to copy all files and folders from ProjectFolder/dist to ProjectFolder.
Then run the jar.. Hope it will fix the issue
Try putting double backslashes in your file paths. Like this:
c:\\document..\\netbeans\\testing\\src\\testing\\
This is the format that java normally requires it to be in

Input Stream returns null for mentioned file path

I am trying to access the sample_report.jrxml file from ReportUtil.java .
Following is the code to access the jrxml file :
InputStream in = new ReportUtil().getClass().getClassLoader().getResourceAsStream("resource/sample_report.jrxml");
I am getting in as NULL. I tried various combinations to read the jrxml file.
Can any one point correct way to get the file ?
Make an entry of resources folder in your classpath.
Assuming that your project is a maven project and you follow maven conventions, then
your file sample_report.jrxml residing in resources folder will be copied into the root level of the generated jar file.
For loading files you should use:
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream( "sample_report.jrxml" );
BalusC has explained this very well here
And as Brian Roach pointed out, there is a typo in the path to your file.

Search and extract a file from jar. Java

I get jar files from net. I've been able to open it and to search for a file(.txt file) that I need inside it. For doing that I am using JarEntry and method getNextJarEntry().
Finally when I identify the file I need I would like to save it in some directory on my disc. I don't know how to do this. The object I have after file identification is JarEntry which corresponds to the file I want to save.
Can anybody suggest me a way how to store the file I need?
My bad, I only have JarInputStream and not the jar itself.
On the original JarFile object, you can pass the JarEntry and get an InputStream:
InputStream getInputStream(ZipEntry ze)
Use the JarFile.getInputStream(ZipEntry) method, read all the bytes from the input stream, and write them to a FileOutputStream.
You might load the JAR using a custom classloader (java.net.URLClassloader), then get the resource using getResourceAsStream.

proper file path of a text file to read

I want to manipulate a file in my java program.The file to read must be paralled to my src folder.
What should I give as file path?
An elaborated example might help. From your question, what I get is,
Source Path : /home/user/project1/src/
File Path : /home/user/project1/src/
If this is the case, then once you build the project, the file path is not going to remain the same. So if you say that relative path for the file to open remains the same in built code, then you can use Class.getResourceAsStream(String path) which returns you the InputStream for given file. You can then construct the File object using it.
Refer this for details.
You should have a File object representing your src folder, and then create a new File object using that:
File textFile = new File(srcFolder, relativePath);
How you determine srcFolder really depends on the context.
EDIT: If you're just trying to read a file which is present at build time, you should include it in your built jar file and use either ClassLoader.getResourceAsStream or Class.getResourceAsStream to load it at execution time.
For example, if you have this structure:
src\
com\
xyz\
Foo.class
data\
input.txt
Then you could use Foo.class.getResourceAsStream("/data/input.txt") or Foo.class.getClassLoader.getResourceAsStream("data/input.txt"). Both will give you an InputStream you can use to load the data.

reading xml file inside a jar-package

Here's my structure:
com/mycompany/ValueReader.class
com/mycompany/resources/values.xml
I can read the file in my Eclipse project, but when I export it to a .jar it can never find the values.xml.
I tried using ValueReader.class.getResource() and ValueReader.class.getResourceAsStream() but it doesn't work.
What's the problem here?
How do I get a File-object to my values.xml?
B.
You can't get a File object (since it's no longer a file once it's in the .jar), but you should be able to get it as a stream via getResourceAsStream(path);, where path is the complete path to your class.
e.g.
/com/mycompany/resources/values.xml
You can't get a File for the file because it's in a jar file. But you can get an input stream:
InputStream in = ValueReader.class.getResourceAsStream("resources/values.xml");
getResourceAsStream and getResource convert the package of the class to a file path, then add on the argument. This will give a stream for the file at path /com/mycompany/resources/values.xml.
This will work...
Thread.currentThread().getContextClassLoader().getResource("com/mycompany/resources/values.xml")
You can extract the jar then take what you want, in the same class-path using :
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new
FileInputStream(zipfile.getCanonicalFile())));

Categories

Resources