Search and extract a file from jar. Java - 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.

Related

Access to file from jar archive

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");

Extracting a file from the currently running JAR through code

Are there any built-in methods I can use to allow users to extract a file from the currently running JAR and save it on their disk?
Thanks in advance.
File file = new File("newname.ext");
if (!file.exists()) {
InputStream link = (getClass().getResourceAsStream("/path/resources/filename.ext"));
Files.copy(link, file.getAbsoluteFile().toPath());
}
Use getResourceAsStream (docs), you can do whatever you want with it after that.
For a two-liner you could use one of the Commons IO copy methods.
I am not sure whether you will get know from which jar your class is getting executed but you can try this to extract resources from jar :
How to write a Java program which can extract a JAR file and store its data in specified directory (location)?

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

how to call jasper report on jsp page

When I call the jrxml file through .load(), its throw a exception FileNotfoundException.
I have tried with absolute path, but it does not work. Please help.
FileNotFoundException generally means the file is not there. Get the path from your code and paste it in you filesystem explorer and see if it exists.
If it does, it means it is for some reason inaccessible:
This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist. It will also be thrown by these constructors if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing.
You should always use absolute paths in Java IO stuff. Apparently the one you tried was plain wrong. Maybe you just guessed it based on the deploy location of the webapp. You shouldn't do that. If the jrxml file is actually located in the webcontent, then you can use ServletContext#getRealPath() to convert a relative web path to an absolute disk file system path which you in turn can use further in the usual Java IO stuff.
Assuming that file.jrxml is located in webcontent root (e.g. accessible by http://example.com/contextname/file.jrxml), here's an example:
String absolutePath = getServletContext().getRealPath("file.jrxml");
File file = new File(absolutePath);
It depends on how you give it the reference to the file. Looking at the documentation for the JRXmlLoader http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/xml/JRXmlLoader.html
you can see that you can pass it in a reference to a File object. You are probably just passing in a string and that might be wrong.
Try something like
String path = "/tmp/test.jrmxl";
File jrxmlFile = new File(path);
JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFile);
with the appropriate try/catch and more and then debug on the file first before you worry about the loader.
You should probably get some of the jasperreports documentation. The books are quite good for that basic stuff.

Categories

Resources