Apache Commons Configuration: Save properties file within a jar - java

All the apologies for disturbing you. I have an issue with the apache commons configuration. I have a properties file that is located at the src folder of an application I am building (with Eclipse). When I run the app and trying saving into the properties file from eclipse it works. However when I export it as a jar and try saving into the file when I run it from CLI I get the following error:
Could not save to URL rsrc:smpp-config.properties and trace shows:
ava.net.UnknowServiceException: protocol does not support output.
Please help.
Thanks

You cannot modify a file inside a jar file.
The only solution is to unzip the jar file using ZipFile and then modify the file.
Also refer the following question dealing with the same issue
Modifying a file inside a jar
Change file in jar

Related

Defining paths to .jar file

I would like to ask how to define path to files stored directly in my project folder. When i generate .jar file, xml file is stored in main folder in .jar.
private static final String PATH_TO_XML = "xmlOutput.xml";
This is working when I run project in Netbeans IDE. However, executing .jar file throws exception that file cannot be found. I tried some ways but i cannot find the way to specify path to file to be working in IDE and also as .jar file.
I read information about access as stream. However, my application executes XSLT transformation using XsltTemplate.xsl, xmlOutput.xml and htmlOutput.html so I need to use this file for transformation.
Thanks for help.
It might help to think about where the referenced files are stored in each context. If these are not packaged in the JAR like they are located in the IDE's project, then they won't be found. It might be best to configure the location in some configuration file and/or use an absolute path which is also available when running the JAR.

Produce .jar -> including .txt file is not found

In my project i write into and read from a project.txt file. This project.txt file is first reading in by programm start.
All works perfectly, but if i export my project as a .jar file and try to execute this .jar file, then my programm says, that he could not find the project.txt (i created this error-message).
The path of project.txt is "./res/project.txt". The res-folder is included in the build-path of this project. I'm using Eclipse.
What i'm doing wrong?
TO READ
Two possible causes:
you are trying to access the project.txt through java.io and not as resource (please post the code)
you access through ClassLoader.class.getResourceAsStream("/res/project.txt"); but the file was not exported into the jar (please post the code)
in othe words: the "./res/project.txt" is near the eclipse project path but outside the scope of the jar.
TO WRITE
Finally the writing into the jar file is absolutly not Java compliant, it should work in some cases such as ear or war and not jar.

JNLP Webstart Launch Issue

I have a requirement to dynamically extract the content of a Jar file to a local directory. Remaining part of application will use these content. Everything working well in my eclipse development environment. However following peace of code returns null when it comes into JNLP launch.
InputStream stream = VLCLibManager.class.getClass().getClassLoader().getSystemResourceAsStream("XXX.jar");
I already did following :
Manifest file of JAR that contains VLCLibManager.class updated with proper class-path entries
My XXX.jar is placed under /lib directory of JNLP. It's getting downloaded correctly
Have the entry (jar href="lib/XXX.jar"/>>) in XYZ.jnlp file
Any help appreciated as I'm stucked with this issue for the past few days.
Finally I resolved the issue. Thought of publishing here as it would help others who face similar issue.
I did below :
I packed resources that I wanted as a Zip file and placed into /resources directory of my maven project
Maven compiler plugin packs this zip file along with resultant jar.
So I can load the zip file to my java code using
YourClass.class.getResourceAsStream("/XXXX.zip")
This loads resources to java program. You can unzip as you wanted and use it whereever needed

Put and Use .properties file outside Jar

I'm developing a Java Application in Netbeans. The application uses a .properties file (Properties class). The problem is that Netbeans insert this properties file into final .jar file.
How can I make Netbeans not to do this and put the properties file out of jar file?
In this case, how can I use the properties file in code?
Look at the source directories that NetBeans is asked to insert into the JAR and remove the .properties from the list.
You can always read the .properties file by reading it as an InputStream, as long as it's in the CLASSPATH.
The question is: Why do you feel the need to move it out of that JAR? At least it's in the CLASSPATH that way. You seem to be acting against your own best interests here.
when you export project by jar formart from Eclipse IDE, you can config it do not import file .properties into jar file.
If you want use .properties file in the same folder with file jar, you can get current path with code:
String path = <NameClass>.class.getProtectionDomain().getCodeSource().getLocation().getPath();
and now, you can load file .properties from above path.
Good luck to you!

Is it possible in Java read files placed in a Jar that it's placed in a Ear too?

I was wondering if is possible to find the content in an XML file placed in a jar thath is placed in a ear too. It would help me find the properties of java beans.
Up into the ear I can iterate through documents and see what's inside, but if it is a jar I can't iterate documents inside that.
Someone can give me some advice?
From the ear file you should be able to extract the jar file. Then you can use WinZip, 7 Zip, etc to do explore the jar file contents the GUI. Or you can run the jar tf command to extract the content of the jar file in command line. If you don't have any of these tools and using windows, then you can rename the jar file to a .zip and windows should be able to explore it (most of the cases it works).
Edits - I am not sure if you wanted to do it using Java. In that case you are looking for JarFile. I found an example of it here for exploring Jar contents programatically.
so i just tested the thing you want to do - and as long as the JAR lies in the classpath of your EAR, then you can access any file within it. basically the try to look up the file from the context-root of your application.
for example if in your JAR the file abc.xml resides under the package a.b.resources, then from say a servlet in your EAR you can access it using :
InputStream is = this.getClass().getClassLoader().getResourceAsStream("a/b/resources/abc.properties");
Yes, you can read any file that is packed into zip file. It does not matter how many nested zip file you have to open on your way. Use ZipInputStream, get needed ZipEntry, read it content. If it is still zip, open it and do it again and again until you access the required resource.

Categories

Resources