How to access the content of the jar during runtime? - java

How can I access the content of the jar file which has been started. I want to create a big jar file which contains everything I need and then during runtime I want to copy some files of my jar into an external folder. Is this possible?

You want this.getClass().getClassLoader().getResourceAsStream. Example:
InputStream config =
this.getClass().getClassLoader().getResourceAsStream("config.txt");

The files in the JAR are not accessible as files, so you must use getResourceAsStream to read them. See access files and folders in executable jars how to access the files within the jar.
Following, you use the inputstream to write the files onto the file system.
See:
Easy way to write contents of a Java InputStream to an OutputStream
http://www.mkyong.com/java/how-to-convert-inputstream-to-file-in-java/

You can access any file via the ClassPath using the classloader. Start with Class.getResourceAsStream

Related

Java FileNotFoundException when trying to read txt file from resources folder

I'm trying to read a text file located in src/main/resources/test/file.txt. I'm trying to get the path of the file using String path = getClass().getResource("/text/file.txt").getFile(); but when I try to read it I get a FileNotFoundException. I tried putting many different paths, all of which failed. How can I go about doing this?
The idea of putting something into the src/main/resources tree is that it will be copied into the JAR file that you build from your project. It will then be available to your application via the Class methods getResource(String) and getResourceAsStream(String) methods.
When you are running in your application in the development environment, it is certainly possible to use FileInputStream etcetera to access the resource. But this won't work in production. In production, the resources will then be inside your app's JAR file. FileInputStream cannot open a JAR file and its contents by name.
When you do this:
getClass().getResource("/text/file.txt");
you get a URL for the resource, which will look something like this:
jar:file:/path/to/your.jar!/text/file.txt"
It is not possible to turn that into a pathname the FileInputStream will understand. Whatever you try will give you a FileNotFoundException ... or something that is not the resource you want to read.
So what to do?
You have a few options, depending on your application's requirements.
You can use getResourceAsStream and use the resulting input stream directly.
You can copy the contents of getResourceAsStream to a temporary file, and then use the pathname of the temporary file.
You can create an application specific directory (e.g. in the user's home directory) and extract the file you need from the JAR into the directory. You might do this the first time the application runs.
You could open the JAR file as a JarFile and use that API to open an InputStream for the resource. But this assumes that that the resources are in a JAR ... and on some platforms (e.g. Windows) you may encounter problems with file locking. (And it would be a bad idea to attempt to update the resource in the JAR.)
Try giving complete path of the file from the disk.
C:\Users\MyUser\Desktop\file name with extension

Relative paths in properties file in Eclipse

How to configure properties file in Eclipse java?
How can we provide relative addresses in the properties file?
These two lines are working
modelsPath=C:\\Users\\rishika.shrivastava\\workspace\\CSVWEB\\src\\com\\models
csvFilePath=c:/users/rishika.shrivastava/workspace/CSVWEB/
But when i use relative addresses like this:
modelsPath=/CSVWEB\\src\\com\\models
csvFilePath=/CSVWEB/
it doesn't work.
If your files are to be resource on the classpath, then you should read them as resources, not as files on the file system, (which is what happens when you use File or a FileXxx variant).
To read resources from the classpath, you could do
getClass().getResource("/com/models/file")
Or if you need an InputStream you can do
getClass().getResourceAsStream("/com/models/file")
Some Resources
Class API to see what those methods do
How to really read text file from classpath in Java

Creating and executable JAR file that uses an external XML file

I have a java application in Eclipse that uses an eml file like so
File matches = new File("matches.xml");
The file is located in the default package as all the other classes. When I create the JAR it bundles in the XML file with it. My application require me to be able to make changes to the XML file. How can I set it up so the JAR can reference the XML file outside of itself?
My application require me to be able to make changes to the XML file.
Then you will need to extract it from the Jar and save it somewhere on the local file system.
See How can an app use files inside the JAR for read and write? for more details.
If you're using new File("matches.xml") that won't use a file within a jar file at all. It will only look on the external file system.
If you need to be able to use an external file if it's present, or the version in the jar file as a fallback, you'll need to test for the file's existence (File.exists()) and use Class.getResourceAsStream("matches.xml") for the fallback behaviour.
As you want to keep the file outside the jar and want to update it so the jar can read, so you can put the file in the same directory where the jar is and use the following code to access the file
FileInputStream file = new java.io.FileInputStream("matches.xml");
So this can be the directory structure.
- matches\
- matches.jar
- matches.xml

finding static file in jar

I am trying to use a jar file which itself is a web application in another web project. In my jar which i have created using eclipse's export to jar functionality, I have stored a csv file in a folder. To use relative paths in the code in the jar I access it using
MyClass.class.getResource(ApplicationConstants.ALIASESFILE).getPath();
and this works fine when I deploy (glassfish) and use the project as a separate application. But when I am using the same from within another project, it gives a path as shown below
D:\javaProjects\AutomodeGS_Prachi\lib\internal\RESTWSGS.jar!\aliases\aliases.csv
I am getting a file notfound exception.What could be wrong?
The getResource() method is returning a "jar:" URL. The path component of that URL is not a normal filesystem pathname, and can't be opened directly using Java's file classes.
The simple way to do this is to use Class.getResourceAsStream(...) to open the stream. If you need an "identifier" for the JAR entry, use Class.getResource(...), but then open the stream using URL.openStream().
This works fine from glassfish may be because glassfish has exploded jar on file system so that your csv file is acutually a file to the file system,
if you try to read it from another project it fails because the jar containing your file is in classpath that is fine, but the csv file is under jar file and it is no longer a File
You can read it as Stream
InputStream is = MyClass.class.getResourceAsStream(ApplicationConstants.ALIASESFILE);

Java FileNotFoundException when running a jar file

In my project I load my resource using
getClass().getResource("/package/my_reource.file").getFile()
All works good when I run the project in netbeans, but if I run the jar file, I get FileNotFoundException, why?
Thanks.
You can use InputStream rather than getClass().getResource("/package/my_reource.file").getFile()
You should use
getClass.getResourceAsStream("/package/myresource.file")
I don't think you need the filename. You rather need its content. So use getResourceAsStream() to obtain the InputStream and read the content from there.
Check your jar. I believe that your file is not there.
The reasons depend on how are you creating your jar. If you are doing it using netbeans, check your settings. Probably it includes only *.class files? The same is about ant. Check tag.
The getFile() returns the file path portion of the URL returned by getResource()
So if its in the Jar, you have to read the jar to gett he file. If its on the filesystem you can read using FileInputStream.
If you want to get the InputStream and you don't create where you get it from use getResourceAsStream()

Categories