Extracting a file from the currently running JAR through code - java

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)?

Related

Store a file inside a jar library

How do you store a file inside a jar library?
Setup :
Create a simple maven project with a class that loads something from the resources folder using the getResoruceAsStream() method and a test that runs it. The test will fail with the problem being it couldn't find that file, the same issue will appear later when we try to compile it as a lib and run that class.
The problem : Using the only method I know (and the only that you can find on the web) getResourceAsStream() will give you a null reference exception, because the file is missing. So how do I add the file to the jar and load it later?
Keep the file in the src/main/resources directory and use the following method in the class where you need to load that file -
private InputStream readFileFromResourcePath(String filename) {
return getClass().getClassLoader().getResourceAsStream(filename);
}
Do not forget to close the stream after consuming it.

Reading a file into compiled jar by relative path

How would you read a file into a program that's compiled into a jar next to it through its local directory? The type read would be a simple .txt file.
It depends on what the usage of the program is. Do you know how the jar is supposed to be executed? When you try to run it, does it spit out a "usage: somejar firstarg secondarg" type message?
Also, if its a jar that you've compiled and you know how it should be executed, then you may have forgot to set its main class or manifest.
Check this: http://www.mkyong.com/java/how-to-make-an-executable-jar-file/
If you want to read a file that exists within an external .jar file, you will need to unzip the .jar file first in your code and then retrieve the file. You can do this using Java's zip APIs. See this answer if this is the case: Easiest way to unpack a jar in java
If you want to read a file that is in the same .jar file that your code is executing, you can get the file as a resource. See this answer: Get a resource using getResource()
If the file is simply in the exact same directory as the executable .jar, create a new file like so:
File input = new File("myfile.txt");

java read file for ftp upload from jar file

I am trying to upload files by ftp using the apache common library. I want to keep those file inside the jar, since these are files that will stay the same and need to be uploaded on the different hosts several time.
I am trying the following:
public void doCGIRecovery() throws IOException{
System.out.println(getClass().getResource("/cgi/example").getPath());
FileInputStream file = new FileInputStream(getClass().getResource("/cgi/example").getPath());
f.storeFile("./", file);
}
Using eclipse this works and the sysout is giving me:
/Users/user/Documents/workspace/example-Project/bin/com/example/java/example/business/cgi/example
But after the compilation into a jar it returns me the following:
file:/Users/user/Desktop/example.jar!/com/example/java/example/business/cgi/example
And I get (No such file or directory).
So how does that come? Further where does the "!" after example.jar come from?
Best Regards
Don't worry about messing with the path, use getClass().getResourceAsStream() instead.

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.

File library that help creating paths

I'm looking for a file utilities library that help creating paths, i.e. if I want to create a file in "a/b/c/x/y/z", it will check if each directory exists and creates it if not.
How about reading the File javadoc
Specifically, given File foo which represents a text file you want to create, you can execute foo.getParentFile().mkdirs().
File.mkdirs()

Categories

Resources